| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Endpoints.Users.Create; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An endpoint that creates a new user. Requires the <code>SecurityRoot</code> policy. |
| | | 11 | | /// </summary> |
| | | 12 | | [PublicAPI] |
| | | 13 | | internal class Create : ElsaEndpoint<Request, Response> |
| | | 14 | | { |
| | | 15 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 16 | | private readonly ISecretGenerator _secretGenerator; |
| | | 17 | | private readonly ISecretHasher _secretHasher; |
| | | 18 | | private readonly IUserStore _userStore; |
| | | 19 | | private readonly IRoleStore _roleStore; |
| | | 20 | | |
| | 1 | 21 | | public Create( |
| | 1 | 22 | | IIdentityGenerator identityGenerator, |
| | 1 | 23 | | ISecretGenerator secretGenerator, |
| | 1 | 24 | | ISecretHasher secretHasher, |
| | 1 | 25 | | IUserStore userStore, |
| | 1 | 26 | | IRoleStore roleStore) |
| | | 27 | | { |
| | 1 | 28 | | _identityGenerator = identityGenerator; |
| | 1 | 29 | | _secretGenerator = secretGenerator; |
| | 1 | 30 | | _secretHasher = secretHasher; |
| | 1 | 31 | | _userStore = userStore; |
| | 1 | 32 | | _roleStore = roleStore; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public override void Configure() |
| | | 37 | | { |
| | 1 | 38 | | Post("/identity/users"); |
| | 1 | 39 | | ConfigurePermissions("create:user"); |
| | 1 | 40 | | Policies(IdentityPolicyNames.SecurityRoot); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 45 | | { |
| | 0 | 46 | | var id = _identityGenerator.GenerateId(); |
| | 0 | 47 | | var password = string.IsNullOrWhiteSpace(request.Password) ? _secretGenerator.Generate() : request.Password.Trim |
| | 0 | 48 | | var hashedPassword = _secretHasher.HashSecret(password); |
| | | 49 | | |
| | 0 | 50 | | var user = new User |
| | 0 | 51 | | { |
| | 0 | 52 | | Id = id, |
| | 0 | 53 | | Name = request.Name, |
| | 0 | 54 | | Roles = request.Roles ?? new List<string>(), |
| | 0 | 55 | | HashedPassword = hashedPassword.EncodeSecret(), |
| | 0 | 56 | | HashedPasswordSalt = hashedPassword.EncodeSalt() |
| | 0 | 57 | | }; |
| | | 58 | | |
| | 0 | 59 | | await _userStore.SaveAsync(user, cancellationToken); |
| | | 60 | | |
| | 0 | 61 | | var response = new Response( |
| | 0 | 62 | | id, |
| | 0 | 63 | | user.Name, |
| | 0 | 64 | | password, |
| | 0 | 65 | | user.Roles, |
| | 0 | 66 | | user.TenantId, |
| | 0 | 67 | | hashedPassword.EncodeSecret(), |
| | 0 | 68 | | hashedPassword.EncodeSalt()); |
| | | 69 | | |
| | 0 | 70 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 71 | | } |
| | | 72 | | } |