From e608bf69280c11cca58e55ce2a83c435df7b41c3 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 6 Nov 2014 01:11:22 +0100 Subject: [PATCH] Manual login works again, too --- Site5/Security/Login.aspx | 66 +++++++++++++++++++++++++++++++++--- Site5/Security/Login.aspx.cs | 39 +++++++++++++++++++-- 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/Site5/Security/Login.aspx b/Site5/Security/Login.aspx index 3c202f9e..507203a6 100644 --- a/Site5/Security/Login.aspx +++ b/Site5/Security/Login.aspx @@ -8,6 +8,7 @@ + @@ -40,13 +41,70 @@ recheckLogin(); }, 1000); + + $('.InputManualCredentials').on('input', function () { + onInputCredentials(); + }); + + $('#TextLogin').focus(); + }); // end of document.ready + + var manualCredentialsTestTrigger; + + function onInputCredentials() { + clearTimeout(manualCredentialsTestTrigger); + + manualCredentialsTestTrigger = setTimeout(function () { + testManualCredentials(); + }, 1000); + } + + function testManualCredentials() { + // Submit credentials to server. If valid, they will validate through the recheckLogin call. + + var jsonData = {}; + jsonData.credentialsLogin = $('#TextLogin').val(); + jsonData.credentialsPass = $('#TextPass').val(); + jsonData.credentials2FA = $('#Text2FA').val(); + jsonData.logonUriEncoded = bitIdUri; + + $.ajax({ + type: "POST", + url: "Login.aspx/TestCredentials", + data: $.toJSON(jsonData), + contentType: "application/json; charset=utf-8", + dataType: "json", + success: function (msg) { + if (msg.d) { + alert('Logging in, please wait...'); + } else { + // do nothing + } + }, + error: function (msg) { + // retry after a second + alert(msg.responseText); + console.log(msg); + setTimeout(function () { + testManualCredentials(); + }, 1000); + } + + }); + } + function recheckLogin() { + + var jsonData = {}; + jsonData.uriEncoded = bitIdUri; + jsonData.nonce = bitIdNonce; + $.ajax({ type: "POST", url: "Login.aspx/TestLogin", - data: '{"uriEncoded": "' + bitIdUri + '","nonce": "' + bitIdNonce + '"}', + data: $.toJSON(jsonData), contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { @@ -113,9 +171,9 @@
- - - + + +
User  
Pass  
2FA  
User  
Pass  
2FA  
diff --git a/Site5/Security/Login.aspx.cs b/Site5/Security/Login.aspx.cs index 02ddbe3e..176d4262 100644 --- a/Site5/Security/Login.aspx.cs +++ b/Site5/Security/Login.aspx.cs @@ -12,6 +12,7 @@ using System.Web.Services; using System.Web.SessionState; using System.Web.UI; using Swarmops.Basic.Enums; +using Swarmops.Basic.Types; using Swarmops.Database; using Swarmops.Logic.Cache; using Swarmops.Logic.Financial; @@ -36,7 +37,7 @@ namespace Swarmops.Pages.Security return; } - Persistence.Key["Debug_RawData"] = Request.ToRaw(); + // Persistence.Key["Debug_RawData"] = Request.ToRaw(); // Check for POST data - for BitId via Webform @@ -65,7 +66,7 @@ namespace Swarmops.Pages.Security BitIdCredentials credentials = new JavaScriptSerializer().Deserialize( new StreamReader(Request.InputStream).ReadToEnd()); - // TODO: untested but seems to work. Throws? + // TODO: untested but seems to work. Throws? ProcessRespondBitId(credentials, Response); return; @@ -193,7 +194,8 @@ namespace Swarmops.Pages.Security response.StatusCode = 200; response.SetJson(); - response.Write("{\"address\":\"" + credentials.address + "\",\"signature\":\"" + credentials.signature + "\"}"); + response.Write("{\"address\":\"" + credentials.address + "\",\"signature\":\"" + credentials.signature + + "\"}"); response.End(); } else @@ -238,6 +240,37 @@ namespace Swarmops.Pages.Security } } + [WebMethod] + // ReSharper disable once InconsistentNaming + public static bool TestCredentials(string credentialsLogin, string credentialsPass, string credentials2FA, string logonUriEncoded) + { + if (!string.IsNullOrEmpty(credentialsLogin.Trim()) && !string.IsNullOrEmpty(credentialsPass.Trim())) + { + string logonUri = HttpUtility.UrlDecode(logonUriEncoded); + + try + { + Person authenticatedPerson = Swarmops.Logic.Security.Authentication.Authenticate(credentialsLogin, + credentialsPass); + + // TODO: Determine last logged-on organization. Right now, log on to Sandbox. + + GuidCache.Set(logonUri + "-LoggedOn", + authenticatedPerson.Identity.ToString(CultureInfo.InvariantCulture) + ",1,,AuthPlain"); + return true; + } + catch (UnauthorizedAccessException) + { + return false; + } + + } + + return false; + } + + + protected override void OnPreInit(EventArgs e) { base.OnPreInit(e);