< Summary

Information
Class: Elsa.Requirements.LocalHostPermissionRequirementHandler
Assembly: Elsa.Api.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Requirements/LocalHostPermissionRequirement.cs
Line coverage
21%
Covered lines: 3
Uncovered lines: 11
Coverable lines: 14
Total lines: 49
Line coverage: 21.4%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%
HandleRequirementAsync(...)0%7280%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Requirements/LocalHostPermissionRequirement.cs

#LineLine coverage
 1using System.Security.Claims;
 2using Elsa.Extensions;
 3using JetBrains.Annotations;
 4using Microsoft.AspNetCore.Authentication.JwtBearer;
 5using Microsoft.AspNetCore.Authorization;
 6using Microsoft.AspNetCore.Http;
 7
 8namespace Elsa.Requirements;
 9
 10/// <summary>
 11/// Add the "create:application" permission to the current user if the request is local.
 12/// </summary>
 13public class LocalHostPermissionRequirement : IAuthorizationRequirement
 14{
 15}
 16
 17/// <inheritdoc />
 18[PublicAPI]
 19public class LocalHostPermissionRequirementHandler : AuthorizationHandler<LocalHostPermissionRequirement>
 20{
 21    private readonly IHttpContextAccessor _httpContextAccessor;
 22
 23    /// <inheritdoc />
 23424    public LocalHostPermissionRequirementHandler(IHttpContextAccessor httpContextAccessor)
 25    {
 23426        _httpContextAccessor = httpContextAccessor;
 23427    }
 28
 29    /// <inheritdoc />
 30    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalHostPermissionRequirement r
 31    {
 032        if (_httpContextAccessor.HttpContext?.Request.IsLocal() == false)
 033            return Task.CompletedTask;
 34
 035        var currentIdentity = context.User.Identity;
 36
 037        if (currentIdentity?.IsAuthenticated == false)
 38        {
 039            var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
 040            identity.AddClaim(new Claim("permissions", "create:application"));
 041            identity.AddClaim(new Claim("permissions", "create:user"));
 042            identity.AddClaim(new Claim("permissions", "create:role"));
 043            context.User.AddIdentity(identity);
 44        }
 45
 046        context.Succeed(requirement);
 047        return Task.CompletedTask;
 48    }
 49}