| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.ExternalAuthentication.Constants; |
| | | 4 | | using Elsa.ExternalAuthentication.Models; |
| | | 5 | | using Elsa.ExternalAuthentication.Services; |
| | | 6 | | using FastEndpoints; |
| | | 7 | | using Microsoft.AspNetCore.Builder; |
| | | 8 | | using Microsoft.AspNetCore.RateLimiting; |
| | | 9 | | |
| | | 10 | | namespace Elsa.ExternalAuthentication.Endpoints.Broker; |
| | | 11 | | |
| | | 12 | | internal sealed class InitiateExternal(IExternalAuthenticationBroker broker, ITenantAccessor tenantAccessor) : ElsaEndpo |
| | | 13 | | { |
| | | 14 | | public override void Configure() |
| | | 15 | | { |
| | | 16 | | Get("/external-authentication/authorize/{connectionKey}"); |
| | | 17 | | AllowAnonymous(); |
| | | 18 | | Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.ExternalInitiation)); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task HandleAsync(InitiateExternalRequest request, CancellationToken cancellationToken) |
| | | 22 | | { |
| | | 23 | | var clientId = Query<string>("client_id", false) ?? request.ClientId; |
| | | 24 | | var redirectUriValue = Query<string>("redirect_uri", false) ?? request.RedirectUri; |
| | | 25 | | var responseType = Query<string>("response_type", false) ?? request.ResponseType; |
| | | 26 | | var codeChallenge = Query<string>("code_challenge", false) ?? request.CodeChallenge; |
| | | 27 | | var codeChallengeMethod = Query<string>("code_challenge_method", false) ?? request.CodeChallengeMethod; |
| | | 28 | | var returnPath = Query<string>("return_path", false) ?? request.ReturnPath; |
| | | 29 | | if (!Uri.TryCreate(redirectUriValue, UriKind.Absolute, out var redirectUri)) |
| | | 30 | | { |
| | | 31 | | await BrokerEndpointSupport.SendErrorAsync(Send, BrokerErrorFactory.Create(BrokerErrorCategory.InvalidReques |
| | | 32 | | return; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | var result = await broker.InitiateExternalAsync(new BrokerAuthorizationRequest( |
| | | 36 | | clientId ?? string.Empty, redirectUri, responseType ?? string.Empty, codeChallenge ?? string.Empty, |
| | | 37 | | codeChallengeMethod ?? string.Empty, returnPath ?? string.Empty, request.ConnectionKey ?? string.Empty, requ |
| | | 38 | | if (result.Error is { } error) |
| | | 39 | | { |
| | | 40 | | await BrokerEndpointSupport.SendErrorAsync(Send, error, cancellationToken); |
| | | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | await Send.RedirectAsync(result.NavigationUri!.ToString(), false, true); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | internal sealed class InitiateExternalRequest |
| | | 49 | | { |
| | 2 | 50 | | public string? ConnectionKey { get; set; } |
| | 0 | 51 | | public string? ClientId { get; set; } |
| | 0 | 52 | | public string? RedirectUri { get; set; } |
| | 0 | 53 | | public string? ResponseType { get; set; } |
| | 0 | 54 | | public string? CodeChallenge { get; set; } |
| | 0 | 55 | | public string? CodeChallengeMethod { get; set; } |
| | 0 | 56 | | public string? ReturnPath { get; set; } |
| | 1 | 57 | | public string? State { get; set; } |
| | | 58 | | } |