| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Identity.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Validates user credentials |
| | | 9 | | /// </summary> |
| | | 10 | | public class DefaultUserCredentialsValidator : IUserCredentialsValidator |
| | | 11 | | { |
| | | 12 | | private readonly IUserProvider _userProvider; |
| | | 13 | | private readonly ISecretHasher _secretHasher; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class. |
| | | 17 | | /// </summary> |
| | 1 | 18 | | public DefaultUserCredentialsValidator(IUserProvider userProvider, ISecretHasher secretHasher) |
| | | 19 | | { |
| | 1 | 20 | | _userProvider = userProvider; |
| | 1 | 21 | | _secretHasher = secretHasher; |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask<User?> ValidateAsync(string username, string password, CancellationToken cancellationToken = |
| | | 26 | | { |
| | 0 | 27 | | var user = await _userProvider.FindByNameAsync(username, cancellationToken); |
| | | 28 | | |
| | 0 | 29 | | if (user == null) |
| | 0 | 30 | | return null; |
| | | 31 | | |
| | 0 | 32 | | var isValidPassword = _secretHasher.VerifySecret(password, user.HashedPassword, user.HashedPasswordSalt); |
| | | 33 | | |
| | 0 | 34 | | return isValidPassword ? user : null; |
| | 0 | 35 | | } |
| | | 36 | | } |