< Summary

Information
Class: Elsa.Identity.Services.DefaultApplicationCredentialsValidator
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultApplicationCredentialsValidator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 41
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ValidateAsync()0%4260%

File(s)

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

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Entities;
 4using JetBrains.Annotations;
 5
 6namespace Elsa.Identity.Services;
 7
 8/// <inheritdoc />
 9[PublicAPI]
 10public class DefaultApplicationCredentialsValidator : IApplicationCredentialsValidator
 11{
 12    private readonly IApiKeyParser _apiKeyParser;
 13    private readonly IApplicationProvider _applicationProvider;
 14    private readonly ISecretHasher _secretHasher;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="DefaultApplicationCredentialsValidator"/> class.
 18    /// </summary>
 019    public DefaultApplicationCredentialsValidator(IApiKeyParser apiKeyParser, IApplicationProvider applicationProvider, 
 20    {
 021        _apiKeyParser = apiKeyParser;
 022        _applicationProvider = applicationProvider;
 023        _secretHasher = secretHasher;
 024    }
 25
 26    /// <inheritdoc />
 27    public async ValueTask<Application?> ValidateAsync(string apiKey, CancellationToken cancellationToken = default)
 28    {
 029        if(string.IsNullOrWhiteSpace(apiKey))
 030            return null;
 31
 032        var clientId = _apiKeyParser.Parse(apiKey);
 033        var application = await _applicationProvider.FindByClientIdAsync(clientId, cancellationToken);
 34
 035        if(application == null)
 036            return null;
 37
 038        var isValidApiKey = _secretHasher.VerifySecret(apiKey, application.HashedApiKey, application.HashedApiKeySalt);
 039        return isValidApiKey ? application : null;
 040    }
 41}