Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c946dab26 | |||
| 20c3ad9569 | |||
| 9977cf27b9 | |||
| cf7eed0603 | |||
| 8e9ac1e1ad | |||
| fa79bd8021 |
@@ -4,7 +4,11 @@ public sealed class AuthentikSettings
|
|||||||
{
|
{
|
||||||
public string? Authority { get; set; }
|
public string? Authority { get; set; }
|
||||||
|
|
||||||
public string? IntrospectionUrl { get; set; }
|
public string? IntrospectionEndpoint { get; set; }
|
||||||
|
|
||||||
|
public string? MetadataEndpoint { get; set; }
|
||||||
|
|
||||||
|
public string? RevokationEndpoint { get; set; }
|
||||||
|
|
||||||
public string? ClientId { get; set; }
|
public string? ClientId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -27,9 +27,11 @@ public static class Api
|
|||||||
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
||||||
{
|
{
|
||||||
options.Authority = authOptions.Authority;
|
options.Authority = authOptions.Authority;
|
||||||
|
options.MetadataAddress = authOptions.MetadataEndpoint;
|
||||||
|
|
||||||
options.ClientId = authOptions.ClientId;
|
options.ClientId = authOptions.ClientId;
|
||||||
options.ClientSecret = authOptions.ClientSecret;
|
options.ClientSecret = authOptions.ClientSecret;
|
||||||
|
options.SignedOutCallbackPath = "/signout-callback-oidc";
|
||||||
|
|
||||||
options.ResponseType = "code";
|
options.ResponseType = "code";
|
||||||
options.SaveTokens = true;
|
options.SaveTokens = true;
|
||||||
@@ -57,7 +59,7 @@ public static class Api
|
|||||||
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
|
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
|
||||||
{
|
{
|
||||||
options.Authority = authOptions.Authority;
|
options.Authority = authOptions.Authority;
|
||||||
options.IntrospectionEndpoint = authOptions.IntrospectionUrl;
|
options.IntrospectionEndpoint = authOptions.IntrospectionEndpoint;
|
||||||
options.ClientId = authOptions.ClientId;
|
options.ClientId = authOptions.ClientId;
|
||||||
options.ClientSecret = authOptions.ClientSecret;
|
options.ClientSecret = authOptions.ClientSecret;
|
||||||
|
|
||||||
@@ -79,6 +81,47 @@ public static class Api
|
|||||||
return services;
|
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) =>
|
||||||
|
{
|
||||||
|
var authOptions = settings.Value;
|
||||||
|
var accessToken = await context.GetTokenAsync("access_token");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(accessToken))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var client = httpClientFactory.CreateClient();
|
||||||
|
|
||||||
|
var requestContent = new FormUrlEncodedContent(new Dictionary<string, string>(StringComparer.Ordinal)
|
||||||
|
{
|
||||||
|
{ "token", accessToken },
|
||||||
|
{ "client_id", authOptions.ClientId! },
|
||||||
|
{ "client_secret", authOptions.ClientSecret! },
|
||||||
|
});
|
||||||
|
|
||||||
|
await client.PostAsync(authOptions.RevokationEndpoint, requestContent, context.RequestAborted);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||||
|
|
||||||
|
return Results.Redirect("/");
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|||||||
@@ -38,6 +38,7 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" />
|
||||||
|
|
||||||
|
<Using Include="Microsoft.AspNetCore.Authentication"/>
|
||||||
<Using Include="Microsoft.AspNetCore.Authentication.OpenIdConnect"/>
|
<Using Include="Microsoft.AspNetCore.Authentication.OpenIdConnect"/>
|
||||||
<Using Include="Microsoft.AspNetCore.Authentication.Cookies"/>
|
<Using Include="Microsoft.AspNetCore.Authentication.Cookies"/>
|
||||||
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/>
|
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user