From 0a9e036e3efb94e56ce30a6fe6e3484c8fc0e9ec Mon Sep 17 00:00:00 2001 From: Andrew Israel Date: Tue, 18 Mar 2025 02:35:18 -0700 Subject: [PATCH] Allow setting custom/fallback policies in the authorization block --- PropelAuth/PropelAuthExtensions.cs | 9 ++++++++- PropelAuth/PropelAuthOptions.cs | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/PropelAuth/PropelAuthExtensions.cs b/PropelAuth/PropelAuthExtensions.cs index 1850fb4..6777d6a 100644 --- a/PropelAuth/PropelAuthExtensions.cs +++ b/PropelAuth/PropelAuthExtensions.cs @@ -165,7 +165,14 @@ private static void ConfigureAuthentication(IServiceCollection services, PropelA }); } - services.AddAuthorization(); + if (options.ConfigureAuthorization != null) + { + services.AddAuthorization(options.ConfigureAuthorization); + } + else + { + services.AddAuthorization(); + } } /// diff --git a/PropelAuth/PropelAuthOptions.cs b/PropelAuth/PropelAuthOptions.cs index 9edc63b..5ba64b2 100644 --- a/PropelAuth/PropelAuthOptions.cs +++ b/PropelAuth/PropelAuthOptions.cs @@ -1,3 +1,5 @@ +using Microsoft.AspNetCore.Authorization; + namespace PropelAuth.Models { /// @@ -30,6 +32,11 @@ public class PropelAuthOptions /// public OAuthOptions? OAuthOptions { get; } + /// + /// Gets the action to configure authorization options including custom policies. + /// + public Action? ConfigureAuthorization { get; } + #endregion #region Constructors @@ -41,13 +48,15 @@ public class PropelAuthOptions /// The API key used for authenticating requests to PropelAuth. /// Optional. The public key used for token verification. /// Optional. The OAuth options if you are using PropelAuth's OAuth feature. + /// Optional. Action to configure authorization options including custom policies. public PropelAuthOptions(string authUrl, string apiKey, string? publicKey = null, - OAuthOptions? oAuthOptions = null) + OAuthOptions? oAuthOptions = null, Action? configureAuthorization = null) { AuthUrl = authUrl; ApiKey = apiKey; PublicKey = publicKey; OAuthOptions = oAuthOptions; + ConfigureAuthorization = configureAuthorization; } #endregion @@ -56,7 +65,7 @@ public PropelAuthOptions(string authUrl, string apiKey, string? publicKey = null public class OAuthOptions { #region Properties - + /// /// The client ID for the OAuth application. /// @@ -71,16 +80,16 @@ public class OAuthOptions /// The callback path for the OAuth application. Defaults to "/callback" /// public string? CallbackPath { get; } - + /// /// Whether to allow requests via an authorization header `Bearer {TOKEN}`. Default false. /// public bool? AllowBearerTokenAuth { get; } - + #endregion #region Constructor - + /// /// Initializes a new instance of the class. /// @@ -88,7 +97,8 @@ public class OAuthOptions /// The client secret for the OAuth application. /// Optional. The callback path for the OAuth application. Defaults to "/callback" /// Optional. Whether to allow requests via an authorization header `Bearer {TOKEN}`. Default false. - public OAuthOptions(string clientId, string clientSecret, string? callbackPath = "/callback", bool? allowBearerTokenAuth = false) + public OAuthOptions(string clientId, string clientSecret, string? callbackPath = "/callback", + bool? allowBearerTokenAuth = false) { ClientId = clientId; ClientSecret = clientSecret; @@ -97,6 +107,5 @@ public OAuthOptions(string clientId, string clientSecret, string? callbackPath = } #endregion - } } \ No newline at end of file