| | | 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 | | |
| | 7 | 12 | | internal sealed class DiscoverLoginMethods(IExternalAuthenticationBroker broker, ITenantAccessor tenantAccessor) : ElsaE |
| | | 13 | | { |
| | | 14 | | public override void Configure() |
| | | 15 | | { |
| | 6 | 16 | | Get("/external-authentication/login-methods"); |
| | 6 | 17 | | AllowAnonymous(); |
| | 12 | 18 | | Options(x => x.RequireRateLimiting(ExternalAuthenticationRateLimitPolicyNames.Discovery)); |
| | 6 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task<DiscoverLoginMethodsResponse> ExecuteAsync(CancellationToken cancellationToken) |
| | | 22 | | { |
| | 1 | 23 | | var clientId = Query<string>("clientId", true) ?? Query<string>("client_id", true) ?? string.Empty; |
| | | 24 | | try |
| | | 25 | | { |
| | 1 | 26 | | var methods = await broker.DiscoverAsync(tenantAccessor.TenantId, clientId, cancellationToken); |
| | 1 | 27 | | HttpContext.Response.Headers.CacheControl = "no-store"; |
| | 2 | 28 | | return new DiscoverLoginMethodsResponse(methods, methods.SingleOrDefault(x => x.IsPreferred)?.Key); |
| | | 29 | | } |
| | 0 | 30 | | catch (InvalidOperationException) |
| | | 31 | | { |
| | 0 | 32 | | HttpContext.Response.StatusCode = 400; |
| | 0 | 33 | | return new DiscoverLoginMethodsResponse([], null); |
| | | 34 | | } |
| | 1 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>The preferred method is visual metadata only; clients must always render an explicit chooser.</summary> |
| | | 39 | | internal sealed record DiscoverLoginMethodsResponse(IReadOnlyCollection<LoginMethod> Methods, string? PreferredMethodKey |