| | | 1 | | using System.Security.Claims; |
| | | 2 | | using AspNetCore.Authentication.ApiKey; |
| | | 3 | | using Elsa.Identity.Models; |
| | | 4 | | using Elsa.Identity.Options; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Providers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides an <see cref="IApiKey"/> with admin privileges for an explicitly configured admin API key. |
| | | 11 | | /// </summary> |
| | 299 | 12 | | public class AdminApiKeyProvider(IOptions<AdminApiKeyOptions> options) : IApiKeyProvider |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="AdminApiKeyProvider"/> class with no accepted API key. |
| | | 16 | | /// </summary> |
| | | 17 | | [Obsolete("Use the options-based constructor. The built-in admin API key is disabled unless explicitly configured.") |
| | 0 | 18 | | public AdminApiKeyProvider() : this(Microsoft.Extensions.Options.Options.Create(new AdminApiKeyOptions())) |
| | | 19 | | { |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The all-zero development admin API key. Do not enable in production. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static readonly string DevelopmentApiKey = Guid.Empty.ToString(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The legacy development admin API key. |
| | | 29 | | /// </summary> |
| | | 30 | | [Obsolete("Use DevelopmentApiKey. The built-in admin API key is disabled unless explicitly configured.")] |
| | 2 | 31 | | public static readonly string DefaultApiKey = DevelopmentApiKey; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public Task<IApiKey?> ProvideAsync(string key) |
| | | 35 | | { |
| | 299 | 36 | | var apiKeyOptions = options.Value; |
| | 299 | 37 | | if (string.IsNullOrWhiteSpace(apiKeyOptions.ApiKey) || key != apiKeyOptions.ApiKey) |
| | 1 | 38 | | return Task.FromResult<IApiKey?>(null); |
| | | 39 | | |
| | 596 | 40 | | var claims = apiKeyOptions.Permissions.Select(permission => new Claim("permissions", permission)).ToList(); |
| | 298 | 41 | | var apiKey = new ApiKey(key, apiKeyOptions.OwnerName, claims); |
| | 298 | 42 | | return Task.FromResult<IApiKey>(apiKey)!; |
| | | 43 | | } |
| | | 44 | | } |