| | | 1 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 2 | | using Elsa.ExternalAuthentication.Models; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace Elsa.ExternalAuthentication.Policies; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Requests just-in-time creation of a credential-less Elsa user for an unlinked external identity. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class CreateUserUnlinkedIdentityPolicy : IUnlinkedIdentityPolicy |
| | | 11 | | { |
| | | 12 | | public const string PolicyType = "create-user"; |
| | | 13 | | private const string DefaultUserNamePrefix = "external"; |
| | | 14 | | |
| | 7 | 15 | | public string Type => PolicyType; |
| | | 16 | | |
| | 1 | 17 | | public UnlinkedIdentityPolicyDescriptor Describe() => new( |
| | 1 | 18 | | Type, |
| | 1 | 19 | | "Create a user", |
| | 1 | 20 | | "Creates a credential-less Elsa user and an external identity link after successful external authentication.", |
| | 1 | 21 | | 1, |
| | 1 | 22 | | [new("defaultRoleIds", "Default roles", "Role IDs assigned only when this policy creates a new user.", "string-a |
| | 1 | 23 | | null); |
| | | 24 | | |
| | | 25 | | public ValueTask<UnlinkedIdentityDecision> EvaluateAsync(UnlinkedIdentityContext context, CancellationToken cancella |
| | | 26 | | { |
| | 2 | 27 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 2 | 28 | | return ValueTask.FromResult<UnlinkedIdentityDecision>(new UnlinkedIdentityDecision.CreateUser(new UserCreationPr |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static IReadOnlyCollection<string> ReadRoleIds(JsonElement settings) => |
| | 22 | 32 | | settings.ValueKind == JsonValueKind.Object && settings.TryGetProperty("defaultRoleIds", out var values) && value |
| | 69 | 33 | | ? values.EnumerateArray().Where(x => x.ValueKind == JsonValueKind.String).Select(x => x.GetString()).Where(x |
| | 22 | 34 | | : []; |
| | | 35 | | } |