| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using Elsa.Identity.Entities; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.Endpoints.Applications.Create; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// An endpoint that creates a new application. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | 3 | 14 | | internal class Create( |
| | 3 | 15 | | IIdentityGenerator identityGenerator, |
| | 3 | 16 | | IClientIdGenerator clientIdGenerator, |
| | 3 | 17 | | ISecretGenerator secretGenerator, |
| | 3 | 18 | | IApiKeyGenerator apiKeyGenerator, |
| | 3 | 19 | | ISecretHasher secretHasher, |
| | 3 | 20 | | IApplicationStore applicationStore, |
| | 3 | 21 | | IRoleStore roleStore) |
| | | 22 | | : ElsaEndpoint<Request, Response> |
| | | 23 | | { |
| | 3 | 24 | | private readonly IRoleStore _roleStore = roleStore; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public override void Configure() |
| | | 28 | | { |
| | 5 | 29 | | Post("/identity/applications"); |
| | 5 | 30 | | RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Applications, CoreVerbs.Create); |
| | 5 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 35 | | { |
| | 0 | 36 | | var id = identityGenerator.GenerateId(); |
| | 0 | 37 | | var clientId = await clientIdGenerator.GenerateAsync(cancellationToken); |
| | 0 | 38 | | var clientSecret = secretGenerator.Generate(); |
| | 0 | 39 | | var hashedClientSecret = secretHasher.HashSecret(clientSecret); |
| | 0 | 40 | | var apiKey = apiKeyGenerator.Generate(clientId); |
| | 0 | 41 | | var hashedApiKey = secretHasher.HashSecret(apiKey); |
| | | 42 | | |
| | 0 | 43 | | var application = new Application |
| | 0 | 44 | | { |
| | 0 | 45 | | Id = id, |
| | 0 | 46 | | ClientId = clientId, |
| | 0 | 47 | | HashedClientSecret = hashedClientSecret.EncodeSecret(), |
| | 0 | 48 | | HashedClientSecretSalt = hashedClientSecret.EncodeSalt(), |
| | 0 | 49 | | Name = request.Name, |
| | 0 | 50 | | HashedApiKey = hashedApiKey.EncodeSecret(), |
| | 0 | 51 | | HashedApiKeySalt = hashedApiKey.EncodeSalt(), |
| | 0 | 52 | | Roles = request.Roles ?? new List<string>() |
| | 0 | 53 | | }; |
| | | 54 | | |
| | 0 | 55 | | await applicationStore.SaveAsync(application, cancellationToken); |
| | | 56 | | |
| | 0 | 57 | | var response = new Response( |
| | 0 | 58 | | id, |
| | 0 | 59 | | application.Name, |
| | 0 | 60 | | application.Roles, |
| | 0 | 61 | | clientId, |
| | 0 | 62 | | clientSecret, |
| | 0 | 63 | | apiKey, |
| | 0 | 64 | | hashedApiKey.EncodeSecret(), |
| | 0 | 65 | | hashedApiKey.EncodeSalt(), |
| | 0 | 66 | | hashedClientSecret.EncodeSecret(), |
| | 0 | 67 | | hashedClientSecret.EncodeSalt()); |
| | | 68 | | |
| | 0 | 69 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 70 | | } |
| | | 71 | | } |