diff --git a/LiteCharms.Features.Tests/http/authentik/app.http b/LiteCharms.Features.Tests/http/authentik/app.http new file mode 100644 index 0000000..93b1bc2 --- /dev/null +++ b/LiteCharms.Features.Tests/http/authentik/app.http @@ -0,0 +1,6 @@ +## Authentik Token Request +POST {{authority}} +Content-Type: application/x-www-form-urlencoded +Accept-Encoding: identity + +grant_type={{grantType}}&client_id={{clientId}}&client_secret={{clientSecret}}&username={{username}}&password={{password}}&scope={{scope}} \ No newline at end of file diff --git a/LiteCharms.Features/Extensions/Api.cs b/LiteCharms.Features/Extensions/Api.cs index 8bb3b69..bf73b01 100644 --- a/LiteCharms.Features/Extensions/Api.cs +++ b/LiteCharms.Features/Extensions/Api.cs @@ -11,26 +11,31 @@ public static class Api public static IServiceCollection AddAuthentic(this IServiceCollection services, IConfiguration configuration) { + var configSection = configuration.GetSection(nameof(AuthentikSettings)); + var authOptions = new AuthentikSettings(); + configSection.Bind(authOptions); - configuration.GetSection("Authentik").Bind(authOptions); - - services.Configure(configuration.GetSection(nameof(AuthentikSettings))); + services.Configure(configSection); services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme) - .AddOAuth2Introspection(options => + .AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options => { - options.Authority = options.Authority; - options.ClientId = options.ClientId; - options.ClientSecret = options.ClientSecret; + options.Authority = authOptions.Authority; + options.ClientId = authOptions.ApiResourceName; + options.ClientSecret = authOptions.ApiResourceSecret; + options.DiscoveryPolicy.RequireHttps = authOptions.RequireHttpsMetadata; options.EnableCaching = true; options.CacheDuration = TimeSpan.FromMinutes(10); }); if (!string.IsNullOrWhiteSpace(authOptions.RequiredClaimName) && !string.IsNullOrWhiteSpace(authOptions.RequiredClaimNameValue)) - services.AddAuthorizationBuilder().AddPolicy("ApiScope", policy => + { + services.AddAuthorizationBuilder() + .AddPolicy("ApiScope", policy => policy.RequireClaim(authOptions.RequiredClaimName, authOptions.RequiredClaimNameValue)); + } else services.AddAuthorization();