| | | 1 | | using Kestrun.Hosting; |
| | | 2 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.Authentication; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Options for cookie-based authentication. |
| | | 8 | | /// </summary> |
| | | 9 | | public class CookieAuthOptions : CookieAuthenticationOptions, IOpenApiAuthenticationOptions, IAuthenticationHostOptions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// If true, allows cookie authentication over insecure HTTP connections. |
| | | 13 | | /// </summary> |
| | 0 | 14 | | public bool AllowInsecureHttp { get; set; } |
| | | 15 | | |
| | | 16 | | /// <inheritdoc/> |
| | 0 | 17 | | public string? DisplayName { get; set; } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc/> |
| | 2 | 20 | | public bool GlobalScheme { get; set; } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc/> |
| | 2 | 23 | | public string? Description { get; set; } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc/> |
| | 33 | 26 | | public string[] DocumentationId { get; set; } = []; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | 2 | 29 | | public KestrunHost Host { get; set; } = default!; |
| | | 30 | | |
| | | 31 | | private Serilog.ILogger? _logger; |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | public Serilog.ILogger Logger |
| | | 34 | | { |
| | 0 | 35 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Helper to copy values from a user-supplied CookieAuthenticationOptions instance to the instance |
| | | 40 | | /// created by the framework inside AddCookie(). Reassigning the local variable (opts = source) would |
| | | 41 | | /// not work because only the local reference changes – the framework keeps the original instance. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="target">The target options to copy to.</param> |
| | | 44 | | /// <exception cref="ArgumentNullException">Thrown when source or target is null.</exception> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// Only copies primitive properties and references. Does not clone complex objects like CookieBuilder. |
| | | 47 | | /// </remarks> |
| | | 48 | | public void ApplyTo(CookieAuthOptions target) |
| | | 49 | | { |
| | 0 | 50 | | ApplyTo((CookieAuthenticationOptions)target); |
| | 0 | 51 | | target.GlobalScheme = GlobalScheme; |
| | 0 | 52 | | target.Description = Description; |
| | 0 | 53 | | target.DocumentationId = DocumentationId; |
| | 0 | 54 | | target.DisplayName = DisplayName; |
| | 0 | 55 | | target.Host = Host; |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Helper to copy values from this CookieAuthOptions instance to a target CookieAuthenticationOptions instance. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="target">The target CookieAuthenticationOptions instance to copy values to.</param> |
| | | 62 | | public void ApplyTo(CookieAuthenticationOptions target) |
| | | 63 | | { |
| | | 64 | | // Paths & return URL |
| | 0 | 65 | | target.LoginPath = LoginPath; |
| | 0 | 66 | | target.LogoutPath = LogoutPath; |
| | 0 | 67 | | target.AccessDeniedPath = AccessDeniedPath; |
| | 0 | 68 | | target.ReturnUrlParameter = ReturnUrlParameter; |
| | | 69 | | |
| | | 70 | | // Expiration & sliding behavior |
| | 0 | 71 | | target.ExpireTimeSpan = ExpireTimeSpan; |
| | 0 | 72 | | target.SlidingExpiration = SlidingExpiration; |
| | | 73 | | |
| | | 74 | | // Cookie builder settings |
| | | 75 | | // (Cookie is always non-null; copy primitive settings) |
| | 0 | 76 | | if (Cookie.Name is not null) |
| | | 77 | | { |
| | 0 | 78 | | target.Cookie.Name = Cookie.Name; |
| | 0 | 79 | | target.Cookie.Path = Cookie.Path; |
| | 0 | 80 | | target.Cookie.Domain = Cookie.Domain; |
| | 0 | 81 | | target.Cookie.HttpOnly = Cookie.HttpOnly; |
| | 0 | 82 | | target.Cookie.SameSite = Cookie.SameSite; |
| | 0 | 83 | | target.Cookie.SecurePolicy = Cookie.SecurePolicy; |
| | 0 | 84 | | target.Cookie.IsEssential = Cookie.IsEssential; |
| | 0 | 85 | | target.Cookie.MaxAge = Cookie.MaxAge; |
| | | 86 | | } |
| | | 87 | | // Forwarding |
| | 0 | 88 | | target.ForwardAuthenticate = ForwardAuthenticate; |
| | 0 | 89 | | target.ForwardChallenge = ForwardChallenge; |
| | 0 | 90 | | target.ForwardDefault = ForwardDefault; |
| | 0 | 91 | | target.ForwardDefaultSelector = ForwardDefaultSelector; |
| | 0 | 92 | | target.ForwardForbid = ForwardForbid; |
| | 0 | 93 | | target.ForwardSignIn = ForwardSignIn; |
| | 0 | 94 | | target.ForwardSignOut = ForwardSignOut; |
| | | 95 | | |
| | | 96 | | // Data protection / ticket / session |
| | 0 | 97 | | target.TicketDataFormat = TicketDataFormat; |
| | 0 | 98 | | target.DataProtectionProvider = DataProtectionProvider; |
| | 0 | 99 | | target.SessionStore = SessionStore; |
| | | 100 | | |
| | | 101 | | // Events & issuer |
| | 0 | 102 | | if (Events is not null) |
| | | 103 | | { |
| | 0 | 104 | | target.Events = Events; |
| | | 105 | | } |
| | 0 | 106 | | target.EventsType = EventsType; |
| | 0 | 107 | | target.ClaimsIssuer = ClaimsIssuer; |
| | 0 | 108 | | } |
| | | 109 | | } |