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