< Summary

Information
Class: Elsa.Identity.Endpoints.Applications.Create.Create
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Applications/Create/Endpoint.cs
Line coverage
27%
Covered lines: 12
Uncovered lines: 31
Coverable lines: 43
Total lines: 71
Line coverage: 27.9%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Applications/Create/Endpoint.cs

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Identity.Contracts;
 4using Elsa.Identity.Entities;
 5using Elsa.Workflows;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Identity.Endpoints.Applications.Create;
 9
 10/// <summary>
 11/// An endpoint that creates a new application.
 12/// </summary>
 13[PublicAPI]
 314internal class Create(
 315    IIdentityGenerator identityGenerator,
 316    IClientIdGenerator clientIdGenerator,
 317    ISecretGenerator secretGenerator,
 318    IApiKeyGenerator apiKeyGenerator,
 319    ISecretHasher secretHasher,
 320    IApplicationStore applicationStore,
 321    IRoleStore roleStore)
 22    : ElsaEndpoint<Request, Response>
 23{
 324    private readonly IRoleStore _roleStore = roleStore;
 25
 26    /// <inheritdoc />
 27    public override void Configure()
 28    {
 529        Post("/identity/applications");
 530        RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Applications, CoreVerbs.Create);
 531    }
 32
 33    /// <inheritdoc />
 34    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 35    {
 036        var id = identityGenerator.GenerateId();
 037        var clientId = await clientIdGenerator.GenerateAsync(cancellationToken);
 038        var clientSecret = secretGenerator.Generate();
 039        var hashedClientSecret = secretHasher.HashSecret(clientSecret);
 040        var apiKey = apiKeyGenerator.Generate(clientId);
 041        var hashedApiKey = secretHasher.HashSecret(apiKey);
 42
 043        var application = new Application
 044        {
 045            Id = id,
 046            ClientId = clientId,
 047            HashedClientSecret = hashedClientSecret.EncodeSecret(),
 048            HashedClientSecretSalt = hashedClientSecret.EncodeSalt(),
 049            Name = request.Name,
 050            HashedApiKey = hashedApiKey.EncodeSecret(),
 051            HashedApiKeySalt = hashedApiKey.EncodeSalt(),
 052            Roles = request.Roles ?? new List<string>()
 053        };
 54
 055        await applicationStore.SaveAsync(application, cancellationToken);
 56
 057        var response = new Response(
 058            id,
 059            application.Name,
 060            application.Roles,
 061            clientId,
 062            clientSecret,
 063            apiKey,
 064            hashedApiKey.EncodeSecret(),
 065            hashedApiKey.EncodeSalt(),
 066            hashedClientSecret.EncodeSecret(),
 067            hashedClientSecret.EncodeSalt());
 68
 069        await Send.OkAsync(response, cancellationToken);
 070    }
 71}