< Summary

Information
Class: Elsa.Identity.Services.DefaultUserCredentialsValidator
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultUserCredentialsValidator.cs
Line coverage
86%
Covered lines: 33
Uncovered lines: 5
Coverable lines: 38
Total lines: 87
Line coverage: 86.8%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%22100%
ValidateAsync()91.66%131282.14%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultUserCredentialsValidator.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Entities;
 4using Microsoft.Extensions.Logging;
 5using Microsoft.Extensions.Logging.Abstractions;
 6
 7namespace Elsa.Identity.Services;
 8
 9/// <summary>
 10/// Validates user credentials
 11/// </summary>
 12public 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>
 722    public DefaultUserCredentialsValidator(IUserProvider userProvider, ISecretHasher secretHasher) : this(userProvider, 
 23    {
 724    }
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class.
 28    /// </summary>
 229    public DefaultUserCredentialsValidator(IUserProvider userProvider, IUserStore userStore, ISecretHasher secretHasher)
 30    {
 231    }
 32
 33    /// <summary>
 34    /// Initializes a new instance of the <see cref="DefaultUserCredentialsValidator"/> class.
 35    /// </summary>
 1236    public DefaultUserCredentialsValidator(IUserProvider userProvider, IUserStore? userStore, ISecretHasher secretHasher
 37    {
 1238        _userProvider = userProvider;
 1239        _userStore = userStore;
 1240        _secretHasher = secretHasher;
 1241        _logger = logger ?? NullLogger<DefaultUserCredentialsValidator>.Instance;
 1242    }
 43
 44    /// <inheritdoc />
 45    public async ValueTask<User?> ValidateAsync(string username, string password, CancellationToken cancellationToken = 
 46    {
 947        var user = await _userProvider.FindByNameAsync(username, cancellationToken);
 48
 949        if (user == null)
 350            return null;
 51
 652        if (string.IsNullOrEmpty(user.HashedPassword) || string.IsNullOrEmpty(user.HashedPasswordSalt))
 153            return null;
 54
 555        var isValidPassword = _secretHasher.VerifySecret(password, user.HashedPassword, user.HashedPasswordSalt, out var
 56
 557        if (!isValidPassword)
 058            return null;
 59
 560        if (needsRehash && _userStore != null)
 61        {
 262            var previousHashedPassword = user.HashedPassword;
 263            var previousHashedPasswordSalt = user.HashedPasswordSalt;
 264            var hashedPassword = _secretHasher.HashSecret(password);
 265            user.HashedPassword = hashedPassword.EncodeSecret();
 266            user.HashedPasswordSalt = hashedPassword.EncodeSalt();
 67            try
 68            {
 269                await _userStore.SaveAsync(user, cancellationToken);
 170            }
 071            catch (OperationCanceledException)
 72            {
 073                user.HashedPassword = previousHashedPassword;
 074                user.HashedPasswordSalt = previousHashedPasswordSalt;
 075                throw;
 76            }
 177            catch (Exception e)
 78            {
 179                user.HashedPassword = previousHashedPassword;
 180                user.HashedPasswordSalt = previousHashedPasswordSalt;
 181                _logger.LogWarning(e, "Failed to save upgraded password hash for user {UserId}.", user.Id);
 182            }
 283        }
 84
 585        return user;
 986    }
 87}