< 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: 63
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 Microsoft.AspNetCore.Builder;
 6
 7namespace Elsa.ExternalAuthentication.Endpoints.Broker;
 8
 109internal sealed class ExchangeToken(IExternalAuthenticationBroker broker) : ElsaEndpoint<ExchangeTokenRequest>
 10{
 11    public override void Configure()
 12    {
 913        Post("/external-authentication/token");
 914        AllowAnonymous();
 915        AllowFormData(true);
 1816        Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.TokenExchange));
 917    }
 18
 19    public override async Task HandleAsync(ExchangeTokenRequest request, CancellationToken cancellationToken)
 20    {
 121        Uri? redirectUri = null;
 122        if (!string.IsNullOrWhiteSpace(request.RedirectUri) && !Uri.TryCreate(request.RedirectUri, UriKind.Absolute, out
 23        {
 024            await BrokerEndpointSupport.SendErrorAsync(Send, BrokerErrorFactory.Create(BrokerErrorCategory.InvalidReques
 025            return;
 26        }
 27
 128        var basicCredentials = TryGetBasicCredentials(HttpContext.Request.Headers.Authorization);
 129        var result = await broker.ExchangeAsync(new(request.GrantType ?? string.Empty, request.ClientId ?? string.Empty,
 130        if (result.Error is { } error)
 31        {
 032            await BrokerEndpointSupport.SendErrorAsync(Send, error, cancellationToken);
 033            return;
 34        }
 35
 136        await Send.OkAsync(result.Token!, cancellationToken);
 137    }
 38
 39    private static (string ClientId, string Secret)? TryGetBasicCredentials(string? authorization)
 40    {
 141        if (string.IsNullOrWhiteSpace(authorization) || !authorization.StartsWith("Basic ", StringComparison.OrdinalIgno
 142            return null;
 43        try
 44        {
 045            var parts = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(authorization[6..])).Split(':', 2);
 046            return parts.Length == 2 ? (parts[0], parts[1]) : null;
 47        }
 048        catch (FormatException)
 49        {
 050            return null;
 51        }
 052    }
 53}
 54
 55internal sealed class ExchangeTokenRequest
 56{
 57    public string? GrantType { get; set; }
 58    public string? ClientId { get; set; }
 59    public string? RedirectUri { get; set; }
 60    public string? Code { get; set; }
 61    public string? CodeVerifier { get; set; }
 62    public string? RefreshToken { get; set; }
 63}