| | | 1 | | using Kestrun.Claims; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | using Microsoft.AspNetCore.Authentication; |
| | | 4 | | using Microsoft.AspNetCore.Authentication.Cookies; |
| | | 5 | | using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Authentication; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Options for OpenID Connect authentication. |
| | | 11 | | /// </summary> |
| | | 12 | | public class OidcOptions : OpenIdConnectOptions, IOpenApiAuthenticationOptions, IAuthenticationHostOptions, IOAuthCommon |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Options for cookie authentication. |
| | | 16 | | /// </summary> |
| | 5 | 17 | | public CookieAuthOptions CookieOptions { get; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the cookie authentication scheme name. |
| | | 21 | | /// </summary> |
| | | 22 | | public string CookieScheme => |
| | 2 | 23 | | CookieOptions.Cookie.Name ?? (CookieAuthenticationDefaults.AuthenticationScheme + "." + AuthenticationScheme); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// JSON Web Key (JWK) for token validation. |
| | | 27 | | /// </summary> |
| | 7 | 28 | | public string? JwkJson { get; set; } |
| | | 29 | | |
| | | 30 | | #if NET8_0 |
| | | 31 | | /// <summary> |
| | | 32 | | /// Additional authorization parameters to include in the authorization request. |
| | | 33 | | /// </summary> |
| | 11 | 34 | | public string PushedAuthorizationBehavior { get; set; } = "Disable"; |
| | | 35 | | #endif |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | 2 | 38 | | public bool GlobalScheme { get; set; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | 2 | 41 | | public string? Description { get; set; } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | 2 | 44 | | public string? DisplayName { get; set; } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | 13 | 47 | | public string[] DocumentationId { get; set; } = []; |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | #pragma warning disable IDE0370 // Remove unnecessary suppression |
| | 2 | 51 | | public KestrunHost Host { get; set; } = default!; |
| | | 52 | | #pragma warning restore IDE0370 // Remove unnecessary suppression |
| | | 53 | | |
| | | 54 | | private Serilog.ILogger? _logger; |
| | | 55 | | /// <inheritdoc/> |
| | | 56 | | public Serilog.ILogger Logger |
| | | 57 | | { |
| | 0 | 58 | | get => _logger ?? (Host is null ? Serilog.Log.Logger : Host.Logger); set => _logger = value; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Initializes a new instance of the <see cref="OidcOptions"/> class. |
| | | 63 | | /// </summary> |
| | 11 | 64 | | public OidcOptions() |
| | | 65 | | { |
| | 11 | 66 | | CookieOptions = new CookieAuthOptions() |
| | 11 | 67 | | { |
| | 11 | 68 | | SlidingExpiration = true |
| | 11 | 69 | | }; |
| | 11 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets or sets the authentication scheme name. |
| | | 74 | | /// </summary> |
| | 13 | 75 | | public string AuthenticationScheme { get; set; } = AuthenticationDefaults.OidcSchemeName; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Configuration for claim policy enforcement. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | public ClaimPolicyConfig? ClaimPolicy { get; set; } |
| | | 81 | | /// <summary> |
| | | 82 | | /// Helper to copy values from a user-supplied OidcOptions instance to the instance |
| | | 83 | | /// created by the framework inside AddOpenIdConnect(). |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="target">The target options to copy to.</param> |
| | | 86 | | public void ApplyTo(OidcOptions target) |
| | | 87 | | { |
| | 2 | 88 | | ApplyTo((OpenIdConnectOptions)target); |
| | 1 | 89 | | target.JwkJson = JwkJson; |
| | | 90 | | // OpenAPI / documentation properties |
| | 1 | 91 | | target.GlobalScheme = GlobalScheme; |
| | 1 | 92 | | target.Description = Description; |
| | 1 | 93 | | target.DisplayName = DisplayName; |
| | 1 | 94 | | target.DocumentationId = DocumentationId; |
| | 1 | 95 | | target.Host = Host; |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Helper to copy values from a user-supplied OpenIdConnectOptions instance to the instance |
| | | 100 | | /// created by the framework inside AddOpenIdConnect(). |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="target">The target options to copy to.</param> |
| | | 103 | | public void ApplyTo(OpenIdConnectOptions target) |
| | | 104 | | { |
| | 7 | 105 | | CopyCoreEndpoints(target); |
| | 5 | 106 | | CopyFlowConfiguration(target); |
| | 5 | 107 | | CopyScopes(target); |
| | 5 | 108 | | CopyTokenHandling(target); |
| | 5 | 109 | | CopyPaths(target); |
| | 5 | 110 | | CopyTokenValidation(target); |
| | 5 | 111 | | CopySchemeLinkage(target); |
| | 5 | 112 | | CopyBackchannelConfiguration(target); |
| | 5 | 113 | | CopyConfiguration(target); |
| | 5 | 114 | | CopyClaimActions(target); |
| | 5 | 115 | | CopyEvents(target); |
| | 5 | 116 | | CopyIssuerAndProperties(target); |
| | | 117 | | #if NET9_0_OR_GREATER |
| | | 118 | | CopyNet9Features(target); |
| | | 119 | | #endif |
| | 5 | 120 | | } |
| | | 121 | | |
| | | 122 | | private void CopyCoreEndpoints(OpenIdConnectOptions target) |
| | | 123 | | { |
| | 7 | 124 | | target.Authority = Authority; |
| | 5 | 125 | | target.ClientId = ClientId; |
| | 5 | 126 | | target.ClientSecret = ClientSecret; |
| | 5 | 127 | | } |
| | | 128 | | |
| | | 129 | | private void CopyFlowConfiguration(OpenIdConnectOptions target) |
| | | 130 | | { |
| | 5 | 131 | | target.ResponseType = ResponseType; |
| | 5 | 132 | | target.ResponseMode = ResponseMode; |
| | 5 | 133 | | target.UsePkce = UsePkce; |
| | 5 | 134 | | target.RequireHttpsMetadata = RequireHttpsMetadata; |
| | 5 | 135 | | } |
| | | 136 | | |
| | | 137 | | private void CopyScopes(OpenIdConnectOptions target) |
| | | 138 | | { |
| | 5 | 139 | | target.Scope.Clear(); |
| | 32 | 140 | | foreach (var scope in Scope) |
| | | 141 | | { |
| | 11 | 142 | | target.Scope.Add(scope); |
| | | 143 | | } |
| | 5 | 144 | | } |
| | | 145 | | |
| | | 146 | | private void CopyTokenHandling(OpenIdConnectOptions target) |
| | | 147 | | { |
| | 5 | 148 | | target.SaveTokens = SaveTokens; |
| | 5 | 149 | | target.GetClaimsFromUserInfoEndpoint = GetClaimsFromUserInfoEndpoint; |
| | 5 | 150 | | target.MapInboundClaims = MapInboundClaims; |
| | 5 | 151 | | target.UseSecurityTokenValidator = UseSecurityTokenValidator; |
| | 5 | 152 | | } |
| | | 153 | | |
| | | 154 | | private void CopyPaths(OpenIdConnectOptions target) |
| | | 155 | | { |
| | 5 | 156 | | target.CallbackPath = CallbackPath; |
| | 5 | 157 | | target.SignedOutCallbackPath = SignedOutCallbackPath; |
| | 5 | 158 | | target.SignedOutRedirectUri = SignedOutRedirectUri; |
| | 5 | 159 | | target.RemoteSignOutPath = RemoteSignOutPath; |
| | 5 | 160 | | } |
| | | 161 | | |
| | | 162 | | private void CopyTokenValidation(OpenIdConnectOptions target) |
| | | 163 | | { |
| | 5 | 164 | | if (TokenValidationParameters != null) |
| | | 165 | | { |
| | 5 | 166 | | target.TokenValidationParameters = TokenValidationParameters; |
| | | 167 | | } |
| | 5 | 168 | | } |
| | | 169 | | |
| | | 170 | | private void CopySchemeLinkage(OpenIdConnectOptions target) |
| | | 171 | | { |
| | 5 | 172 | | target.SignInScheme = SignInScheme; |
| | 5 | 173 | | target.SignOutScheme = SignOutScheme; |
| | 5 | 174 | | } |
| | | 175 | | |
| | | 176 | | private void CopyBackchannelConfiguration(OpenIdConnectOptions target) |
| | | 177 | | { |
| | 5 | 178 | | if (Backchannel != null) |
| | | 179 | | { |
| | 0 | 180 | | target.Backchannel = Backchannel; |
| | | 181 | | } |
| | 5 | 182 | | if (BackchannelHttpHandler != null) |
| | | 183 | | { |
| | 0 | 184 | | target.BackchannelHttpHandler = BackchannelHttpHandler; |
| | | 185 | | } |
| | 5 | 186 | | if (BackchannelTimeout != default) |
| | | 187 | | { |
| | 5 | 188 | | target.BackchannelTimeout = BackchannelTimeout; |
| | | 189 | | } |
| | 5 | 190 | | } |
| | | 191 | | |
| | | 192 | | private void CopyConfiguration(OpenIdConnectOptions target) |
| | | 193 | | { |
| | 5 | 194 | | if (Configuration != null) |
| | | 195 | | { |
| | 0 | 196 | | target.Configuration = Configuration; |
| | | 197 | | } |
| | 5 | 198 | | if (ConfigurationManager != null) |
| | | 199 | | { |
| | 0 | 200 | | target.ConfigurationManager = ConfigurationManager; |
| | | 201 | | } |
| | 5 | 202 | | } |
| | | 203 | | |
| | | 204 | | private void CopyClaimActions(OpenIdConnectOptions target) |
| | | 205 | | { |
| | 5 | 206 | | if (ClaimActions == null) |
| | | 207 | | { |
| | 0 | 208 | | return; |
| | | 209 | | } |
| | 74 | 210 | | foreach (var jka in ClaimActions |
| | 5 | 211 | | .OfType<Microsoft.AspNetCore.Authentication.OAuth.Claims.JsonKeyClaimAction>() |
| | 37 | 212 | | .Where(a => !string.IsNullOrEmpty(a.JsonKey) && !string.IsNullOrEmpty(a.ClaimType))) |
| | | 213 | | { |
| | 32 | 214 | | target.ClaimActions.MapJsonKey(jka.ClaimType, jka.JsonKey); |
| | | 215 | | } |
| | 5 | 216 | | } |
| | | 217 | | |
| | | 218 | | private void CopyEvents(OpenIdConnectOptions target) |
| | | 219 | | { |
| | 5 | 220 | | if (Events != null) |
| | | 221 | | { |
| | 5 | 222 | | target.Events = Events; |
| | | 223 | | } |
| | 5 | 224 | | if (EventsType != null) |
| | | 225 | | { |
| | 0 | 226 | | target.EventsType = EventsType; |
| | | 227 | | } |
| | 5 | 228 | | } |
| | | 229 | | |
| | | 230 | | private void CopyIssuerAndProperties(OpenIdConnectOptions target) |
| | | 231 | | { |
| | 5 | 232 | | target.ClaimsIssuer = ClaimsIssuer; |
| | 5 | 233 | | target.DisableTelemetry = DisableTelemetry; |
| | 5 | 234 | | target.MaxAge = MaxAge; |
| | 5 | 235 | | target.ProtocolValidator = ProtocolValidator; |
| | 5 | 236 | | target.RefreshOnIssuerKeyNotFound = RefreshOnIssuerKeyNotFound; |
| | 5 | 237 | | target.Resource = Resource; |
| | 5 | 238 | | target.SkipUnrecognizedRequests = SkipUnrecognizedRequests; |
| | 5 | 239 | | target.StateDataFormat = StateDataFormat; |
| | 5 | 240 | | target.StringDataFormat = StringDataFormat; |
| | 5 | 241 | | } |
| | | 242 | | |
| | | 243 | | #if NET9_0_OR_GREATER |
| | | 244 | | private void CopyNet9Features(OpenIdConnectOptions target) |
| | | 245 | | { |
| | | 246 | | target.PushedAuthorizationBehavior = PushedAuthorizationBehavior; |
| | | 247 | | if (AdditionalAuthorizationParameters != null) |
| | | 248 | | { |
| | | 249 | | foreach (var param in AdditionalAuthorizationParameters) |
| | | 250 | | { |
| | | 251 | | target.AdditionalAuthorizationParameters[param.Key] = param.Value; |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | } |
| | | 255 | | #endif |
| | | 256 | | } |