< Summary

Information
Class: Elsa.ExternalAuthentication.Endpoints.Broker.ExchangeToken
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Endpoints/Broker/ExchangeToken.cs
Line coverage
62%
Covered lines: 15
Uncovered lines: 9
Coverable lines: 24
Total lines: 65
Line coverage: 62.5%
Branch coverage
55%
Covered branches: 11
Total branches: 20
Branch coverage: 55%
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()64.28%231463.63%
TryGetBasicCredentials(...)33.33%19628.57%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Endpoints/Broker/ExchangeToken.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.ExternalAuthentication.Constants;
 3using Elsa.ExternalAuthentication.Models;
 4using Elsa.ExternalAuthentication.Services;
 5using FastEndpoints;
 6using Microsoft.AspNetCore.Builder;
 7using Microsoft.AspNetCore.RateLimiting;
 8
 9namespace Elsa.ExternalAuthentication.Endpoints.Broker;
 10
 711internal sealed class ExchangeToken(IExternalAuthenticationBroker broker) : ElsaEndpoint<ExchangeTokenRequest>
 12{
 13    public override void Configure()
 14    {
 615        Post("/external-authentication/token");
 616        AllowAnonymous();
 617        AllowFormData(true);
 1218        Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.TokenExchange));
 619    }
 20
 21    public override async Task HandleAsync(ExchangeTokenRequest request, CancellationToken cancellationToken)
 22    {
 123        Uri? redirectUri = null;
 124        if (!string.IsNullOrWhiteSpace(request.RedirectUri) && !Uri.TryCreate(request.RedirectUri, UriKind.Absolute, out
 25        {
 026            await BrokerEndpointSupport.SendErrorAsync(Send, BrokerErrorFactory.Create(BrokerErrorCategory.InvalidReques
 027            return;
 28        }
 29
 130        var basicCredentials = TryGetBasicCredentials(HttpContext.Request.Headers.Authorization);
 131        var result = await broker.ExchangeAsync(new BrokerTokenRequest(request.GrantType ?? string.Empty, request.Client
 132        if (result.Error is { } error)
 33        {
 034            await BrokerEndpointSupport.SendErrorAsync(Send, error, cancellationToken);
 035            return;
 36        }
 37
 138        await Send.OkAsync(result.Token!, cancellationToken);
 139    }
 40
 41    private static (string ClientId, string Secret)? TryGetBasicCredentials(string? authorization)
 42    {
 143        if (string.IsNullOrWhiteSpace(authorization) || !authorization.StartsWith("Basic ", StringComparison.OrdinalIgno
 144            return null;
 45        try
 46        {
 047            var parts = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(authorization[6..])).Split(':', 2);
 048            return parts.Length == 2 ? (parts[0], parts[1]) : null;
 49        }
 050        catch (FormatException)
 51        {
 052            return null;
 53        }
 054    }
 55}
 56
 57internal sealed class ExchangeTokenRequest
 58{
 59    public string? GrantType { get; set; }
 60    public string? ClientId { get; set; }
 61    public string? RedirectUri { get; set; }
 62    public string? Code { get; set; }
 63    public string? CodeVerifier { get; set; }
 64    public string? RefreshToken { get; set; }
 65}