| | | 1 | | using System.Security.Claims; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using Microsoft.AspNetCore.Authentication; |
| | | 4 | | using Kestrun.Claims; |
| | | 5 | | using Kestrun.Hosting; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Authentication; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Options for configuring Basic Authentication in Kestrun. |
| | | 11 | | /// </summary> |
| | | 12 | | public partial class BasicAuthenticationOptions : AuthenticationSchemeOptions, IAuthenticationCommonOptions, IOpenApiAut |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc/> |
| | 0 | 15 | | public string? DisplayName { get; set; } |
| | | 16 | | /// <inheritdoc/> |
| | 22 | 17 | | public bool GlobalScheme { get; set; } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc/> |
| | 22 | 20 | | public string? Description { get; set; } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc/> |
| | 89 | 23 | | public string[] DocumentationId { get; set; } = []; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc/> |
| | 66 | 26 | | public KestrunHost Host { get; set; } = default!; |
| | | 27 | | |
| | | 28 | | private Serilog.ILogger? _logger; |
| | | 29 | | /// <inheritdoc/> |
| | | 30 | | public Serilog.ILogger Logger |
| | | 31 | | { |
| | 14 | 32 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 33 | | } |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets or sets the name of the HTTP header used for authentication. |
| | | 36 | | /// </summary> |
| | 84 | 37 | | public string HeaderName { get; set; } = "Authorization"; |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets a value indicating whether the credentials are Base64 encoded. |
| | | 40 | | /// </summary> |
| | 85 | 41 | | public bool Base64Encoded { get; set; } = true; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the regular expression used to separate the username and password in the credentials. |
| | | 45 | | /// </summary> |
| | 91 | 46 | | public Regex SeparatorRegex { get; set; } = MyRegex(); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets or sets the authentication realm used in the WWW-Authenticate header. |
| | | 50 | | /// </summary> |
| | 59 | 51 | | public string Realm { get; set; } = "Kestrun"; |
| | | 52 | | |
| | | 53 | | [GeneratedRegex("^([^:]*):(.*)$", RegexOptions.Compiled)] |
| | | 54 | | private static partial Regex MyRegex(); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets a value indicating whether to allow insecure HTTP connections. |
| | | 58 | | /// </summary> |
| | 42 | 59 | | public bool AllowInsecureHttp { get; set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets a value indicating whether to suppress the WWW-Authenticate header in responses. |
| | | 63 | | /// </summary> |
| | 16 | 64 | | public bool SuppressWwwAuthenticate { get; set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Delegate to validate user credentials. |
| | | 68 | | /// Parameters: HttpContext, username, password. Returns: Task<bool> indicating validity. |
| | | 69 | | /// </summary> |
| | 99 | 70 | | public Func<HttpContext, string, string, Task<bool>> ValidateCredentialsAsync { get; set; } = (_, _, _) => Task.From |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Settings for the authentication code, if using a script. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <remarks> |
| | | 76 | | /// This allows you to specify the language, code, and additional imports/refs. |
| | | 77 | | /// </remarks> |
| | 69 | 78 | | public AuthenticationCodeSettings ValidateCodeSettings { get; set; } = new(); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// After credentials are valid, this is called to add extra Claims. |
| | | 82 | | /// Parameters: HttpContext, username → IEnumerable of extra claims. |
| | | 83 | | /// </summary> |
| | 24 | 84 | | public Func<HttpContext, string, Task<IEnumerable<Claim>>>? IssueClaims { get; set; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Settings for the claims issuing code, if using a script. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <remarks> |
| | | 90 | | /// This allows you to specify the language, code, and additional imports/refs for claims issuance. |
| | | 91 | | /// </remarks> |
| | 69 | 92 | | public AuthenticationCodeSettings IssueClaimsCodeSettings { get; set; } = new(); |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets or sets the claim policy configuration. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <remarks> |
| | | 98 | | /// This allows you to define multiple authorization policies based on claims. |
| | | 99 | | /// Each policy can specify a claim type and allowed values. |
| | | 100 | | /// </remarks> |
| | 20 | 101 | | public ClaimPolicyConfig? ClaimPolicyConfig { get; set; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Helper to copy values from a user-supplied BasicAuthenticationOptions instance to the instance |
| | | 105 | | /// created by the framework inside AddBasic(). Reassigning the local variable (opts = source) would |
| | | 106 | | /// not work because only the local reference changes – the framework keeps the original instance. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="target">The target instance to which values will be copied. </param> |
| | | 109 | | public void ApplyTo(BasicAuthenticationOptions target) |
| | | 110 | | { |
| | | 111 | | // Copy properties from the provided configure object |
| | 7 | 112 | | target.HeaderName = HeaderName; |
| | 7 | 113 | | target.Base64Encoded = Base64Encoded; |
| | 7 | 114 | | if (SeparatorRegex is not null) |
| | | 115 | | { |
| | 7 | 116 | | target.SeparatorRegex = new Regex(SeparatorRegex.ToString(), SeparatorRegex.Options); |
| | | 117 | | } |
| | | 118 | | |
| | 7 | 119 | | target.Realm = Realm; |
| | 7 | 120 | | target.AllowInsecureHttp = AllowInsecureHttp; |
| | 7 | 121 | | target.SuppressWwwAuthenticate = SuppressWwwAuthenticate; |
| | 7 | 122 | | target.ValidateCredentialsAsync = ValidateCredentialsAsync; |
| | | 123 | | // Copy properties from the provided configure object |
| | 7 | 124 | | target.ValidateCodeSettings = ValidateCodeSettings; |
| | 7 | 125 | | target.IssueClaimsCodeSettings = IssueClaimsCodeSettings; |
| | 7 | 126 | | target.IssueClaims = IssueClaims; |
| | | 127 | | // Claims policy configuration |
| | 7 | 128 | | target.ClaimPolicyConfig = ClaimPolicyConfig; |
| | | 129 | | |
| | | 130 | | // Copy IAuthenticationHostOptions properties |
| | 7 | 131 | | target.Host = Host; |
| | | 132 | | // OpenAPI / documentation properties(IOpenApiAuthenticationOptions) |
| | 7 | 133 | | target.GlobalScheme = GlobalScheme; |
| | 7 | 134 | | target.Description = Description; |
| | 7 | 135 | | target.DocumentationId = DocumentationId; |
| | 7 | 136 | | } |
| | | 137 | | } |