From b62eda0d9b3f128cb4cc7a2a131dcb51e6615366 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 16 Mar 2016 08:25:09 +0100 Subject: [PATCH] Added debug logging for Linux environment - there's a subtle difference between MS.Net and Mono here somewhere --- Logic/Security/Authentication.cs | 11 +++++++++++ Logic/Support/SwarmopsLog.cs | 18 +++++++++++++++++- Logic/Swarm/Person.cs | 7 ++++++- Site5/Pages/v5/Public/Initialize.aspx.cs | 12 ++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Logic/Security/Authentication.cs b/Logic/Security/Authentication.cs index 309e5151..9ddc6fb2 100644 --- a/Logic/Security/Authentication.cs +++ b/Logic/Security/Authentication.cs @@ -29,11 +29,15 @@ namespace Swarmops.Logic.Security /// public static Person Authenticate (string loginToken, string password) { + SwarmopsLog.DebugLog(string.Format("Authenticating '{0}' against '{1}'", loginToken, password)); + // Get the list of people that match the login token. People candidatePeople = GetPeopleByLoginToken (loginToken); Person authenticatedUser = null; + SwarmopsLog.DebugLog ("- candidate count: " + candidatePeople.Count); + // For every person in the list, test the supplied password against the current and legacy hash schemes. foreach (Person candidate in candidatePeople) @@ -48,6 +52,8 @@ namespace Swarmops.Logic.Security { if (membership.Active) { + SwarmopsLog.DebugLog ("- candidate #" + candidate.Identity + " has a valid participation"); + hasActiveMemberships = true; break; } @@ -57,6 +63,7 @@ namespace Swarmops.Logic.Security if (!hasActiveMemberships) { + SwarmopsLog.DebugLog("- candidate #" + candidate.Identity + " has NO valid participations"); continue; } @@ -157,6 +164,8 @@ namespace Swarmops.Logic.Security // Longer than standard (10) on purpose. Should be good through 2020 or so. string passwordHash = BCrypt.HashPassword (personId.ToString (CultureInfo.InvariantCulture) + password, salt); + SwarmopsLog.DebugLog ("- storing salted pwd " + passwordHash); + return passwordHash; } @@ -168,6 +177,8 @@ namespace Swarmops.Logic.Security return false; // not a BCrypt hash with work factor 12; fall back to legacy or fail. Caller's problem } + SwarmopsLog.DebugLog ("- password hash is " + person.PasswordHash); + return BCrypt.CheckPassword (person.Identity.ToString (CultureInfo.InvariantCulture) + password, person.PasswordHash); } diff --git a/Logic/Support/SwarmopsLog.cs b/Logic/Support/SwarmopsLog.cs index aa15b2d8..bd332ad4 100644 --- a/Logic/Support/SwarmopsLog.cs +++ b/Logic/Support/SwarmopsLog.cs @@ -1,4 +1,6 @@ -using Swarmops.Database; +using System; +using System.IO; +using Swarmops.Database; using Swarmops.Logic.Swarm; namespace Swarmops.Logic.Support @@ -19,5 +21,19 @@ namespace Swarmops.Logic.Support return SwarmopsLogEntry.FromIdentityAggressive (logEntryId); } + + public static void DebugLog (string logEntry) + { + if (Path.DirectorySeparatorChar == '\\') + { + System.Diagnostics.Trace.WriteLine (logEntry); + } + else + { + // Linux - the reason we made this function to begin with + + File.AppendAllText ("/tmp/swarmops-debug.log", DateTime.UtcNow.ToString("yyyy-MM-dd/HH:mm:ss.fff ") + logEntry + @"\n"); + } + } } } \ No newline at end of file diff --git a/Logic/Swarm/Person.cs b/Logic/Swarm/Person.cs index 103ea05d..c426e0c9 100644 --- a/Logic/Swarm/Person.cs +++ b/Logic/Swarm/Person.cs @@ -793,7 +793,12 @@ namespace Swarmops.Logic.Swarm public Participation AddParticipation (Organization organization, DateTime expires) { - return AddParticipation (organization.Identity, expires); + SwarmopsLog.DebugLog (string.Format("Adding participation in {0} until {1:yyyy-MM-dd HH:mm:ss}", organization.Name, expires)); + + Participation participation = AddParticipation (organization.Identity, expires); + + SwarmopsLog.DebugLog ("Created participation #" + (participation != null? participation.Identity.ToString() : "null")); + return participation; } private Participation AddParticipation (int organizationId, DateTime expires) diff --git a/Site5/Pages/v5/Public/Initialize.aspx.cs b/Site5/Pages/v5/Public/Initialize.aspx.cs index 29adeff3..28d47ae5 100644 --- a/Site5/Pages/v5/Public/Initialize.aspx.cs +++ b/Site5/Pages/v5/Public/Initialize.aspx.cs @@ -682,6 +682,8 @@ namespace Swarmops.Frontend.Pages.v5.Public { // Make sure that no first person exists already, as a security measure + SwarmopsLog.DebugLog (String.Format("Creating first user: '{0}', '{1}', '{2}'", name, mail, password)); + Person personOne = null; bool personOneExists = false; @@ -715,11 +717,17 @@ namespace Swarmops.Frontend.Pages.v5.Public throw new InvalidOperationException ("Cannot run initialization processes again when initialized."); } + SwarmopsLog.DebugLog("Person one did not exist, creating"); + Person newPerson = Person.Create (name, mail, password, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, DateTime.MinValue, PersonGender.Unknown); + SwarmopsLog.DebugLog("Adding participation in Sandbox"); + newPerson.AddParticipation (Organization.Sandbox, DateTime.MaxValue); // Add membership in Sandbox + SwarmopsLog.DebugLog ("Creating positions"); + // Initialize staffing to System and Sandbox with the new user Positions.CreateSysadminPositions(); @@ -757,9 +765,13 @@ namespace Swarmops.Frontend.Pages.v5.Public // Get authenticated person + SwarmopsLog.DebugLog (" - before calling Authenticate"); + Person expectedPersonOne = Authentication.Authenticate ("1", this.TextFirstUserPassword1.Text); + SwarmopsLog.DebugLog(" - after calling Authenticate"); + if (expectedPersonOne != null) { Authority firstAuthority = Authority.FromLogin(expectedPersonOne, Organization.Sandbox);