| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.Services; |
| | | 9 | | |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | [PublicAPI] |
| | | 12 | | public class DefaultApplicationCredentialsValidator : IApplicationCredentialsValidator |
| | | 13 | | { |
| | | 14 | | private readonly IApiKeyParser _apiKeyParser; |
| | | 15 | | private readonly IApplicationProvider _applicationProvider; |
| | | 16 | | private readonly IApplicationStore? _applicationStore; |
| | | 17 | | private readonly ISecretHasher _secretHasher; |
| | | 18 | | private readonly ILogger<DefaultApplicationCredentialsValidator> _logger; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="DefaultApplicationCredentialsValidator"/> class. |
| | | 22 | | /// </summary> |
| | 1 | 23 | | public DefaultApplicationCredentialsValidator(IApiKeyParser apiKeyParser, IApplicationProvider applicationProvider, |
| | | 24 | | { |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="DefaultApplicationCredentialsValidator"/> class. |
| | | 29 | | /// </summary> |
| | 2 | 30 | | public DefaultApplicationCredentialsValidator(IApiKeyParser apiKeyParser, IApplicationProvider applicationProvider, |
| | | 31 | | { |
| | 2 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="DefaultApplicationCredentialsValidator"/> class. |
| | | 36 | | /// </summary> |
| | 3 | 37 | | public DefaultApplicationCredentialsValidator(IApiKeyParser apiKeyParser, IApplicationProvider applicationProvider, |
| | | 38 | | { |
| | 3 | 39 | | _apiKeyParser = apiKeyParser; |
| | 3 | 40 | | _applicationProvider = applicationProvider; |
| | 3 | 41 | | _applicationStore = applicationStore; |
| | 3 | 42 | | _secretHasher = secretHasher; |
| | 3 | 43 | | _logger = logger ?? NullLogger<DefaultApplicationCredentialsValidator>.Instance; |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async ValueTask<Application?> ValidateAsync(string apiKey, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 3 | 49 | | if(string.IsNullOrWhiteSpace(apiKey)) |
| | 0 | 50 | | return null; |
| | | 51 | | |
| | 3 | 52 | | var clientId = _apiKeyParser.Parse(apiKey); |
| | 3 | 53 | | var application = await _applicationProvider.FindByClientIdAsync(clientId, cancellationToken); |
| | | 54 | | |
| | 3 | 55 | | if(application == null) |
| | 0 | 56 | | return null; |
| | | 57 | | |
| | 3 | 58 | | var isValidApiKey = _secretHasher.VerifySecret(apiKey, application.HashedApiKey, application.HashedApiKeySalt, o |
| | | 59 | | |
| | 3 | 60 | | if (!isValidApiKey) |
| | 0 | 61 | | return null; |
| | | 62 | | |
| | 3 | 63 | | if (needsRehash && _applicationStore != null) |
| | | 64 | | { |
| | 2 | 65 | | var previousHashedApiKey = application.HashedApiKey; |
| | 2 | 66 | | var previousHashedApiKeySalt = application.HashedApiKeySalt; |
| | 2 | 67 | | var hashedApiKey = _secretHasher.HashSecret(apiKey); |
| | 2 | 68 | | application.HashedApiKey = hashedApiKey.EncodeSecret(); |
| | 2 | 69 | | application.HashedApiKeySalt = hashedApiKey.EncodeSalt(); |
| | | 70 | | try |
| | | 71 | | { |
| | 2 | 72 | | await _applicationStore.SaveAsync(application, cancellationToken); |
| | 1 | 73 | | } |
| | 0 | 74 | | catch (OperationCanceledException) |
| | | 75 | | { |
| | 0 | 76 | | application.HashedApiKey = previousHashedApiKey; |
| | 0 | 77 | | application.HashedApiKeySalt = previousHashedApiKeySalt; |
| | 0 | 78 | | throw; |
| | | 79 | | } |
| | 1 | 80 | | catch (Exception e) |
| | | 81 | | { |
| | 1 | 82 | | application.HashedApiKey = previousHashedApiKey; |
| | 1 | 83 | | application.HashedApiKeySalt = previousHashedApiKeySalt; |
| | 1 | 84 | | _logger.LogWarning(e, "Failed to save upgraded API key hash for application {ApplicationId}.", applicati |
| | 1 | 85 | | } |
| | 2 | 86 | | } |
| | | 87 | | |
| | 3 | 88 | | return application; |
| | 3 | 89 | | } |
| | | 90 | | } |