|
|
|
@@ -9,7 +9,52 @@ public static class Api
|
|
|
|
|
public const string Books = nameof(Books);
|
|
|
|
|
public const string Payments = nameof(Payments);
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddAuthentic(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
public static IServiceCollection AddAuthentikUiSecurity(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var configSection = configuration.GetSection(nameof(AuthentikSettings));
|
|
|
|
|
|
|
|
|
|
var authOptions = new AuthentikSettings();
|
|
|
|
|
configSection.Bind(authOptions);
|
|
|
|
|
|
|
|
|
|
services.Configure<AuthentikSettings>(configSection);
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = authOptions.Authority;
|
|
|
|
|
options.MetadataAddress = authOptions.MetadataEndpoint;
|
|
|
|
|
|
|
|
|
|
options.ClientId = authOptions.ClientId;
|
|
|
|
|
options.ClientSecret = authOptions.ClientSecret;
|
|
|
|
|
options.SignedOutCallbackPath = "/signout-callback-oidc";
|
|
|
|
|
|
|
|
|
|
options.ResponseType = "code";
|
|
|
|
|
options.SaveTokens = true;
|
|
|
|
|
options.GetClaimsFromUserInfoEndpoint = true;
|
|
|
|
|
|
|
|
|
|
options.Scope.Clear();
|
|
|
|
|
options.Scope.Add("openid");
|
|
|
|
|
options.Scope.Add("profile");
|
|
|
|
|
options.Scope.Add("email");
|
|
|
|
|
|
|
|
|
|
options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
options.CorrelationCookie.SameSite = SameSiteMode.None;
|
|
|
|
|
options.CorrelationCookie.HttpOnly = true;
|
|
|
|
|
|
|
|
|
|
options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
options.NonceCookie.SameSite = SameSiteMode.None;
|
|
|
|
|
options.NonceCookie.HttpOnly = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddAuthentikApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var configSection = configuration.GetSection(nameof(AuthentikSettings));
|
|
|
|
|
|
|
|
|
@@ -22,21 +67,53 @@ public static class Api
|
|
|
|
|
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = authOptions.Authority;
|
|
|
|
|
options.IntrospectionEndpoint = authOptions.IntrospectionUrl;
|
|
|
|
|
options.ClientId = authOptions.ApiResourceName;
|
|
|
|
|
options.ClientSecret = authOptions.ApiResourceSecret;
|
|
|
|
|
options.IntrospectionEndpoint = authOptions.IntrospectionEndpoint;
|
|
|
|
|
options.ClientId = authOptions.ClientId;
|
|
|
|
|
options.ClientSecret = authOptions.ClientSecret;
|
|
|
|
|
|
|
|
|
|
options.NameClaimType = "sub";
|
|
|
|
|
options.DiscoveryPolicy.RequireHttps = authOptions.RequireHttpsMetadata;
|
|
|
|
|
options.DiscoveryPolicy.ValidateEndpoints = false;
|
|
|
|
|
options.EnableCaching = false;
|
|
|
|
|
options.EnableCaching = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddAuthorization();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(authOptions.RequiredClaimName) && !string.IsNullOrWhiteSpace(authOptions.RequiredClaimNameValue))
|
|
|
|
|
{
|
|
|
|
|
services.AddAuthorizationBuilder()
|
|
|
|
|
.AddPolicy("RequiredScope", policy =>
|
|
|
|
|
policy.RequireClaim(authOptions.RequiredClaimName, authOptions.RequiredClaimNameValue));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static WebApplication AddSecurityEndpoints(this WebApplication app)
|
|
|
|
|
{
|
|
|
|
|
app.MapGet("/login", async (HttpContext context, string redirectUri = "/") =>
|
|
|
|
|
{
|
|
|
|
|
await context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
|
|
|
|
|
{
|
|
|
|
|
RedirectUri = redirectUri,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.MapGet("/logout", async (HttpContext context, IHttpClientFactory httpClientFactory, IOptions<AuthentikSettings> settings) =>
|
|
|
|
|
{
|
|
|
|
|
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
|
|
|
|
|
string currentBaseUrl = $"https://{context.Request.Host}{context.Request.PathBase}/";
|
|
|
|
|
|
|
|
|
|
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties
|
|
|
|
|
{
|
|
|
|
|
RedirectUri = currentBaseUrl
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddHttpClient();
|
|
|
|
@@ -124,5 +201,4 @@ public static class Api
|
|
|
|
|
|
|
|
|
|
public static string ToEndpointName(this Type target, string? annotation = "") =>
|
|
|
|
|
$"{target.Name.Replace("Endpoint", string.Empty)}{annotation}".ToLower(CultureInfo.CurrentCulture);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|