| | | 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 InitiateLocal(IExternalAuthenticationBroker broker, ITenantAccessor tenantAccessor) : ElsaEndpoint |
| | | 13 | | { |
| | | 14 | | public override void Configure() |
| | | 15 | | { |
| | | 16 | | Post("/external-authentication/local/authorize"); |
| | | 17 | | AllowAnonymous(); |
| | | 18 | | Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.LocalInitiation)); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task HandleAsync(InitiateLocalRequest request, CancellationToken cancellationToken) |
| | | 22 | | { |
| | | 23 | | if (!Uri.TryCreate(request.RedirectUri, UriKind.Absolute, out var redirectUri)) |
| | | 24 | | { |
| | | 25 | | await BrokerEndpointSupport.SendErrorAsync(Send, BrokerErrorFactory.Create(BrokerErrorCategory.InvalidReques |
| | | 26 | | return; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | var result = await broker.InitiateLocalAsync(new LocalBrokerAuthorizationRequest( |
| | | 30 | | request.ClientId ?? string.Empty, redirectUri, request.ResponseType ?? string.Empty, request.CodeChallenge ? |
| | | 31 | | request.CodeChallengeMethod ?? string.Empty, request.ReturnPath ?? string.Empty, request.Username ?? string. |
| | | 32 | | request.Password ?? string.Empty, request.State), tenantAccessor.TenantId, cancellationToken); |
| | | 33 | | if (result.Error is { } error) |
| | | 34 | | { |
| | | 35 | | await BrokerEndpointSupport.SendErrorAsync(Send, error, cancellationToken); |
| | | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | await Send.OkAsync(new InitiateLocalResponse(result.RedirectUri!.ToString()), cancellationToken); |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal sealed class InitiateLocalRequest |
| | | 44 | | { |
| | | 45 | | public string? ClientId { get; set; } |
| | | 46 | | public string? RedirectUri { get; set; } |
| | | 47 | | public string? ResponseType { get; set; } |
| | | 48 | | public string? CodeChallenge { get; set; } |
| | | 49 | | public string? CodeChallengeMethod { get; set; } |
| | | 50 | | public string? ReturnPath { get; set; } |
| | | 51 | | public string? State { get; set; } |
| | | 52 | | public string? Username { get; set; } |
| | | 53 | | public string? Password { get; set; } |
| | | 54 | | } |
| | | 55 | | |
| | 2 | 56 | | internal sealed record InitiateLocalResponse(string RedirectUri); |