< Summary

Information
Class: Elsa.Http.Handlers.AuthenticationBasedHttpEndpointAuthorizationHandler
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/AuthenticationBasedHttpEndpointAuthorizationHandler.cs
Line coverage
5%
Covered lines: 1
Uncovered lines: 16
Coverable lines: 17
Total lines: 44
Line coverage: 5.8%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%
AuthorizeAsync()0%4260%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Handlers/AuthenticationBasedHttpEndpointAuthorizationHandler.cs

#LineLine coverage
 1using JetBrains.Annotations;
 2using Microsoft.AspNetCore.Authorization;
 3
 4namespace Elsa.Http.Handlers;
 5
 6/// <summary>
 7/// An <see cref="IHttpEndpointAuthorizationHandler"/> that uses the <see cref="IAuthorizationService"/> to authorize an
 8/// </summary>
 9[PublicAPI]
 10public class AuthenticationBasedHttpEndpointAuthorizationHandler : IHttpEndpointAuthorizationHandler
 11{
 12    private readonly IAuthorizationService _authorizationService;
 13
 14    /// <summary>
 15    /// Initializes a new instance of the <see cref="AuthenticationBasedHttpEndpointAuthorizationHandler"/> class.
 16    /// </summary>
 42817    public AuthenticationBasedHttpEndpointAuthorizationHandler(IAuthorizationService authorizationService) => _authoriza
 18
 19    /// <inheritdoc />
 20    public async ValueTask<bool> AuthorizeAsync(AuthorizeHttpEndpointContext context)
 21    {
 022        var httpContext = context.HttpContext;
 023        var user = httpContext.User;
 024        var identity = user.Identity;
 25
 026        if (identity == null)
 027            return false;
 28
 029        if (identity.IsAuthenticated == false)
 030            return false;
 31
 032        if (string.IsNullOrWhiteSpace(context.Policy))
 033            return identity.IsAuthenticated;
 34
 035        var protectedResource = new
 036        {
 037            context.Workflow
 038        };
 39
 040        var authorizationResult = await _authorizationService.AuthorizeAsync(user, protectedResource, context.Policy!);
 41
 042        return authorizationResult.Succeeded;
 043    }
 44}