| | | 1 | | using Elsa.Identity.Contracts; |
| | | 2 | | using Elsa.Identity.Entities; |
| | | 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 | | /// Represents a user provider that returns a single explicitly configured admin user. This is useful for development pu |
| | | 11 | | /// </summary> |
| | | 12 | | public class AdminUserProvider : IUserProvider |
| | | 13 | | { |
| | | 14 | | private readonly User? _adminUser; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="AdminUserProvider"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | [Obsolete("Use the options-based constructor. The built-in admin user is disabled unless explicitly configured.")] |
| | 0 | 20 | | public AdminUserProvider(ISecretHasher secretHasher) : this(secretHasher, Microsoft.Extensions.Options.Options.Creat |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="AdminUserProvider"/> class. |
| | | 26 | | /// </summary> |
| | 4 | 27 | | public AdminUserProvider(ISecretHasher secretHasher, IOptions<AdminUserProviderOptions> options) |
| | | 28 | | { |
| | 4 | 29 | | var providerOptions = options.Value; |
| | 4 | 30 | | if (string.IsNullOrWhiteSpace(providerOptions.UserName) || string.IsNullOrWhiteSpace(providerOptions.Password)) |
| | 2 | 31 | | return; |
| | | 32 | | |
| | 2 | 33 | | var hashedSecret = secretHasher.HashSecret(providerOptions.Password); |
| | | 34 | | |
| | 2 | 35 | | _adminUser = new User |
| | 2 | 36 | | { |
| | 2 | 37 | | Id = providerOptions.UserId, |
| | 2 | 38 | | Name = providerOptions.UserName, |
| | 2 | 39 | | HashedPassword = hashedSecret.EncodeSecret(), |
| | 2 | 40 | | HashedPasswordSalt = hashedSecret.EncodeSalt(), |
| | 2 | 41 | | Roles = providerOptions.Roles.ToList() |
| | 2 | 42 | | }; |
| | 2 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task<User?> FindAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 4 | 48 | | if (_adminUser == null) |
| | 2 | 49 | | return Task.FromResult<User?>(null); |
| | | 50 | | |
| | 2 | 51 | | if (filter.Id != null && filter.Id != _adminUser.Id) |
| | 0 | 52 | | return Task.FromResult<User?>(null); |
| | | 53 | | |
| | 2 | 54 | | if (filter.Name != null && filter.Name != _adminUser.Name) |
| | 1 | 55 | | return Task.FromResult<User?>(null); |
| | | 56 | | |
| | 1 | 57 | | return Task.FromResult<User?>(_adminUser); |
| | | 58 | | } |
| | | 59 | | } |