Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3847927ace | |||
| d38d1dd059 | |||
| c27aba1954 | |||
| e646d16053 | |||
| 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;
|
||||||
@@ -39,6 +41,25 @@ public static class Api
|
|||||||
options.Scope.Add("openid");
|
options.Scope.Add("openid");
|
||||||
options.Scope.Add("profile");
|
options.Scope.Add("profile");
|
||||||
options.Scope.Add("email");
|
options.Scope.Add("email");
|
||||||
|
|
||||||
|
options.Events = new OpenIdConnectEvents
|
||||||
|
{
|
||||||
|
OnRedirectToIdentityProvider = context =>
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(context.ProtocolMessage.RedirectUri) && context.ProtocolMessage.RedirectUri.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var uriBuilder = new UriBuilder(context.ProtocolMessage.RedirectUri)
|
||||||
|
{
|
||||||
|
Scheme = "https",
|
||||||
|
Port = -1,
|
||||||
|
};
|
||||||
|
|
||||||
|
context.ProtocolMessage.RedirectUri = uriBuilder.Uri.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
@@ -57,7 +78,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 +100,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($"{authOptions.Authority}end-session/");
|
||||||
|
});
|
||||||
|
|
||||||
|
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