| | | 1 | | using System.Security.Claims; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Common; |
| | | 4 | | using Elsa.Common.Models; |
| | | 5 | | using Elsa.ExternalAuthentication.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.ExternalAuthentication.Contracts; |
| | | 8 | | |
| | | 9 | | public interface IExternalAuthenticationAdapter |
| | | 10 | | { |
| | | 11 | | string Type { get; } |
| | | 12 | | ExternalAuthenticationAdapterDescriptor Describe(); |
| | | 13 | | ValueTask<ConnectionValidationResult> ValidateAsync(ConnectionValidationContext context, CancellationToken cancellat |
| | | 14 | | ValueTask<ExternalAuthorizationRequest> CreateAuthorizationRequestAsync(ExternalAuthorizationContext context, Cancel |
| | | 15 | | ValueTask<ExternalAuthenticationResult> AuthenticateCallbackAsync(ExternalCallbackContext context, CancellationToken |
| | | 16 | | ValueTask<ConnectionTestResult> TestAsync(ConnectionTestContext context, CancellationToken cancellationToken = defau |
| | | 17 | | ValueTask<ExternalLogoutRequest?> CreateLogoutRequestAsync(ExternalLogoutContext context, CancellationToken cancella |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public interface IExternalAuthenticationAdapterRegistry |
| | | 21 | | { |
| | | 22 | | IReadOnlyCollection<ExternalAuthenticationAdapterDescriptor> ListDescriptors(); |
| | | 23 | | bool TryGet(string type, out IExternalAuthenticationAdapter adapter); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public interface IUnlinkedIdentityPolicyRegistry |
| | | 27 | | { |
| | | 28 | | IReadOnlyCollection<UnlinkedIdentityPolicyDescriptor> ListDescriptors(); |
| | | 29 | | bool TryGet(string type, out IUnlinkedIdentityPolicy policy); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public interface IPermissionGrantSourceRegistry |
| | | 33 | | { |
| | | 34 | | IReadOnlyCollection<PermissionGrantSourceDescriptor> ListDescriptors(); |
| | | 35 | | bool TryGet(string type, out IPermissionGrantSource source); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public interface IExternalUserMatcherRegistry |
| | | 39 | | { |
| | | 40 | | IReadOnlyCollection<ExternalUserMatcherDescriptor> ListDescriptors(); |
| | | 41 | | bool TryGet(string type, out IExternalUserMatcher matcher); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public interface IAdapterSettingsMigration |
| | | 45 | | { |
| | | 46 | | string AdapterType { get; } |
| | | 47 | | int FromVersion { get; } |
| | | 48 | | int ToVersion { get; } |
| | | 49 | | ValueTask<JsonElement> MigrateAsync(JsonElement settings, CancellationToken cancellationToken = default); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public interface IAdapterSettingsMigrationService |
| | | 53 | | { |
| | | 54 | | ValueTask<AdapterSettingsMigrationResult> MigrateAsync( |
| | | 55 | | string adapterType, |
| | | 56 | | int settingsVersion, |
| | | 57 | | JsonElement settings, |
| | | 58 | | CancellationToken cancellationToken = default); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public interface IIdentityProviderConnectionSource |
| | | 62 | | { |
| | | 63 | | string Name { get; } |
| | | 64 | | ConnectionSourceOwnership Ownership { get; } |
| | | 65 | | ValueTask<ConnectionSourceSnapshot> GetSnapshotAsync(ConnectionScope scope, CancellationToken cancellationToken = de |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public interface IIdentityProviderConnectionRegistry |
| | | 69 | | { |
| | | 70 | | ValueTask<EffectiveConnectionRegistry> GetAsync(string targetTenantId, CancellationToken cancellationToken = default |
| | | 71 | | ValueTask<EffectiveIdentityProviderConnection?> FindByKeyAsync(string targetTenantId, string key, CancellationToken |
| | | 72 | | ValueTask<EffectiveIdentityProviderConnection?> FindByIdAsync(string targetTenantId, string connectionId, Cancellati |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public interface IIdentityProviderConnectionValidityAssessor |
| | | 76 | | { |
| | | 77 | | ValueTask<EffectiveIdentityProviderConnection> AssessAsync( |
| | | 78 | | EffectiveIdentityProviderConnection connection, |
| | | 79 | | CancellationToken cancellationToken = default); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public interface IIdentityProviderConnectionStore |
| | | 83 | | { |
| | | 84 | | ValueTask<Page<IdentityProviderConnection>> FindAsync(ConnectionFilter filter, CancellationToken cancellationToken = |
| | | 85 | | ValueTask<IdentityProviderConnection?> FindByIdAsync(string id, CancellationToken cancellationToken = default); |
| | | 86 | | ValueTask<ConnectionMutationResult> CreateAsync(IdentityProviderConnection connection, CancellationToken cancellatio |
| | | 87 | | ValueTask<ConnectionMutationResult> UpdateAsync(IdentityProviderConnection connection, long expectedRevision, Cancel |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public interface ISecretBindingResolver |
| | | 91 | | { |
| | | 92 | | string Type { get; } |
| | | 93 | | ValueTask<SecretBindingState> GetStateAsync(SecretBinding binding, CancellationToken cancellationToken = default); |
| | | 94 | | ValueTask<ResolvedSecretBinding> ResolveAsync(SecretBinding binding, CancellationToken cancellationToken = default); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary>Optionally accepts a secret value once and stages it through a managed secret backend.</summary> |
| | | 98 | | public interface IManagedSecretBindingWriter |
| | | 99 | | { |
| | | 100 | | string ResolverType { get; } |
| | | 101 | | string DisplayName { get; } |
| | | 102 | | /// <summary> |
| | | 103 | | /// Stores the value under a fresh reference that is not used by any live binding. |
| | | 104 | | /// Implementations must not rotate or overwrite an existing reference. |
| | | 105 | | /// </summary> |
| | | 106 | | ValueTask<SecretBinding> StageAsync(ManagedSecretBindingWriteRequest request, CancellationToken cancellationToken = |
| | | 107 | | ValueTask RemoveAsync(SecretBinding binding, CancellationToken cancellationToken = default); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary>Write-only input for a managed connection secret. Implementations must not expose <see cref="Value"/>.</sum |
| | | 111 | | public sealed record ManagedSecretBindingWriteRequest(string ConnectionId, string FieldName, SensitiveString Value); |
| | | 112 | | |
| | | 113 | | /// <summary>Creates keyed, non-reversible storage keys for opaque browser handles.</summary> |
| | | 114 | | public interface IExternalAuthenticationHandleHasher |
| | | 115 | | { |
| | | 116 | | string Hash(string value); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public interface IUnlinkedIdentityPolicy |
| | | 120 | | { |
| | | 121 | | string Type { get; } |
| | | 122 | | UnlinkedIdentityPolicyDescriptor Describe(); |
| | | 123 | | ValueTask<UnlinkedIdentityDecision> EvaluateAsync(UnlinkedIdentityContext context, CancellationToken cancellationTok |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary>Trusted deployed extension that may identify one Elsa user for an unlinked external identity.</summary> |
| | | 127 | | public interface IExternalUserMatcher |
| | | 128 | | { |
| | | 129 | | string Type { get; } |
| | | 130 | | ExternalUserMatcherDescriptor Describe(); |
| | | 131 | | ValueTask<ExternalUserMatchResult> MatchAsync(ExternalUserMatcherContext context, CancellationToken cancellationToke |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public interface IExternalIdentityResolver |
| | | 135 | | { |
| | | 136 | | ValueTask<ExternalIdentityResolution> ResolveAsync(ExternalIdentityResolutionContext context, CancellationToken canc |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary>Optionally records completed sign-ins for an external identity resolver or provisioner.</summary> |
| | | 140 | | public interface IExternalIdentitySignInTracker |
| | | 141 | | { |
| | | 142 | | /// <summary> |
| | | 143 | | /// Records a completed sign-in for the tenant-scoped external identity link without changing its other metadata. |
| | | 144 | | /// Concurrent calls must preserve the latest timestamp. |
| | | 145 | | /// </summary> |
| | | 146 | | ValueTask<bool> RecordSuccessfulSignInAsync( |
| | | 147 | | string tenantId, |
| | | 148 | | string connectionKey, |
| | | 149 | | ExternalIdentity identity, |
| | | 150 | | string userId, |
| | | 151 | | DateTimeOffset signedInAt, |
| | | 152 | | CancellationToken cancellationToken = default); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | public interface IExternalIdentityProvisioner |
| | | 156 | | { |
| | | 157 | | /// <summary> |
| | | 158 | | /// Finds the link for a normalized external identity without exposing its persisted subject representation. |
| | | 159 | | /// </summary> |
| | | 160 | | ValueTask<ExternalIdentityLink?> FindLinkAsync(string tenantId, string connectionKey, ExternalIdentity identity, Can |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Creates the requested link and, when requested, its credential-less user; compensates a losing writer; or return |
| | | 164 | | /// </summary> |
| | | 165 | | ValueTask<ProvisioningResult> CreateLinkOrGetExistingAsync(ProvisioningRequest request, CancellationToken cancellati |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Atomically removes the tenant-scoped link identified by <see cref="ExternalIdentityLinkReplaceRequest.LinkId"/> |
| | | 169 | | /// and creates its replacement, or returns the conflicting link without changing the original. |
| | | 170 | | /// </summary> |
| | | 171 | | ValueTask<ExternalIdentityLinkReplaceResult> ReplaceAsync(ExternalIdentityLinkReplaceRequest request, CancellationTo |
| | | 172 | | throw new NotSupportedException("This external identity provisioner does not support atomic link replacement."); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Provides tenant-bounded administration of existing external identity links. |
| | | 177 | | /// Creation remains on <see cref="IExternalIdentityProvisioner"/> so administrator prelinks and JIT provisioning use th |
| | | 178 | | /// </summary> |
| | | 179 | | public interface IExternalIdentityLinkManagementStore |
| | | 180 | | { |
| | | 181 | | ValueTask<Page<ExternalIdentityLink>> FindAsync(ExternalIdentityLinkFilter filter, CancellationToken cancellationTok |
| | | 182 | | ValueTask<bool> DeleteAsync(string tenantId, string linkId, CancellationToken cancellationToken = default); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | public interface IPermissionGrantSource |
| | | 186 | | { |
| | | 187 | | string Type { get; } |
| | | 188 | | PermissionGrantSourceDescriptor Describe(); |
| | | 189 | | ValueTask<PermissionGrantResult> GetGrantsAsync(PermissionGrantContext context, CancellationToken cancellationToken |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | public interface IPermissionDelegationAuthorizer |
| | | 193 | | { |
| | | 194 | | ValueTask<PermissionDelegationResult> AuthorizeAsync(ClaimsPrincipal actor, IReadOnlyCollection<GrantSourceSelection |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public interface IPermissionGrantResolver |
| | | 198 | | { |
| | | 199 | | ValueTask<PermissionGrantResult> ResolveAsync(PermissionGrantResolutionContext context, CancellationToken cancellati |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public interface IPermissionDescriptorProvider |
| | | 203 | | { |
| | | 204 | | IEnumerable<PermissionDescriptor> GetDescriptors(); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public interface IPermissionDescriptorRegistry |
| | | 208 | | { |
| | | 209 | | IReadOnlyCollection<PermissionDescriptor> List(); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public interface IExternalAuthenticationStateStore |
| | | 213 | | { |
| | | 214 | | ValueTask PutAsync<T>(string purpose, string handleHash, T value, DateTimeOffset expiresAt, CancellationToken cancel |
| | | 215 | | ValueTask<TakeResult<T>> TryTakeAsync<T>(string purpose, string handleHash, CancellationToken cancellationToken = de |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | public interface IAuthorizationGrantStore |
| | | 219 | | { |
| | | 220 | | ValueTask SaveAsync(AuthorizationGrant grant, CancellationToken cancellationToken = default); |
| | | 221 | | ValueTask<TakeResult<AuthorizationGrant>> TryTakeAsync(string codeHash, CancellationToken cancellationToken = defaul |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public interface IExternalAuthenticationSessionStore |
| | | 225 | | { |
| | | 226 | | ValueTask<IReadOnlyCollection<ExternalAuthenticationSession>> FindAsync(ExternalAuthenticationSessionFilter filter, |
| | | 227 | | ValueTask<ExternalAuthenticationSession?> FindByIdAsync(string sessionId, CancellationToken cancellationToken = defa |
| | | 228 | | ValueTask<ExternalAuthenticationSession?> FindByRefreshTokenHashAsync(string refreshTokenHash, CancellationToken can |
| | | 229 | | ValueTask SaveAsync(ExternalAuthenticationSession session, CancellationToken cancellationToken = default); |
| | | 230 | | ValueTask<ExternalAuthenticationSessionRotationResult> TryRotateRefreshTokenAsync(string sessionId, string refreshTo |
| | | 231 | | ValueTask<bool> RevokeAsync(string sessionId, string reason, DateTimeOffset revokedAt, CancellationToken cancellatio |
| | | 232 | | ValueTask<int> RevokeActiveForConnectionAsync(string connectionKey, string reason, DateTimeOffset revokedAt, Cancell |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | public interface IPreviewResultStore |
| | | 236 | | { |
| | | 237 | | ValueTask SaveAsync(PreviewResult result, CancellationToken cancellationToken = default); |
| | | 238 | | ValueTask<TakeResult<PreviewResult>> TryTakeAsync(string handleHash, string administratorId, CancellationToken cance |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | public interface IConnectionObservationStore |
| | | 242 | | { |
| | | 243 | | ValueTask<ConnectionObservation?> FindLatestAsync(string connectionId, CancellationToken cancellationToken = default |
| | | 244 | | ValueTask SaveLatestAsync(ConnectionObservation observation, CancellationToken cancellationToken = default); |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | public interface IConnectionRegistryVersionStore |
| | | 248 | | { |
| | | 249 | | ValueTask<long> GetVersionAsync(CancellationToken cancellationToken = default); |
| | | 250 | | ValueTask<long> AdvanceAsync(CancellationToken cancellationToken = default); |
| | | 251 | | ValueTask<bool> IsCurrentAsync(long version, CancellationToken cancellationToken = default); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | public interface IExternalAuthenticationTokenIssuer |
| | | 255 | | { |
| | | 256 | | ValueTask<ExternalTokenResponse> IssueAsync(ExternalAuthenticationSession session, CancellationToken cancellationTok |
| | | 257 | | ValueTask<ExternalTokenResponse> RefreshAsync(string clientId, SensitiveString refreshToken, CancellationToken cance |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public sealed record ResolvedSecretBinding(SensitiveString Value, string GenerationFingerprint); |
| | | 261 | | |
| | 40190 | 262 | | public sealed record ConnectionSourceSnapshot(ConnectionScope Scope, string Version, IReadOnlyCollection<IdentityProvide |
| | | 263 | | |
| | | 264 | | public sealed record IdentityProviderConnectionReference( |
| | | 265 | | string Id, |
| | | 266 | | string DisplayName, |
| | | 267 | | ConnectionSourceOwnership Ownership); |
| | | 268 | | |
| | | 269 | | public sealed record EffectiveIdentityProviderConnection( |
| | | 270 | | IdentityProviderConnection Connection, |
| | | 271 | | ConnectionSourceOwnership Ownership, |
| | | 272 | | ConnectionScope Scope, |
| | | 273 | | ConnectionValidity Validity, |
| | | 274 | | bool IsShadowed, |
| | | 275 | | string SourceName) |
| | | 276 | | { |
| | | 277 | | public IdentityProviderConnectionReference? ShadowedBy { get; init; } |
| | | 278 | | public IReadOnlyCollection<IdentityProviderConnectionReference> Shadows { get; init; } = []; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | public sealed record EffectiveConnectionRegistry( |
| | | 282 | | IReadOnlyCollection<EffectiveIdentityProviderConnection> Connections, |
| | | 283 | | IReadOnlyCollection<LoginMethod> LoginMethods, |
| | | 284 | | string SourceVersion); |
| | | 285 | | |
| | | 286 | | public sealed record ConnectionValidationContext(EffectiveIdentityProviderConnection Connection, IReadOnlyDictionary<str |
| | | 287 | | |
| | | 288 | | public sealed record ExternalAuthorizationContext(EffectiveIdentityProviderConnection Connection, IReadOnlyDictionary<st |
| | | 289 | | |
| | | 290 | | public sealed record ExternalCallbackContext(EffectiveIdentityProviderConnection Connection, IReadOnlyDictionary<string, |
| | | 291 | | |
| | | 292 | | public sealed record ConnectionTestContext(EffectiveIdentityProviderConnection Connection, IReadOnlyDictionary<string, R |
| | | 293 | | |
| | | 294 | | public sealed record ExternalLogoutContext(EffectiveIdentityProviderConnection Connection, IReadOnlyDictionary<string, R |
| | | 295 | | |
| | | 296 | | public sealed record UnlinkedIdentityContext(string TargetTenantId, EffectiveIdentityProviderConnection Connection, Exte |
| | | 297 | | public sealed record ExternalUserMatcherContext(string TargetTenantId, EffectiveIdentityProviderConnection Connection, E |
| | | 298 | | |
| | | 299 | | public abstract record UnlinkedIdentityDecision |
| | | 300 | | { |
| | | 301 | | private UnlinkedIdentityDecision() { } |
| | | 302 | | public sealed record Reject(string SafeReason) : UnlinkedIdentityDecision; |
| | | 303 | | public sealed record CreateUser(UserCreationProposal Proposal) : UnlinkedIdentityDecision; |
| | | 304 | | public sealed record LinkExistingUser(string UserId, string AuthorizationBasis) : UnlinkedIdentityDecision; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | public sealed record ExternalIdentityResolutionContext(string TargetTenantId, EffectiveIdentityProviderConnection Connec |
| | | 308 | | |
| | | 309 | | public sealed record PermissionGrantContext(string TargetTenantId, string UserId, EffectiveIdentityProviderConnection Co |
| | | 310 | | public sealed record PermissionGrantResolutionContext(string TargetTenantId, string UserId, EffectiveIdentityProviderCon |
| | | 311 | | |
| | | 312 | | public abstract record ExternalAuthenticationSessionRotationResult |
| | | 313 | | { |
| | | 314 | | private ExternalAuthenticationSessionRotationResult() { } |
| | | 315 | | public sealed record Rotated(ExternalAuthenticationSession Session) : ExternalAuthenticationSessionRotationResult; |
| | | 316 | | public sealed record NotFound : ExternalAuthenticationSessionRotationResult; |
| | | 317 | | public sealed record Expired : ExternalAuthenticationSessionRotationResult; |
| | | 318 | | public sealed record Reused : ExternalAuthenticationSessionRotationResult; |
| | | 319 | | public sealed record Revoked : ExternalAuthenticationSessionRotationResult; |
| | | 320 | | } |