< Summary

Information
Class: Elsa.Requirements.LocalHostRequirementHandler
Assembly: Elsa.Api.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Requirements/LocalHostRequirement.cs
Line coverage
42%
Covered lines: 3
Uncovered lines: 4
Coverable lines: 7
Total lines: 36
Line coverage: 42.8%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%2040%

File(s)

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

#LineLine coverage
 1using Elsa.Extensions;
 2using JetBrains.Annotations;
 3using Microsoft.AspNetCore.Authorization;
 4using Microsoft.AspNetCore.Http;
 5
 6namespace Elsa.Requirements;
 7
 8/// <summary>
 9/// Represents an authorization requirement for localhost requests, meaning that if a request comes from the localhost, 
 10/// </summary>
 11public class LocalHostRequirement : IAuthorizationRequirement
 12{
 13}
 14
 15/// <inheritdoc />
 16[PublicAPI]
 17public class LocalHostRequirementHandler : AuthorizationHandler<LocalHostRequirement>
 18{
 19    private readonly IHttpContextAccessor _httpContextAccessor;
 20
 21    /// <inheritdoc />
 23422    public LocalHostRequirementHandler(IHttpContextAccessor httpContextAccessor)
 23    {
 23424        _httpContextAccessor = httpContextAccessor;
 23425    }
 26
 27    /// <inheritdoc />
 28    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, LocalHostRequirement requirement
 29    {
 030        if (_httpContextAccessor.HttpContext?.Request.IsLocal() == false)
 031            context.Fail(new AuthorizationFailureReason(this, "Only requests from localhost are allowed"));
 32
 033        context.Succeed(requirement);
 034        return Task.CompletedTask;
 35    }
 36}