From c628cf135f9df91beee32d75f44280b9c0a6b7ae Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Thu, 11 Oct 2018 11:26:43 -0400 Subject: [PATCH] Don't set User.LastPasswordUpdate when initially creating a user. This prevents current tests from failing but we might have to deal with it later if we ever change a user's password --- src/Tgstation.Server.Host/Controllers/UserController.cs | 4 ++-- src/Tgstation.Server.Host/Models/DatabaseSeeder.cs | 4 ++-- src/Tgstation.Server.Host/Security/CryptographySuite.cs | 5 +++-- src/Tgstation.Server.Host/Security/ICryptographySuite.cs | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Tgstation.Server.Host/Controllers/UserController.cs b/src/Tgstation.Server.Host/Controllers/UserController.cs index f69a285c1f..e41f4f7380 100644 --- a/src/Tgstation.Server.Host/Controllers/UserController.cs +++ b/src/Tgstation.Server.Host/Controllers/UserController.cs @@ -109,7 +109,7 @@ namespace Tgstation.Server.Host.Controllers { if (model.Password.Length < generalConfiguration.MinimumPasswordLength) return BadRequest(new ErrorMessage { Message = String.Format(CultureInfo.InvariantCulture, "Password must be at least {0} characters long!", generalConfiguration.MinimumPasswordLength) }); - cryptographySuite.SetUserPassword(dbUser, model.Password); + cryptographySuite.SetUserPassword(dbUser, model.Password, true); } dbUser.CanonicalName = dbUser.Name.ToUpperInvariant(); @@ -143,7 +143,7 @@ namespace Tgstation.Server.Host.Controllers { if (originalUser.PasswordHash == null) return BadRequest(new ErrorMessage { Message = "Cannot convert a system user to a password user!" }); - cryptographySuite.SetUserPassword(originalUser, model.Password); + cryptographySuite.SetUserPassword(originalUser, model.Password, false); } else if (model.SystemIdentifier != null && model.SystemIdentifier != originalUser.SystemIdentifier) return BadRequest(new ErrorMessage { Message = "Cannot change a user's system identifier!" }); diff --git a/src/Tgstation.Server.Host/Models/DatabaseSeeder.cs b/src/Tgstation.Server.Host/Models/DatabaseSeeder.cs index 5076d67cb8..fbe245ad79 100644 --- a/src/Tgstation.Server.Host/Models/DatabaseSeeder.cs +++ b/src/Tgstation.Server.Host/Models/DatabaseSeeder.cs @@ -37,7 +37,7 @@ namespace Tgstation.Server.Host.Models CanonicalName = Api.Models.User.AdminName.ToUpperInvariant(), Enabled = true, }; - cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword); + cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword, true); databaseContext.Users.Add(admin); } @@ -57,7 +57,7 @@ namespace Tgstation.Server.Host.Models else { admin.Enabled = true; - cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword); + cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword, false); } await databaseContext.Save(cancellationToken).ConfigureAwait(false); diff --git a/src/Tgstation.Server.Host/Security/CryptographySuite.cs b/src/Tgstation.Server.Host/Security/CryptographySuite.cs index 557170cf75..b702fc0475 100644 --- a/src/Tgstation.Server.Host/Security/CryptographySuite.cs +++ b/src/Tgstation.Server.Host/Security/CryptographySuite.cs @@ -34,14 +34,15 @@ namespace Tgstation.Server.Host.Security public CryptographySuite(IPasswordHasher passwordHasher) => this.passwordHasher = passwordHasher ?? throw new ArgumentNullException(nameof(passwordHasher)); /// - public void SetUserPassword(User user, string newPassword) + public void SetUserPassword(User user, string newPassword, bool newUser) { if (user == null) throw new ArgumentNullException(nameof(user)); if (String.IsNullOrEmpty(newPassword)) throw new ArgumentNullException(nameof(newPassword)); user.PasswordHash = passwordHasher.HashPassword(user, newPassword); - user.LastPasswordUpdate = DateTimeOffset.Now; + if (!newUser) + user.LastPasswordUpdate = DateTimeOffset.Now; } /// diff --git a/src/Tgstation.Server.Host/Security/ICryptographySuite.cs b/src/Tgstation.Server.Host/Security/ICryptographySuite.cs index 137cbf54ff..87e64f7978 100644 --- a/src/Tgstation.Server.Host/Security/ICryptographySuite.cs +++ b/src/Tgstation.Server.Host/Security/ICryptographySuite.cs @@ -12,7 +12,8 @@ namespace Tgstation.Server.Host.Security /// /// The whos is to be set /// The new password for the - void SetUserPassword(User user, string newPassword); + /// If the is just being created + void SetUserPassword(User user, string newPassword, bool newUser); /// /// Checks a given matches a given 's . This may result in being modified and this should be persisted