< Summary

Information
Class: AsiBackbone.AspNetCore.Actors.AsiBackboneHttpActorContextOptions
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Actors/AsiBackboneHttpActorContextOptions.cs
Line coverage
97%
Covered lines: 39
Uncovered lines: 1
Coverable lines: 40
Total lines: 118
Line coverage: 97.5%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Actors/AsiBackboneHttpActorContextOptions.cs

#LineLine coverage
 1using System.Security.Claims;
 2using AsiBackbone.Core.Actors;
 3
 4namespace AsiBackbone.AspNetCore.Actors;
 5
 6/// <summary>
 7/// Configures how ASP.NET Core claims are mapped into framework-neutral actor contexts.
 8/// </summary>
 9public sealed class AsiBackboneHttpActorContextOptions
 10{
 11    /// <summary>
 12    /// Gets the default stable identifier claim types checked for authenticated actors.
 13    /// </summary>
 4514    public static IReadOnlyList<string> DefaultActorIdClaimTypes { get; } =
 315    [
 316        ClaimTypes.NameIdentifier,
 317        "sub",
 318        "oid",
 319        "client_id",
 320        "azp",
 321        ClaimTypes.Email,
 322    ];
 23
 24    /// <summary>
 25    /// Gets the default display-name claim types checked for authenticated actors.
 26    /// </summary>
 4527    public static IReadOnlyList<string> DefaultDisplayNameClaimTypes { get; } =
 328    [
 329        ClaimTypes.Name,
 330        "name",
 331        "preferred_username",
 332        ClaimTypes.Email,
 333        "email",
 334    ];
 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>
 4542    public static IReadOnlyList<AsiBackboneActorType> DefaultAllowedActorTypesFromClaims { get; } =
 343    [
 344        AsiBackboneActorType.Human,
 345    ];
 46
 47    /// <summary>
 48    /// Gets or sets the claim types used to resolve a stable actor identifier.
 49    /// </summary>
 26750    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>
 12755    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>
 13063    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>
 18971    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>
 12076    public AsiBackboneActorType DefaultAuthenticatedActorType { get; set; } = AsiBackboneActorType.Human;
 77
 78    /// <summary>
 79    /// Gets or sets the display name used for unauthenticated actors.
 80    /// </summary>
 581    public string? UnauthenticatedDisplayName { get; set; }
 82
 83    /// <summary>
 84    /// Validates the options.
 85    /// </summary>
 86    public void Validate()
 87    {
 6988        if (ActorIdClaimTypes is null || ActorIdClaimTypes.Count == 0 || ActorIdClaimTypes.All(string.IsNullOrWhiteSpace
 89        {
 190            throw new InvalidOperationException("At least one actor identifier claim type must be configured.");
 91        }
 92
 6893        if (DisplayNameClaimTypes is null)
 94        {
 095            throw new InvalidOperationException("DisplayNameClaimTypes must be configured.");
 96        }
 97
 6898        if (string.IsNullOrWhiteSpace(ActorTypeClaimType))
 99        {
 3100            throw new InvalidOperationException("ActorTypeClaimType must be configured.");
 101        }
 102
 65103        if (AllowedActorTypesFromClaims is null)
 104        {
 1105            throw new InvalidOperationException("AllowedActorTypesFromClaims must be configured.");
 106        }
 107
 128108        if (AllowedActorTypesFromClaims.Any(static actorType => !Enum.IsDefined(actorType)))
 109        {
 1110            throw new InvalidOperationException("AllowedActorTypesFromClaims must contain only defined actor types.");
 111        }
 112
 63113        if (!Enum.IsDefined(DefaultAuthenticatedActorType))
 114        {
 1115            throw new InvalidOperationException("DefaultAuthenticatedActorType must be a defined actor type.");
 116        }
 62117    }
 118}