| | | 1 | | using System.Security.Claims; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.Actors; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Configures how ASP.NET Core claims are mapped into framework-neutral actor contexts. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class AsiBackboneHttpActorContextOptions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Gets the default stable identifier claim types checked for authenticated actors. |
| | | 13 | | /// </summary> |
| | 45 | 14 | | public static IReadOnlyList<string> DefaultActorIdClaimTypes { get; } = |
| | 3 | 15 | | [ |
| | 3 | 16 | | ClaimTypes.NameIdentifier, |
| | 3 | 17 | | "sub", |
| | 3 | 18 | | "oid", |
| | 3 | 19 | | "client_id", |
| | 3 | 20 | | "azp", |
| | 3 | 21 | | ClaimTypes.Email, |
| | 3 | 22 | | ]; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the default display-name claim types checked for authenticated actors. |
| | | 26 | | /// </summary> |
| | 45 | 27 | | public static IReadOnlyList<string> DefaultDisplayNameClaimTypes { get; } = |
| | 3 | 28 | | [ |
| | 3 | 29 | | ClaimTypes.Name, |
| | 3 | 30 | | "name", |
| | 3 | 31 | | "preferred_username", |
| | 3 | 32 | | ClaimTypes.Email, |
| | 3 | 33 | | "email", |
| | 3 | 34 | | ]; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the default actor types that may be accepted from an HTTP claim. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <remarks> |
| | | 40 | | /// Privileged software actor types require explicit host opt-in because claim trust is established by the host iden |
| | | 41 | | /// </remarks> |
| | 45 | 42 | | public static IReadOnlyList<AsiBackboneActorType> DefaultAllowedActorTypesFromClaims { get; } = |
| | 3 | 43 | | [ |
| | 3 | 44 | | AsiBackboneActorType.Human, |
| | 3 | 45 | | ]; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the claim types used to resolve a stable actor identifier. |
| | | 49 | | /// </summary> |
| | 267 | 50 | | public IList<string> ActorIdClaimTypes { get; set; } = [.. DefaultActorIdClaimTypes]; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets or sets the claim types used to resolve an optional actor display name. |
| | | 54 | | /// </summary> |
| | 127 | 55 | | public IList<string> DisplayNameClaimTypes { get; set; } = [.. DefaultDisplayNameClaimTypes]; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets the claim type used to resolve an actor type. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <remarks> |
| | | 61 | | /// This must identify a trusted identity-provider-issued or host-generated claim. It must not be populated from use |
| | | 62 | | /// </remarks> |
| | 130 | 63 | | public string ActorTypeClaimType { get; set; } = "actor_type"; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets the actor types that may be accepted from <see cref="ActorTypeClaimType" />. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <remarks> |
| | | 69 | | /// The conservative default accepts only <see cref="AsiBackboneActorType.Human" />. A host must explicitly add <see |
| | | 70 | | /// </remarks> |
| | 189 | 71 | | public IList<AsiBackboneActorType> AllowedActorTypesFromClaims { get; set; } = [.. DefaultAllowedActorTypesFromClaim |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets or sets the actor type used when an authenticated principal does not provide a valid or allowed actor type |
| | | 75 | | /// </summary> |
| | 120 | 76 | | public AsiBackboneActorType DefaultAuthenticatedActorType { get; set; } = AsiBackboneActorType.Human; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets or sets the display name used for unauthenticated actors. |
| | | 80 | | /// </summary> |
| | 5 | 81 | | public string? UnauthenticatedDisplayName { get; set; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Validates the options. |
| | | 85 | | /// </summary> |
| | | 86 | | public void Validate() |
| | | 87 | | { |
| | 69 | 88 | | if (ActorIdClaimTypes is null || ActorIdClaimTypes.Count == 0 || ActorIdClaimTypes.All(string.IsNullOrWhiteSpace |
| | | 89 | | { |
| | 1 | 90 | | throw new InvalidOperationException("At least one actor identifier claim type must be configured."); |
| | | 91 | | } |
| | | 92 | | |
| | 68 | 93 | | if (DisplayNameClaimTypes is null) |
| | | 94 | | { |
| | 0 | 95 | | throw new InvalidOperationException("DisplayNameClaimTypes must be configured."); |
| | | 96 | | } |
| | | 97 | | |
| | 68 | 98 | | if (string.IsNullOrWhiteSpace(ActorTypeClaimType)) |
| | | 99 | | { |
| | 3 | 100 | | throw new InvalidOperationException("ActorTypeClaimType must be configured."); |
| | | 101 | | } |
| | | 102 | | |
| | 65 | 103 | | if (AllowedActorTypesFromClaims is null) |
| | | 104 | | { |
| | 1 | 105 | | throw new InvalidOperationException("AllowedActorTypesFromClaims must be configured."); |
| | | 106 | | } |
| | | 107 | | |
| | 128 | 108 | | if (AllowedActorTypesFromClaims.Any(static actorType => !Enum.IsDefined(actorType))) |
| | | 109 | | { |
| | 1 | 110 | | throw new InvalidOperationException("AllowedActorTypesFromClaims must contain only defined actor types."); |
| | | 111 | | } |
| | | 112 | | |
| | 63 | 113 | | if (!Enum.IsDefined(DefaultAuthenticatedActorType)) |
| | | 114 | | { |
| | 1 | 115 | | throw new InvalidOperationException("DefaultAuthenticatedActorType must be a defined actor type."); |
| | | 116 | | } |
| | 62 | 117 | | } |
| | | 118 | | } |