| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.ExternalAuthentication.Constants; |
| | | 3 | | using Elsa.ExternalAuthentication.Models; |
| | | 4 | | using Elsa.ExternalAuthentication.Services; |
| | | 5 | | using FastEndpoints; |
| | | 6 | | using Microsoft.AspNetCore.Builder; |
| | | 7 | | using Microsoft.AspNetCore.RateLimiting; |
| | | 8 | | |
| | | 9 | | namespace Elsa.ExternalAuthentication.Endpoints.Broker; |
| | | 10 | | |
| | | 11 | | internal 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 | | |
| | | 57 | | internal sealed class ExchangeTokenRequest |
| | | 58 | | { |
| | 1 | 59 | | public string? GrantType { get; set; } |
| | 1 | 60 | | public string? ClientId { get; set; } |
| | 1 | 61 | | public string? RedirectUri { get; set; } |
| | 2 | 62 | | public string? Code { get; set; } |
| | 1 | 63 | | public string? CodeVerifier { get; set; } |
| | 1 | 64 | | public string? RefreshToken { get; set; } |
| | | 65 | | } |