| | | 1 | | using System.Security.Claims; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using Microsoft.AspNetCore.Authentication; |
| | | 4 | | using Kestrun.Claims; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.Authentication; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Options for configuring Basic Authentication in Kestrun. |
| | | 10 | | /// </summary> |
| | | 11 | | public partial class BasicAuthenticationOptions : AuthenticationSchemeOptions, IAuthenticationCommonOptions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets or sets the name of the HTTP header used for authentication. |
| | | 15 | | /// </summary> |
| | 74 | 16 | | public string HeaderName { get; set; } = "Authorization"; |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets or sets a value indicating whether the credentials are Base64 encoded. |
| | | 19 | | /// </summary> |
| | 75 | 20 | | public bool Base64Encoded { get; set; } = true; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets the regular expression used to separate the username and password in the credentials. |
| | | 24 | | /// </summary> |
| | 71 | 25 | | public Regex SeparatorRegex { get; set; } = MyRegex(); |
| | | 26 | | |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets or sets the authentication realm used in the WWW-Authenticate header. |
| | | 30 | | /// </summary> |
| | 49 | 31 | | public string Realm { get; set; } = "Kestrun"; |
| | | 32 | | |
| | | 33 | | [GeneratedRegex("^([^:]*):(.*)$", RegexOptions.Compiled)] |
| | | 34 | | private static partial Regex MyRegex(); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets or sets the Serilog logger used for authentication events. |
| | | 38 | | /// </summary> |
| | 78 | 39 | | public Serilog.ILogger Logger { get; set; } = Serilog.Log.ForContext<BasicAuthenticationOptions>(); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets a value indicating whether HTTPS is required for authentication. |
| | | 43 | | /// </summary> |
| | 76 | 44 | | public bool RequireHttps { get; set; } = true; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets or sets a value indicating whether to suppress the WWW-Authenticate header in responses. |
| | | 48 | | /// </summary> |
| | 6 | 49 | | public bool SuppressWwwAuthenticate { get; set; } |
| | | 50 | | |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Delegate to validate user credentials. |
| | | 54 | | /// Parameters: HttpContext, username, password. Returns: Task<bool> indicating validity. |
| | | 55 | | /// </summary> |
| | 85 | 56 | | public Func<HttpContext, string, string, Task<bool>> ValidateCredentialsAsync { get; set; } = (_, _, _) => Task.From |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Settings for the authentication code, if using a script. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <remarks> |
| | | 62 | | /// This allows you to specify the language, code, and additional imports/refs. |
| | | 63 | | /// </remarks> |
| | 60 | 64 | | public AuthenticationCodeSettings ValidateCodeSettings { get; set; } = new(); |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// After credentials are valid, this is called to add extra Claims. |
| | | 68 | | /// Parameters: HttpContext, username → IEnumerable of extra claims. |
| | | 69 | | /// </summary> |
| | 10 | 70 | | public Func<HttpContext, string, Task<IEnumerable<Claim>>>? IssueClaims { get; set; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Settings for the claims issuing code, if using a script. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <remarks> |
| | | 76 | | /// This allows you to specify the language, code, and additional imports/refs for claims issuance. |
| | | 77 | | /// </remarks> |
| | 60 | 78 | | public AuthenticationCodeSettings IssueClaimsCodeSettings { get; set; } = new(); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets or sets the claim policy configuration. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <remarks> |
| | | 84 | | /// This allows you to define multiple authorization policies based on claims. |
| | | 85 | | /// Each policy can specify a claim type and allowed values. |
| | | 86 | | /// </remarks> |
| | 11 | 87 | | public ClaimPolicyConfig? ClaimPolicyConfig { get; set; } |
| | | 88 | | } |