| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Authorization; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Identity.Endpoints.Secrets.Hash; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Hash a user password, returning the encoded hash and salt the user store would otherwise have written. |
| | | 10 | | /// Requires <c>identity/users:create</c>. Scoped to user credentials only: application credentials are not |
| | | 11 | | /// hashed here. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// This endpoint previously carried only the <c>SecurityRoot</c> policy, which by default resolved to nothing |
| | | 15 | | /// more than "any authenticated caller" -- so any signed-in user could exercise the password hasher. It is |
| | | 16 | | /// declared against identity/users:create because the credential it prepares is a user password: the caller |
| | | 17 | | /// seeding a user out of band needs the same hash the user store would have written. |
| | | 18 | | /// |
| | | 19 | | /// No application-provisioning flow reaches this endpoint, and none is documented to. |
| | | 20 | | /// <c>POST /identity/applications</c> generates the client secret and the API key itself, hashes both, and |
| | | 21 | | /// returns each plaintext alongside its hash, so <c>identity/applications:create</c> on its own stays |
| | | 22 | | /// sufficient to create an application end to end. See ADR 0010. |
| | | 23 | | /// </remarks> |
| | | 24 | | [PublicAPI] |
| | 3 | 25 | | internal class Hash(ISecretHasher secretHasher) : ElsaEndpoint<Request, Response> |
| | | 26 | | { |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public override void Configure() |
| | | 29 | | { |
| | 5 | 30 | | Post("/identity/secrets/hash"); |
| | 5 | 31 | | RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Users, CoreVerbs.Create); |
| | 5 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public override Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken) |
| | | 36 | | { |
| | 0 | 37 | | var hashedPassword = secretHasher.HashSecret(request.Secret); |
| | 0 | 38 | | var response = new Response(hashedPassword.EncodeSecret(), hashedPassword.EncodeSalt()); |
| | | 39 | | |
| | 0 | 40 | | return Task.FromResult(response); |
| | | 41 | | } |
| | | 42 | | } |