< Summary

Information
Class: Elsa.ExternalAuthentication.Endpoints.Broker.ExchangeTokenRequest
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Endpoints/Broker/ExchangeToken.cs
Line coverage
100%
Covered lines: 6
Uncovered lines: 0
Coverable lines: 6
Total lines: 63
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_GrantType()100%11100%
get_ClientId()100%11100%
get_RedirectUri()100%11100%
get_Code()100%11100%
get_CodeVerifier()100%11100%
get_RefreshToken()100%11100%

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
 9internal sealed class ExchangeToken(IExternalAuthenticationBroker broker) : ElsaEndpoint<ExchangeTokenRequest>
 10{
 11    public override void Configure()
 12    {
 13        Post("/external-authentication/token");
 14        AllowAnonymous();
 15        AllowFormData(true);
 16        Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.TokenExchange));
 17    }
 18
 19    public override async Task HandleAsync(ExchangeTokenRequest request, CancellationToken cancellationToken)
 20    {
 21        Uri? redirectUri = null;
 22        if (!string.IsNullOrWhiteSpace(request.RedirectUri) && !Uri.TryCreate(request.RedirectUri, UriKind.Absolute, out
 23        {
 24            await BrokerEndpointSupport.SendErrorAsync(Send, BrokerErrorFactory.Create(BrokerErrorCategory.InvalidReques
 25            return;
 26        }
 27
 28        var basicCredentials = TryGetBasicCredentials(HttpContext.Request.Headers.Authorization);
 29        var result = await broker.ExchangeAsync(new(request.GrantType ?? string.Empty, request.ClientId ?? string.Empty,
 30        if (result.Error is { } error)
 31        {
 32            await BrokerEndpointSupport.SendErrorAsync(Send, error, cancellationToken);
 33            return;
 34        }
 35
 36        await Send.OkAsync(result.Token!, cancellationToken);
 37    }
 38
 39    private static (string ClientId, string Secret)? TryGetBasicCredentials(string? authorization)
 40    {
 41        if (string.IsNullOrWhiteSpace(authorization) || !authorization.StartsWith("Basic ", StringComparison.OrdinalIgno
 42            return null;
 43        try
 44        {
 45            var parts = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(authorization[6..])).Split(':', 2);
 46            return parts.Length == 2 ? (parts[0], parts[1]) : null;
 47        }
 48        catch (FormatException)
 49        {
 50            return null;
 51        }
 52    }
 53}
 54
 55internal sealed class ExchangeTokenRequest
 56{
 157    public string? GrantType { get; set; }
 158    public string? ClientId { get; set; }
 159    public string? RedirectUri { get; set; }
 260    public string? Code { get; set; }
 161    public string? CodeVerifier { get; set; }
 162    public string? RefreshToken { get; set; }
 63}