| | | 1 | | using System.Security.Claims; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Elsa.Abstractions; |
| | | 5 | | using Elsa.Common.Multitenancy; |
| | | 6 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 7 | | using Elsa.ExternalAuthentication.Notifications; |
| | | 8 | | using Elsa.ExternalAuthentication.Options; |
| | | 9 | | using Elsa.ExternalAuthentication.Permissions; |
| | | 10 | | using Elsa.ExternalAuthentication.Services; |
| | | 11 | | using Microsoft.AspNetCore.Http; |
| | | 12 | | using Microsoft.Extensions.Options; |
| | | 13 | | |
| | | 14 | | namespace Elsa.ExternalAuthentication.Endpoints.Sessions; |
| | | 15 | | |
| | | 16 | | internal sealed class ListExternalAuthenticationSessions( |
| | | 17 | | IExternalAuthenticationSessionStore sessions, |
| | | 18 | | ITenantAccessor tenantAccessor, |
| | | 19 | | IOptions<ExternalAuthenticationOptions> options) : ElsaEndpoint<ExternalAuthenticationSessionListRequest, ExternalAu |
| | | 20 | | { |
| | | 21 | | public override void Configure() |
| | | 22 | | { |
| | | 23 | | Get("/external-authentication/sessions"); |
| | | 24 | | ConfigurePermissions(ExternalAuthenticationPermissions.SessionsRead); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public override async Task<ExternalAuthenticationSessionListResponse> ExecuteAsync(ExternalAuthenticationSessionList |
| | | 28 | | { |
| | | 29 | | if (!options.Value.Operations.EnableSessionAdministration) |
| | | 30 | | { |
| | | 31 | | HttpContext.Response.StatusCode = StatusCodes.Status404NotFound; |
| | | 32 | | return new ExternalAuthenticationSessionListResponse([], null); |
| | | 33 | | } |
| | | 34 | | if (request.PageSize is < 1 or > 100 || !IsKnownStatus(request.Status) || !TryDecode(request.Cursor, out var cur |
| | | 35 | | { |
| | | 36 | | HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; |
| | | 37 | | return new ExternalAuthenticationSessionListResponse([], null); |
| | | 38 | | } |
| | | 39 | | var pageSize = request.PageSize ?? 100; |
| | | 40 | | var rows = (await sessions.FindAsync(new Models.ExternalAuthenticationSessionFilter { TenantId = tenantAccessor. |
| | | 41 | | .OrderBy(x => x.StartedAt).ThenBy(x => x.Id, StringComparer.Ordinal) |
| | | 42 | | .Where(x => cursor is null || Compare(new SessionCursor(x.StartedAt, x.Id), cursor) > 0) |
| | | 43 | | .Take(pageSize + 1).ToArray(); |
| | | 44 | | var hasMore = rows.Length > pageSize; |
| | | 45 | | var page = hasMore ? rows[..^1] : rows; |
| | | 46 | | return new ExternalAuthenticationSessionListResponse(page.Select(ExternalAuthenticationSessionDocument.From).ToA |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private static bool IsKnownStatus(string? status) => string.IsNullOrWhiteSpace(status) || status.Equals("active", St |
| | | 50 | | private static int Compare(SessionCursor left, SessionCursor right) => left.StartedAt != right.StartedAt ? left.Star |
| | | 51 | | internal static string Encode(SessionCursor cursor) => Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer. |
| | | 52 | | internal static bool TryDecode(string? value, out SessionCursor? cursor) |
| | | 53 | | { |
| | | 54 | | cursor = null; |
| | | 55 | | if (string.IsNullOrWhiteSpace(value)) return true; |
| | | 56 | | try |
| | | 57 | | { |
| | | 58 | | var padded = value.Replace('-', '+').Replace('_', '/'); |
| | | 59 | | padded = padded.PadRight(padded.Length + (4 - padded.Length % 4) % 4, '='); |
| | | 60 | | cursor = JsonSerializer.Deserialize<SessionCursor>(Encoding.UTF8.GetString(Convert.FromBase64String(padded)) |
| | | 61 | | return cursor is { Id.Length: > 0 and <= 256 }; |
| | | 62 | | } |
| | | 63 | | catch (Exception exception) when (exception is FormatException or JsonException) { return false; } |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | internal sealed class RevokeExternalAuthenticationSession( |
| | 0 | 68 | | IExternalAuthenticationSessionStore sessions, |
| | 0 | 69 | | ITenantAccessor tenantAccessor, |
| | 0 | 70 | | IOptions<ExternalAuthenticationOptions> options, |
| | 0 | 71 | | ExternalAuthenticationSecurityNotifier notifier) : ElsaEndpoint<RevokeExternalAuthenticationSessionRequest> |
| | | 72 | | { |
| | | 73 | | public override void Configure() |
| | | 74 | | { |
| | 0 | 75 | | Delete("/external-authentication/sessions/{sessionId}"); |
| | 0 | 76 | | ConfigurePermissions(ExternalAuthenticationPermissions.SessionsRevoke); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public override async Task HandleAsync(RevokeExternalAuthenticationSessionRequest request, CancellationToken cancell |
| | | 80 | | { |
| | 0 | 81 | | if (!options.Value.Operations.EnableSessionAdministration) |
| | | 82 | | { |
| | 0 | 83 | | HttpContext.Response.StatusCode = StatusCodes.Status404NotFound; |
| | 0 | 84 | | return; |
| | | 85 | | } |
| | 0 | 86 | | var session = await sessions.FindByIdAsync(Route<string>("sessionId")!, cancellationToken); |
| | 0 | 87 | | if (session is null || !string.Equals(session.TenantId, tenantAccessor.TenantId, StringComparison.Ordinal)) |
| | | 88 | | { |
| | 0 | 89 | | HttpContext.Response.StatusCode = StatusCodes.Status404NotFound; |
| | 0 | 90 | | return; |
| | | 91 | | } |
| | 0 | 92 | | var revoked = await sessions.RevokeAsync(session.Id, string.IsNullOrWhiteSpace(request.Reason) ? "administrator_ |
| | 0 | 93 | | if (!revoked) |
| | | 94 | | { |
| | 0 | 95 | | HttpContext.Response.StatusCode = StatusCodes.Status409Conflict; |
| | 0 | 96 | | return; |
| | | 97 | | } |
| | 0 | 98 | | await notifier.PublishAsync(new ExternalAuthenticationSessionRevoked( |
| | 0 | 99 | | ExternalAuthenticationSecurityNotifier.Context(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? User.Find |
| | 0 | 100 | | session.Id, |
| | 0 | 101 | | "administrator_revoked"), cancellationToken); |
| | 0 | 102 | | HttpContext.Response.StatusCode = StatusCodes.Status204NoContent; |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | internal sealed class ExternalAuthenticationSessionListRequest |
| | | 107 | | { |
| | | 108 | | public string? UserId { get; set; } |
| | | 109 | | public string? ConnectionKey { get; set; } |
| | | 110 | | public string? Status { get; set; } |
| | | 111 | | public string? Cursor { get; set; } |
| | | 112 | | public int? PageSize { get; set; } |
| | | 113 | | } |
| | | 114 | | internal sealed class RevokeExternalAuthenticationSessionRequest { public string? Reason { get; set; } } |
| | | 115 | | internal sealed record SessionCursor(DateTimeOffset StartedAt, string Id); |
| | | 116 | | internal sealed record ExternalAuthenticationSessionListResponse(IReadOnlyCollection<ExternalAuthenticationSessionDocume |
| | | 117 | | internal sealed record ExternalAuthenticationSessionDocument(string Id, string UserId, string TenantId, string Connectio |
| | | 118 | | { |
| | | 119 | | public static ExternalAuthenticationSessionDocument From(Models.ExternalAuthenticationSession value) => new(value.Id |
| | | 120 | | } |