| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Validates user credentials |
| | | 11 | | /// </summary> |
| | | 12 | | public class DefaultUserCredentialsValidator : IUserCredentialsValidator |
| | | 13 | | { |
| | | 14 | | private readonly IUserProvider _userProvider; |
| | | 15 | | private readonly IUserStore? _userStore; |
| | | 16 | | private readonly ISecretHasher _secretHasher; |
| | | 17 | | private readonly ILogger<DefaultUserCredentialsValidator> _logger; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class. |
| | | 21 | | /// </summary> |
| | 7 | 22 | | public DefaultUserCredentialsValidator(IUserProvider userProvider, ISecretHasher secretHasher) : this(userProvider, |
| | | 23 | | { |
| | 7 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class. |
| | | 28 | | /// </summary> |
| | 2 | 29 | | public DefaultUserCredentialsValidator(IUserProvider userProvider, IUserStore userStore, ISecretHasher secretHasher) |
| | | 30 | | { |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class. |
| | | 35 | | /// </summary> |
| | 12 | 36 | | public DefaultUserCredentialsValidator(IUserProvider userProvider, IUserStore? userStore, ISecretHasher secretHasher |
| | | 37 | | { |
| | 12 | 38 | | _userProvider = userProvider; |
| | 12 | 39 | | _userStore = userStore; |
| | 12 | 40 | | _secretHasher = secretHasher; |
| | 12 | 41 | | _logger = logger ?? NullLogger<DefaultUserCredentialsValidator>.Instance; |
| | 12 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async ValueTask<User?> ValidateAsync(string username, string password, CancellationToken cancellationToken = |
| | | 46 | | { |
| | 9 | 47 | | var user = await _userProvider.FindByNameAsync(username, cancellationToken); |
| | | 48 | | |
| | 9 | 49 | | if (user == null) |
| | 3 | 50 | | return null; |
| | | 51 | | |
| | 6 | 52 | | if (string.IsNullOrEmpty(user.HashedPassword) || string.IsNullOrEmpty(user.HashedPasswordSalt)) |
| | 1 | 53 | | return null; |
| | | 54 | | |
| | 5 | 55 | | var isValidPassword = _secretHasher.VerifySecret(password, user.HashedPassword, user.HashedPasswordSalt, out var |
| | | 56 | | |
| | 5 | 57 | | if (!isValidPassword) |
| | 0 | 58 | | return null; |
| | | 59 | | |
| | 5 | 60 | | if (needsRehash && _userStore != null) |
| | | 61 | | { |
| | 2 | 62 | | var previousHashedPassword = user.HashedPassword; |
| | 2 | 63 | | var previousHashedPasswordSalt = user.HashedPasswordSalt; |
| | 2 | 64 | | var hashedPassword = _secretHasher.HashSecret(password); |
| | 2 | 65 | | user.HashedPassword = hashedPassword.EncodeSecret(); |
| | 2 | 66 | | user.HashedPasswordSalt = hashedPassword.EncodeSalt(); |
| | | 67 | | try |
| | | 68 | | { |
| | 2 | 69 | | await _userStore.SaveAsync(user, cancellationToken); |
| | 1 | 70 | | } |
| | 0 | 71 | | catch (OperationCanceledException) |
| | | 72 | | { |
| | 0 | 73 | | user.HashedPassword = previousHashedPassword; |
| | 0 | 74 | | user.HashedPasswordSalt = previousHashedPasswordSalt; |
| | 0 | 75 | | throw; |
| | | 76 | | } |
| | 1 | 77 | | catch (Exception e) |
| | | 78 | | { |
| | 1 | 79 | | user.HashedPassword = previousHashedPassword; |
| | 1 | 80 | | user.HashedPasswordSalt = previousHashedPasswordSalt; |
| | 1 | 81 | | _logger.LogWarning(e, "Failed to save upgraded password hash for user {UserId}.", user.Id); |
| | 1 | 82 | | } |
| | 2 | 83 | | } |
| | | 84 | | |
| | 5 | 85 | | return user; |
| | 9 | 86 | | } |
| | | 87 | | } |