| | | 1 | | |
| | | 2 | | using Kestrun.Claims; |
| | | 3 | | using Kestrun.Hosting; |
| | | 4 | | using Microsoft.AspNetCore.Authentication; |
| | | 5 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 6 | | using Microsoft.AspNetCore.Authentication.OAuth; |
| | | 7 | | |
| | | 8 | | namespace Kestrun.Authentication; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Options for OAuth2 authentication. |
| | | 12 | | /// </summary> |
| | | 13 | | public class OAuth2Options : OAuthOptions, IOpenApiAuthenticationOptions, IAuthenticationHostOptions, IOAuthCommonOption |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Options for cookie authentication. |
| | | 17 | | /// </summary> |
| | 5 | 18 | | public CookieAuthOptions CookieOptions { get; } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc/> |
| | 0 | 21 | | public bool GlobalScheme { get; set; } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | 0 | 24 | | public string? Description { get; set; } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | 0 | 27 | | public string? DisplayName { get; set; } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc/> |
| | 12 | 30 | | public string[] DocumentationId { get; set; } = []; |
| | | 31 | | |
| | | 32 | | /// <inheritdoc/> |
| | | 33 | | #pragma warning disable IDE0370 // Remove unnecessary suppression |
| | 0 | 34 | | public KestrunHost Host { get; set; } = default!; |
| | | 35 | | #pragma warning restore IDE0370 // Remove unnecessary suppression |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="OAuth2Options"/> class. |
| | | 39 | | /// </summary> |
| | 12 | 40 | | public OAuth2Options() |
| | | 41 | | { |
| | 12 | 42 | | CookieOptions = new CookieAuthOptions() |
| | 12 | 43 | | { |
| | 12 | 44 | | SlidingExpiration = true |
| | 12 | 45 | | }; |
| | 12 | 46 | | } |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the authentication scheme name. |
| | | 49 | | /// </summary> |
| | 13 | 50 | | public string AuthenticationScheme { get; set; } = AuthenticationDefaults.OAuth2SchemeName; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the cookie authentication scheme name. |
| | | 54 | | /// </summary> |
| | | 55 | | public string CookieScheme => |
| | 2 | 56 | | CookieOptions.Cookie.Name ?? (CookieAuthenticationDefaults.AuthenticationScheme + "." + AuthenticationScheme); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Configuration for claim policy enforcement. |
| | | 60 | | /// </summary> |
| | 10 | 61 | | public ClaimPolicyConfig? ClaimPolicy { get; set; } |
| | | 62 | | |
| | | 63 | | private Serilog.ILogger? _logger; |
| | | 64 | | /// <inheritdoc/> |
| | | 65 | | public Serilog.ILogger Logger |
| | | 66 | | { |
| | 0 | 67 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Helper to copy values from a user-supplied OAuth2Options instance to the instance |
| | | 72 | | /// created by the framework inside AddOAuth(). Reassigning the local variable (opts = source) would |
| | | 73 | | /// not work because only the local reference changes – the framework keeps the original instance. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="target">The target OAuth2Options instance to copy values to.</param> |
| | | 76 | | public void ApplyTo(OAuth2Options target) |
| | | 77 | | { |
| | 1 | 78 | | ApplyTo((OAuthOptions)target); |
| | 0 | 79 | | CookieOptions.ApplyTo(target.CookieOptions); |
| | | 80 | | // OpenAPI / documentation properties |
| | 0 | 81 | | target.GlobalScheme = GlobalScheme; |
| | 0 | 82 | | target.Description = Description; |
| | 0 | 83 | | target.DisplayName = DisplayName; |
| | 0 | 84 | | target.DocumentationId = DocumentationId; |
| | 0 | 85 | | target.Host = Host; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Apply these options to the target <see cref="OAuthOptions"/> instance. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="target">The target OAuthOptions instance to apply settings to.</param> |
| | | 92 | | public void ApplyTo(OAuthOptions target) |
| | | 93 | | { |
| | | 94 | | // Core OAuth endpoints |
| | 6 | 95 | | target.AuthorizationEndpoint = AuthorizationEndpoint; |
| | 5 | 96 | | target.TokenEndpoint = TokenEndpoint; |
| | 5 | 97 | | target.UserInformationEndpoint = UserInformationEndpoint; |
| | 5 | 98 | | target.ClientId = ClientId; |
| | 5 | 99 | | target.ClientSecret = ClientSecret; |
| | 5 | 100 | | target.CallbackPath = CallbackPath; |
| | | 101 | | |
| | | 102 | | // OAuth configuration |
| | 5 | 103 | | target.UsePkce = UsePkce; |
| | 5 | 104 | | target.SaveTokens = SaveTokens; |
| | 5 | 105 | | target.ClaimsIssuer = ClaimsIssuer; |
| | | 106 | | |
| | | 107 | | // Scopes - clear and copy |
| | 5 | 108 | | target.Scope.Clear(); |
| | 14 | 109 | | foreach (var scope in Scope) |
| | | 110 | | { |
| | 2 | 111 | | target.Scope.Add(scope); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Token handling |
| | 5 | 115 | | target.AccessDeniedPath = AccessDeniedPath; |
| | 5 | 116 | | target.RemoteAuthenticationTimeout = RemoteAuthenticationTimeout; |
| | 5 | 117 | | target.ReturnUrlParameter = ReturnUrlParameter; |
| | | 118 | | |
| | | 119 | | // Scheme linkage |
| | 5 | 120 | | target.SignInScheme = SignInScheme; |
| | | 121 | | |
| | | 122 | | // Backchannel configuration |
| | 5 | 123 | | if (Backchannel != null) |
| | | 124 | | { |
| | 0 | 125 | | target.Backchannel = Backchannel; |
| | | 126 | | } |
| | 5 | 127 | | if (BackchannelHttpHandler != null) |
| | | 128 | | { |
| | 0 | 129 | | target.BackchannelHttpHandler = BackchannelHttpHandler; |
| | | 130 | | } |
| | 5 | 131 | | if (BackchannelTimeout != default) |
| | | 132 | | { |
| | 5 | 133 | | target.BackchannelTimeout = BackchannelTimeout; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // Claim actions |
| | 5 | 137 | | if (ClaimActions != null) |
| | | 138 | | { |
| | 14 | 139 | | foreach (var jka in ClaimActions |
| | 5 | 140 | | .OfType<Microsoft.AspNetCore.Authentication.OAuth.Claims.JsonKeyClaimAction>() |
| | 7 | 141 | | .Where(a => !string.IsNullOrEmpty(a.JsonKey) && !string.IsNullOrEmpty(a.ClaimType))) |
| | | 142 | | { |
| | 2 | 143 | | target.ClaimActions.MapJsonKey(jka.ClaimType, jka.JsonKey); |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | // Events - copy if provided |
| | 5 | 148 | | if (Events != null) |
| | | 149 | | { |
| | 5 | 150 | | target.Events = Events; |
| | | 151 | | } |
| | 5 | 152 | | if (EventsType != null) |
| | | 153 | | { |
| | 0 | 154 | | target.EventsType = EventsType; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // Other properties |
| | 5 | 158 | | target.StateDataFormat = StateDataFormat; |
| | 5 | 159 | | } |
| | | 160 | | } |