| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Identity.Services; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | [PublicAPI] |
| | | 10 | | public 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> |
| | 0 | 19 | | public DefaultApplicationCredentialsValidator(IApiKeyParser apiKeyParser, IApplicationProvider applicationProvider, |
| | | 20 | | { |
| | 0 | 21 | | _apiKeyParser = apiKeyParser; |
| | 0 | 22 | | _applicationProvider = applicationProvider; |
| | 0 | 23 | | _secretHasher = secretHasher; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async ValueTask<Application?> ValidateAsync(string apiKey, CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 0 | 29 | | if(string.IsNullOrWhiteSpace(apiKey)) |
| | 0 | 30 | | return null; |
| | | 31 | | |
| | 0 | 32 | | var clientId = _apiKeyParser.Parse(apiKey); |
| | 0 | 33 | | var application = await _applicationProvider.FindByClientIdAsync(clientId, cancellationToken); |
| | | 34 | | |
| | 0 | 35 | | if(application == null) |
| | 0 | 36 | | return null; |
| | | 37 | | |
| | 0 | 38 | | var isValidApiKey = _secretHasher.VerifySecret(apiKey, application.HashedApiKey, application.HashedApiKeySalt); |
| | 0 | 39 | | return isValidApiKey ? application : null; |
| | 0 | 40 | | } |
| | | 41 | | } |