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
This commit is contained in:
Jordan Brown
2018-10-11 11:26:43 -04:00
parent 3e384abd60
commit c628cf135f
4 changed files with 9 additions and 7 deletions
@@ -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!" });
@@ -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);
@@ -34,14 +34,15 @@ namespace Tgstation.Server.Host.Security
public CryptographySuite(IPasswordHasher<User> passwordHasher) => this.passwordHasher = passwordHasher ?? throw new ArgumentNullException(nameof(passwordHasher));
/// <inheritdoc />
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;
}
/// <inheritdoc />
@@ -12,7 +12,8 @@ namespace Tgstation.Server.Host.Security
/// </summary>
/// <param name="user">The <see cref="User"/> whos <see cref="User.PasswordHash"/> is to be set</param>
/// <param name="newPassword">The new password for the <see cref="User"/></param>
void SetUserPassword(User user, string newPassword);
/// <param name="newUser">If the <paramref name="user"/> is just being created</param>
void SetUserPassword(User user, string newPassword, bool newUser);
/// <summary>
/// Checks a given <paramref name="password"/> matches a given <paramref name="user"/>'s <see cref="User.PasswordHash"/>. This may result in <see cref="User.PasswordHash"/> being modified and this should be persisted