| | | 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.Models; |
| | | 7 | | using Elsa.ExternalAuthentication.Permissions; |
| | | 8 | | using Elsa.ExternalAuthentication.Services; |
| | | 9 | | using Microsoft.AspNetCore.Http; |
| | | 10 | | |
| | | 11 | | namespace Elsa.ExternalAuthentication.Endpoints.IdentityLinks; |
| | | 12 | | |
| | | 13 | | internal sealed class GetIdentityLinks(ExternalIdentityLinkManagementService management, ITenantAccessor tenantAccessor) |
| | | 14 | | { |
| | | 15 | | public override void Configure() |
| | | 16 | | { |
| | | 17 | | Get("/external-authentication/identity-links"); |
| | | 18 | | ConfigurePermissions(ExternalAuthenticationPermissions.LinksManage); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task<IdentityLinkListResponse> ExecuteAsync(IdentityLinkListRequest request, CancellationToken |
| | | 22 | | { |
| | | 23 | | if (request.PageSize is < 1 or > 100 || !IdentityLinkPagination.TryDecodeCursor(request.Cursor, out var cursor)) |
| | | 24 | | { |
| | | 25 | | HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; |
| | | 26 | | return new IdentityLinkListResponse([], null); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | var pageSize = request.PageSize ?? 100; |
| | | 30 | | var links = (await management.ListAsync(tenantAccessor.TenantId, new ExternalIdentityLinkFilter { UserId = reque |
| | | 31 | | .OrderBy(x => x.CreatedAt) |
| | | 32 | | .ThenBy(x => x.Id, StringComparer.Ordinal) |
| | | 33 | | .Where(x => cursor is null || IdentityLinkPagination.Compare(new IdentityLinkCursor(x.CreatedAt, x.Id), curs |
| | | 34 | | .Take(pageSize + 1) |
| | | 35 | | .ToArray(); |
| | | 36 | | var hasMore = links.Length > pageSize; |
| | | 37 | | var items = hasMore ? links[..^1] : links; |
| | | 38 | | return new IdentityLinkListResponse(items.Select(IdentityLinkDocument.From).ToArray(), hasMore ? IdentityLinkPag |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal sealed class FindUsers(ExternalIdentityLinkManagementService management, ITenantAccessor tenantAccessor) : Elsa |
| | | 43 | | { |
| | | 44 | | public override void Configure() |
| | | 45 | | { |
| | | 46 | | Get("/external-authentication/user-options"); |
| | | 47 | | ConfigurePermissions(ExternalAuthenticationPermissions.LinksManage); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public override async Task<FindIdentityLinkUsersResponse> ExecuteAsync(FindIdentityLinkUsersRequest request, Cancell |
| | | 51 | | { |
| | | 52 | | if (request.PageSize is < 1 or > 50 || !IdentityLinkPagination.TryDecodeUserCursor(request.Cursor, out var curso |
| | | 53 | | { |
| | | 54 | | HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; |
| | | 55 | | return new FindIdentityLinkUsersResponse([], null); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | var pageSize = request.PageSize ?? 50; |
| | | 59 | | var users = (await management.FindUsersAsync(tenantAccessor.TenantId, request.Search, cancellationToken)).Items |
| | | 60 | | .Where(x => cursor is null || IdentityLinkPagination.Compare(new UserCursor(x.DisplayName, x.Id), cursor) > |
| | | 61 | | .Take(pageSize + 1) |
| | | 62 | | .ToArray(); |
| | | 63 | | var hasMore = users.Length > pageSize; |
| | | 64 | | var items = hasMore ? users[..^1] : users; |
| | | 65 | | return new FindIdentityLinkUsersResponse(items.Select(x => new IdentityLinkUserDocument(x.Id, x.DisplayName)).To |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | internal sealed class PrelinkIdentityLink(ExternalIdentityLinkManagementService management, ITenantAccessor tenantAccess |
| | | 70 | | { |
| | | 71 | | public override void Configure() |
| | | 72 | | { |
| | | 73 | | Post("/external-authentication/identity-links"); |
| | | 74 | | ConfigurePermissions(ExternalAuthenticationPermissions.LinksManage); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public override async Task HandleAsync(PrelinkIdentityLinkRequest request, CancellationToken cancellationToken) |
| | | 78 | | { |
| | | 79 | | ExternalIdentityLinkPrelinkResult result; |
| | | 80 | | try |
| | | 81 | | { |
| | | 82 | | result = await management.PrelinkAsync(tenantAccessor.TenantId, request.UserId, request.ConnectionKey, reque |
| | | 83 | | } |
| | | 84 | | catch (ArgumentException) |
| | | 85 | | { |
| | | 86 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status400BadRequest, "validation_f |
| | | 87 | | return; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | switch (result) |
| | | 91 | | { |
| | | 92 | | case ExternalIdentityLinkPrelinkResult.Success(var link, var wasCreated): |
| | | 93 | | HttpContext.Response.StatusCode = wasCreated ? StatusCodes.Status201Created : StatusCodes.Status200OK; |
| | | 94 | | await HttpContext.Response.WriteAsJsonAsync(IdentityLinkDocument.From(link), cancellationToken); |
| | | 95 | | return; |
| | | 96 | | case ExternalIdentityLinkPrelinkResult.Conflict: |
| | | 97 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status409Conflict, "conflict", |
| | | 98 | | return; |
| | | 99 | | default: |
| | | 100 | | // Do not reveal whether a user or connection exists outside the trusted tenant scope. |
| | | 101 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status404NotFound, "not_found" |
| | | 102 | | return; |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | internal sealed class ReplaceIdentityLink(ExternalIdentityLinkManagementService management, ITenantAccessor tenantAccess |
| | | 108 | | { |
| | | 109 | | public override void Configure() |
| | | 110 | | { |
| | | 111 | | Post("/external-authentication/identity-links/{linkId}/replace"); |
| | | 112 | | ConfigurePermissions(ExternalAuthenticationPermissions.LinksManage); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public override async Task HandleAsync(ReplaceIdentityLinkRequest request, CancellationToken cancellationToken) |
| | | 116 | | { |
| | | 117 | | ExternalIdentityLinkReplaceResult result; |
| | | 118 | | try |
| | | 119 | | { |
| | | 120 | | result = await management.ReplaceAsync( |
| | | 121 | | tenantAccessor.TenantId, |
| | | 122 | | Route<string>("linkId")!, |
| | | 123 | | request.UserId, |
| | | 124 | | request.ConnectionKey, |
| | | 125 | | request.Issuer, |
| | | 126 | | request.Subject, |
| | | 127 | | User, |
| | | 128 | | cancellationToken); |
| | | 129 | | } |
| | | 130 | | catch (ArgumentException) |
| | | 131 | | { |
| | | 132 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status400BadRequest, "validation_f |
| | | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | switch (result) |
| | | 137 | | { |
| | | 138 | | case ExternalIdentityLinkReplaceResult.Success success: |
| | | 139 | | HttpContext.Response.StatusCode = StatusCodes.Status201Created; |
| | | 140 | | await HttpContext.Response.WriteAsJsonAsync(IdentityLinkDocument.From(success.NewLink), cancellationToke |
| | | 141 | | return; |
| | | 142 | | case ExternalIdentityLinkReplaceResult.Conflict: |
| | | 143 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status409Conflict, "conflict", |
| | | 144 | | return; |
| | | 145 | | default: |
| | | 146 | | await IdentityLinkEndpointSupport.SendErrorAsync(HttpContext, StatusCodes.Status404NotFound, "not_found" |
| | | 147 | | return; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | internal sealed class DeleteIdentityLink(ExternalIdentityLinkManagementService management, ITenantAccessor tenantAccesso |
| | | 153 | | { |
| | | 154 | | public override void Configure() |
| | | 155 | | { |
| | | 156 | | Delete("/external-authentication/identity-links/{linkId}"); |
| | | 157 | | ConfigurePermissions(ExternalAuthenticationPermissions.LinksManage); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 161 | | { |
| | | 162 | | var deleted = await management.UnlinkAsync(tenantAccessor.TenantId, Route<string>("linkId")!, User, cancellation |
| | | 163 | | HttpContext.Response.StatusCode = deleted ? StatusCodes.Status204NoContent : StatusCodes.Status404NotFound; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | internal sealed class IdentityLinkListRequest |
| | | 168 | | { |
| | 13 | 169 | | public string? UserId { get; set; } |
| | 16 | 170 | | public string? ConnectionKey { get; set; } |
| | 14 | 171 | | public string? Cursor { get; set; } |
| | 27 | 172 | | public int? PageSize { get; set; } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | internal sealed class FindIdentityLinkUsersRequest |
| | | 176 | | { |
| | | 177 | | public string? Search { get; set; } |
| | | 178 | | public string? Cursor { get; set; } |
| | | 179 | | public int? PageSize { get; set; } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | internal sealed class PrelinkIdentityLinkRequest |
| | | 183 | | { |
| | | 184 | | public string UserId { get; set; } = null!; |
| | | 185 | | public string ConnectionKey { get; set; } = null!; |
| | | 186 | | public string Issuer { get; set; } = null!; |
| | | 187 | | public string Subject { get; set; } = null!; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | internal sealed class ReplaceIdentityLinkRequest |
| | | 191 | | { |
| | | 192 | | public string UserId { get; set; } = null!; |
| | | 193 | | public string ConnectionKey { get; set; } = null!; |
| | | 194 | | public string Issuer { get; set; } = null!; |
| | | 195 | | public string Subject { get; set; } = null!; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | internal sealed record IdentityLinkListResponse(IReadOnlyCollection<IdentityLinkDocument> Items, string? NextCursor); |
| | | 199 | | internal sealed record FindIdentityLinkUsersResponse(IReadOnlyCollection<IdentityLinkUserDocument> Items, string? NextCu |
| | | 200 | | internal sealed record IdentityLinkUserDocument(string Id, string DisplayName); |
| | | 201 | | internal sealed record IdentityLinkDocument(string Id, string UserId, string ConnectionKey, string Issuer, string? Subje |
| | | 202 | | { |
| | | 203 | | public static IdentityLinkDocument From(ExternalIdentityLink link) => new(link.Id, link.UserId, link.ConnectionKey, |
| | | 204 | | } |
| | | 205 | | internal sealed record IdentityLinkError(string Error, string Message); |
| | | 206 | | internal sealed record IdentityLinkCursor(DateTimeOffset CreatedAt, string Id); |
| | | 207 | | internal sealed record UserCursor(string DisplayName, string Id); |
| | | 208 | | |
| | | 209 | | internal static class IdentityLinkEndpointSupport |
| | | 210 | | { |
| | | 211 | | public static Task SendErrorAsync(HttpContext httpContext, int status, string error, string message, CancellationTok |
| | | 212 | | { |
| | | 213 | | httpContext.Response.StatusCode = status; |
| | | 214 | | return httpContext.Response.WriteAsJsonAsync(new IdentityLinkError(error, message), cancellationToken); |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | internal static class IdentityLinkPagination |
| | | 219 | | { |
| | | 220 | | public static string EncodeCursor(IdentityLinkCursor cursor) => Encode(cursor); |
| | | 221 | | public static string EncodeUserCursor(UserCursor cursor) => Encode(cursor); |
| | | 222 | | public static bool TryDecodeCursor(string? value, out IdentityLinkCursor? cursor) => TryDecode(value, out cursor); |
| | | 223 | | public static bool TryDecodeUserCursor(string? value, out UserCursor? cursor) => TryDecode(value, out cursor); |
| | | 224 | | |
| | | 225 | | public static int Compare(IdentityLinkCursor left, IdentityLinkCursor right) |
| | | 226 | | { |
| | | 227 | | var result = left.CreatedAt.CompareTo(right.CreatedAt); |
| | | 228 | | return result != 0 ? result : string.Compare(left.Id, right.Id, StringComparison.Ordinal); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | public static int Compare(UserCursor left, UserCursor right) |
| | | 232 | | { |
| | | 233 | | var result = string.Compare(left.DisplayName, right.DisplayName, StringComparison.Ordinal); |
| | | 234 | | return result != 0 ? result : string.Compare(left.Id, right.Id, StringComparison.Ordinal); |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | private static string Encode<T>(T cursor) => Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer.Serialize( |
| | | 238 | | |
| | | 239 | | private static bool TryDecode<T>(string? value, out T? cursor) where T : class |
| | | 240 | | { |
| | | 241 | | cursor = null; |
| | | 242 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 243 | | return true; |
| | | 244 | | if (value.Length > 512) |
| | | 245 | | return false; |
| | | 246 | | try |
| | | 247 | | { |
| | | 248 | | var padded = value.Replace('-', '+').Replace('_', '/'); |
| | | 249 | | padded = padded.PadRight(padded.Length + (4 - padded.Length % 4) % 4, '='); |
| | | 250 | | cursor = JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(Convert.FromBase64String(padded))); |
| | | 251 | | return cursor is not null && IsBounded(cursor); |
| | | 252 | | } |
| | | 253 | | catch (Exception exception) when (exception is FormatException or JsonException) |
| | | 254 | | { |
| | | 255 | | return false; |
| | | 256 | | } |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | private static bool IsBounded<T>(T cursor) where T : class => cursor switch |
| | | 260 | | { |
| | | 261 | | IdentityLinkCursor value => value.Id.Length is > 0 and <= 256, |
| | | 262 | | UserCursor value => value.Id.Length is > 0 and <= 256 && value.DisplayName.Length <= 512, |
| | | 263 | | _ => false |
| | | 264 | | }; |
| | | 265 | | } |