| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Kestrun.Claims; |
| | | 3 | | using Kestrun.Hosting; |
| | | 4 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | | 5 | | using Microsoft.IdentityModel.Tokens; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Authentication; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Options for JWT-based authentication. |
| | | 11 | | /// </summary> |
| | | 12 | | public class JwtAuthOptions : JwtBearerOptions, IOpenApiAuthenticationOptions, IClaimsCommonOptions, IAuthenticationHost |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// If true, allows cookie authentication over insecure HTTP connections. |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public bool AllowInsecureHttp { get; set; } |
| | | 18 | | |
| | | 19 | | private Serilog.ILogger? _logger; |
| | | 20 | | /// <inheritdoc/> |
| | | 21 | | public Serilog.ILogger Logger |
| | | 22 | | { |
| | 0 | 23 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets or sets the token validation parameters. |
| | | 28 | | /// </summary> |
| | 2 | 29 | | public TokenValidationParameters? ValidationParameters { get; set; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | 6 | 32 | | public string? DisplayName { get; set; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | 9 | 35 | | public bool GlobalScheme { get; set; } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | 9 | 38 | | public string? Description { get; set; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | 30 | 41 | | public string[] DocumentationId { get; set; } = []; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | 15 | 44 | | public KestrunHost Host { get; set; } = default!; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Configuration for claim policy enforcement. |
| | | 48 | | /// </summary> |
| | 13 | 49 | | public ClaimPolicyConfig? ClaimPolicy { get; set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// After credentials are valid, this is called to add extra Claims. |
| | | 53 | | /// Parameters: HttpContext, username → IEnumerable of extra claims. |
| | | 54 | | /// </summary> |
| | 3 | 55 | | public Func<HttpContext, string, Task<IEnumerable<Claim>>>? IssueClaims { get; set; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Settings for the claims issuing code, if using a script. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <remarks> |
| | | 61 | | /// This allows you to specify the language, code, and additional imports/refs for claims issuance. |
| | | 62 | | /// </remarks> |
| | 13 | 63 | | public AuthenticationCodeSettings IssueClaimsCodeSettings { get; set; } = new(); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets the claim policy configuration. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <remarks> |
| | | 69 | | /// This allows you to define multiple authorization policies based on claims. |
| | | 70 | | /// Each policy can specify a claim type and allowed values. |
| | | 71 | | /// </remarks> |
| | 2 | 72 | | public ClaimPolicyConfig? ClaimPolicyConfig { get; set; } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Helper to copy values from a user-supplied JwtBearerOptions instance to the instance |
| | | 76 | | /// created by the framework inside AddJwtBearer(). Reassigning the local variable (opts = source) would |
| | | 77 | | /// not work because only the local reference changes – the framework keeps the original instance. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="target">The target options to copy to.</param> |
| | | 80 | | /// <exception cref="ArgumentNullException">Thrown when source or target is null.</exception> |
| | | 81 | | /// <remarks> |
| | | 82 | | /// Only copies primitive properties and references. Does not clone complex objects like CookieBuilder. |
| | | 83 | | /// </remarks> |
| | | 84 | | public void ApplyTo(JwtAuthOptions target) |
| | | 85 | | { |
| | 3 | 86 | | ApplyTo((JwtBearerOptions)target); |
| | 3 | 87 | | target.GlobalScheme = GlobalScheme; |
| | 3 | 88 | | target.Description = Description; |
| | 3 | 89 | | target.DocumentationId = DocumentationId; |
| | 3 | 90 | | target.DisplayName = DisplayName; |
| | 3 | 91 | | target.Host = Host; |
| | 3 | 92 | | target.ClaimPolicy = ClaimPolicy; |
| | 3 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Helper to copy values from this JwtAuthOptions instance to a target JwtBearerOptions instance. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="target">The target JwtBearerOptions instance to copy values to.</param> |
| | | 99 | | public void ApplyTo(JwtBearerOptions target) |
| | | 100 | | { |
| | | 101 | | // Paths & return URL |
| | 3 | 102 | | target.TokenValidationParameters = TokenValidationParameters; |
| | 3 | 103 | | target.MapInboundClaims = MapInboundClaims; |
| | 3 | 104 | | target.SaveToken = SaveToken; |
| | 3 | 105 | | target.IncludeErrorDetails = IncludeErrorDetails; |
| | 3 | 106 | | target.RefreshOnIssuerKeyNotFound = RefreshOnIssuerKeyNotFound; |
| | 3 | 107 | | target.RequireHttpsMetadata = RequireHttpsMetadata; |
| | 3 | 108 | | target.MetadataAddress = MetadataAddress; |
| | 3 | 109 | | target.Authority = Authority; |
| | 3 | 110 | | target.Audience = Audience; |
| | 3 | 111 | | target.Challenge = Challenge; |
| | 3 | 112 | | } |
| | | 113 | | } |