| | | 1 | | using Elsa.ExternalAuthentication.Models; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using Elsa.Identity.Entities; |
| | | 5 | | using Elsa.Identity.Models; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | |
| | | 8 | | namespace Elsa.ExternalAuthentication.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Applies the provider-independent user resolution and creation policy used by external identity provisioners. |
| | | 12 | | /// </summary> |
| | 126 | 13 | | public sealed class ExternalIdentityUserProvisioningService( |
| | 126 | 14 | | IUserStore userStore, |
| | 126 | 15 | | IUserProvider userProvider, |
| | 126 | 16 | | IRoleProvider roleProvider, |
| | 126 | 17 | | IIdentityGenerator identityGenerator) |
| | | 18 | | { |
| | | 19 | | private const int MaximumUserNameAttempts = 10; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Resolves an explicitly selected user or creates a credential-less user from the supplied proposal. |
| | | 23 | | /// </summary> |
| | | 24 | | public async ValueTask<(User User, bool WasCreated)> ResolveAsync( |
| | | 25 | | ProvisioningRequest request, |
| | | 26 | | Func<string, bool>? tryReserveUserName = null, |
| | | 27 | | CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 63 | 29 | | if (!string.IsNullOrWhiteSpace(request.ExistingUserId)) |
| | | 30 | | { |
| | 44 | 31 | | var existingUser = await userProvider.FindAsync(new UserFilter { Id = request.ExistingUserId }, cancellation |
| | 44 | 32 | | ?? throw new InvalidOperationException("The requested Elsa user does not exist."); |
| | 44 | 33 | | if (!string.Equals(existingUser.TenantId, request.TenantId, StringComparison.Ordinal)) |
| | 1 | 34 | | throw new InvalidOperationException("The requested Elsa user is outside the target tenant."); |
| | | 35 | | |
| | 43 | 36 | | return (existingUser, false); |
| | | 37 | | } |
| | | 38 | | |
| | 19 | 39 | | var proposal = request.Proposal ?? throw new InvalidOperationException("A user creation proposal is required for |
| | 19 | 40 | | var roleIds = await ResolveRoleIdsAsync(proposal.DefaultRoleIds, cancellationToken); |
| | 19 | 41 | | var prefix = NormalizeUserNamePrefix(proposal.UserNamePrefix); |
| | 40 | 42 | | for (var attempt = 0; attempt < MaximumUserNameAttempts; attempt++) |
| | | 43 | | { |
| | 20 | 44 | | var name = $"{prefix}-{identityGenerator.GenerateId()}"; |
| | 20 | 45 | | if (tryReserveUserName is not null && !tryReserveUserName(name)) |
| | | 46 | | continue; |
| | 20 | 47 | | if (await userProvider.FindAsync(new UserFilter { Name = name }, cancellationToken) is not null) |
| | | 48 | | continue; |
| | | 49 | | |
| | 19 | 50 | | var user = new User |
| | 19 | 51 | | { |
| | 19 | 52 | | Id = identityGenerator.GenerateId(), |
| | 19 | 53 | | Name = name, |
| | 19 | 54 | | TenantId = request.TenantId, |
| | 19 | 55 | | HashedPassword = null, |
| | 19 | 56 | | HashedPasswordSalt = null, |
| | 19 | 57 | | Roles = roleIds.ToList() |
| | 19 | 58 | | }; |
| | | 59 | | |
| | | 60 | | try |
| | | 61 | | { |
| | 19 | 62 | | await userStore.SaveAsync(user, cancellationToken); |
| | 19 | 63 | | return (user, true); |
| | | 64 | | } |
| | 0 | 65 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 66 | | { |
| | 0 | 67 | | var persistedUser = await userStore.FindAsync(new UserFilter { Id = user.Id }, CancellationToken.None); |
| | 0 | 68 | | if (persistedUser is not null) |
| | 0 | 69 | | await userStore.DeleteAsync(new UserFilter { Id = user.Id }, CancellationToken.None); |
| | 0 | 70 | | throw; |
| | 0 | 71 | | } |
| | 0 | 72 | | catch |
| | | 73 | | { |
| | 0 | 74 | | var persistedUser = await userProvider.FindAsync(new UserFilter { Id = user.Id }, cancellationToken); |
| | 0 | 75 | | if (persistedUser is not null) |
| | 0 | 76 | | return (persistedUser, true); |
| | 0 | 77 | | if (await userProvider.FindAsync(new UserFilter { Name = name }, cancellationToken) is null) |
| | 0 | 78 | | throw; |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | throw new InvalidOperationException("A unique Elsa user name could not be reserved for the external identity."); |
| | 62 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Removes a user created by an operation that could not publish its external identity link. |
| | | 87 | | /// </summary> |
| | | 88 | | public Task RemoveAsync(User user, CancellationToken cancellationToken = default) => |
| | 4 | 89 | | userStore.DeleteAsync(new UserFilter { Id = user.Id }, cancellationToken); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Checks that the resolved user still exists in the source that supplied it. |
| | | 93 | | /// </summary> |
| | | 94 | | public async ValueTask<bool> ExistsAsync(User user, bool wasCreated, CancellationToken cancellationToken = default) |
| | 64 | 95 | | wasCreated |
| | 64 | 96 | | ? await userStore.FindAsync(new UserFilter { Id = user.Id }, cancellationToken) is not null |
| | 64 | 97 | | : await userProvider.FindAsync(new UserFilter { Id = user.Id }, cancellationToken) is not null; |
| | | 98 | | |
| | | 99 | | private static string NormalizeUserNamePrefix(string prefix) |
| | | 100 | | { |
| | 171 | 101 | | var normalized = new string((prefix ?? string.Empty).Trim().Where(character => char.IsAsciiLetterOrDigit(charact |
| | 19 | 102 | | return string.IsNullOrEmpty(normalized) ? "external" : normalized; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private async ValueTask<IReadOnlyCollection<string>> ResolveRoleIdsAsync(IReadOnlyCollection<string>? roleIds, Cance |
| | | 106 | | { |
| | | 107 | | var requested = (roleIds ?? []).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.Ordinal).ToArr |
| | 19 | 108 | | if (requested.Length == 0) |
| | 18 | 109 | | return []; |
| | | 110 | | var found = (await roleProvider.FindByIdsAsync(requested, cancellationToken)).Select(x => x.Id).ToHashSet(String |
| | 1 | 111 | | if (!found.SetEquals(requested)) |
| | 0 | 112 | | throw new InvalidOperationException("A configured default role no longer exists."); |
| | 1 | 113 | | return requested; |
| | 19 | 114 | | } |
| | | 115 | | } |