< 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
39%
Covered lines: 20
Uncovered lines: 31
Coverable lines: 51
Total lines: 87
Line coverage: 39.2%
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.Abstractions;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Entities;
 4using Elsa.Workflows;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Identity.Endpoints.Applications.Create;
 8
 9/// <summary>
 10/// An endpoint that creates a new application. Requires the <code>SecurityRoot</code> policy.
 11/// </summary>
 12[PublicAPI]
 13internal class Create : ElsaEndpoint<Request, Response>
 14{
 15    private readonly IIdentityGenerator _identityGenerator;
 16    private readonly IClientIdGenerator _clientIdGenerator;
 17    private readonly ISecretGenerator _secretGenerator;
 18    private readonly IApiKeyGenerator _apiKeyGenerator;
 19    private readonly ISecretHasher _secretHasher;
 20    private readonly IApplicationStore _applicationStore;
 21    private readonly IRoleStore _roleStore;
 22
 123    public Create(
 124        IIdentityGenerator identityGenerator,
 125        IClientIdGenerator clientIdGenerator,
 126        ISecretGenerator secretGenerator,
 127        IApiKeyGenerator apiKeyGenerator,
 128        ISecretHasher secretHasher,
 129        IApplicationStore applicationStore,
 130        IRoleStore roleStore)
 31    {
 132        _identityGenerator = identityGenerator;
 133        _clientIdGenerator = clientIdGenerator;
 134        _secretGenerator = secretGenerator;
 135        _apiKeyGenerator = apiKeyGenerator;
 136        _secretHasher = secretHasher;
 137        _applicationStore = applicationStore;
 138        _roleStore = roleStore;
 139    }
 40
 41    /// <inheritdoc />
 42    public override void Configure()
 43    {
 144        Post("/identity/applications");
 145        ConfigurePermissions("create:application");
 146        Policies(IdentityPolicyNames.SecurityRoot);
 147    }
 48
 49    /// <inheritdoc />
 50    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 51    {
 052        var id = _identityGenerator.GenerateId();
 053        var clientId = await _clientIdGenerator.GenerateAsync(cancellationToken);
 054        var clientSecret = _secretGenerator.Generate();
 055        var hashedClientSecret = _secretHasher.HashSecret(clientSecret);
 056        var apiKey = _apiKeyGenerator.Generate(clientId);
 057        var hashedApiKey = _secretHasher.HashSecret(apiKey);
 58
 059        var application = new Application
 060        {
 061            Id = id,
 062            ClientId = clientId,
 063            HashedClientSecret = hashedClientSecret.EncodeSecret(),
 064            HashedClientSecretSalt = hashedClientSecret.EncodeSalt(),
 065            Name = request.Name,
 066            HashedApiKey = hashedApiKey.EncodeSecret(),
 067            HashedApiKeySalt = hashedApiKey.EncodeSalt(),
 068            Roles = request.Roles ?? new List<string>()
 069        };
 70
 071        await _applicationStore.SaveAsync(application, cancellationToken);
 72
 073        var response = new Response(
 074            id,
 075            application.Name,
 076            application.Roles,
 077            clientId,
 078            clientSecret,
 079            apiKey,
 080            hashedApiKey.EncodeSecret(),
 081            hashedApiKey.EncodeSalt(),
 082            hashedClientSecret.EncodeSecret(),
 083            hashedClientSecret.EncodeSalt());
 84
 085        await Send.OkAsync(response, cancellationToken);
 086    }
 87}