Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e2d29261da | |||
| 5d5b59d610 | |||
| f001b02633 | |||
| 90a11dc65e | |||
| de955a96a8 | |||
| cdf5cfb5cd | |||
| c4d3bb4cdf | |||
| 65f102f18a | |||
| cdc80db214 | |||
| 4576b5aa2b | |||
| 3847927ace | |||
| d38d1dd059 | |||
| c27aba1954 | |||
| e646d16053 | |||
| 1c946dab26 | |||
| 20c3ad9569 | |||
| 9977cf27b9 | |||
| cf7eed0603 | |||
| 8e9ac1e1ad | |||
| fa79bd8021 | |||
| 16dae7c9fb | |||
| 5666ffd474 | |||
| f8153e86b4 | |||
| eef1096ec5 |
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
### Authentik Token Request (Service Account Explicit)
|
### Authentik Token Request (Service Account Explicit)
|
||||||
POST {{authority}}
|
POST {{authority}}/connect/token
|
||||||
Content-Type: application/x-www-form-urlencoded
|
Content-Type: application/x-www-form-urlencoded
|
||||||
Accept-Encoding: identity
|
Accept-Encoding: identity
|
||||||
|
|
||||||
grant_type={{grantType}}&client_id={{clientId}}&username={{username}}&password={{password}}&scope={{scope}}
|
grant_type={{grantType}}&client_id={{clientId}}&client_secret={{clientSecret}}&scope={{scope}}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"uat": {
|
||||||
|
"authority": "https://sts.security.khongisa.co.za",
|
||||||
|
"grantType": "client_credentials",
|
||||||
|
"clientId": "midrandbooks-api-uat",
|
||||||
|
"clientSecret": "secret_5a36d0024980544c875447a4b052938becc3fbbb10b8b2c097310c1a53ba3c0a",
|
||||||
|
"scope": "midrandbooks-api"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
namespace LiteCharms.Features.Api.Configuration;
|
|
||||||
|
|
||||||
public sealed class AuthentikSettings
|
|
||||||
{
|
|
||||||
public string? Authority { get; set; }
|
|
||||||
|
|
||||||
public string? IntrospectionUrl { get; set; }
|
|
||||||
|
|
||||||
public string? ApiResourceName { get; set; }
|
|
||||||
|
|
||||||
public string? ApiResourceSecret { get; set; }
|
|
||||||
|
|
||||||
public string? RequiredClaimName { get; set; }
|
|
||||||
|
|
||||||
public string? RequiredClaimNameValue { get; set; }
|
|
||||||
|
|
||||||
public bool RequireHttpsMetadata { get; set; }
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
namespace LiteCharms.Features.Api.Configuration;
|
||||||
|
|
||||||
|
public sealed class LiteCharmsSettings
|
||||||
|
{
|
||||||
|
public string? Authority { get; set; }
|
||||||
|
|
||||||
|
public string? ClientId { get; set; }
|
||||||
|
|
||||||
|
public string? ClientSecret { get; set; }
|
||||||
|
|
||||||
|
public string? Audience { get; set; }
|
||||||
|
|
||||||
|
public string? RequiredClaimName { get; set; }
|
||||||
|
|
||||||
|
public string? RequiredClaimNameValue { get; set; }
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using LiteCharms.Features.Abstractions;
|
using LiteCharms.Features.Abstractions;
|
||||||
using LiteCharms.Features.Api;
|
using LiteCharms.Features.Api;
|
||||||
using LiteCharms.Features.Api.Configuration;
|
using LiteCharms.Features.Api.Configuration;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
|
||||||
namespace LiteCharms.Features.Extensions;
|
namespace LiteCharms.Features.Extensions;
|
||||||
|
|
||||||
@@ -9,34 +10,109 @@ public static class Api
|
|||||||
public const string Books = nameof(Books);
|
public const string Books = nameof(Books);
|
||||||
public const string Payments = nameof(Payments);
|
public const string Payments = nameof(Payments);
|
||||||
|
|
||||||
public static IServiceCollection AddAuthentic(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddLiteCharmsUiSecurity(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var configSection = configuration.GetSection(nameof(AuthentikSettings));
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
||||||
|
|
||||||
var authOptions = new AuthentikSettings();
|
var authOptions = new LiteCharmsSettings();
|
||||||
configSection.Bind(authOptions);
|
configSection.Bind(authOptions);
|
||||||
|
|
||||||
services.Configure<AuthentikSettings>(configSection);
|
services.Configure<LiteCharmsSettings>(configSection);
|
||||||
|
|
||||||
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
|
services.AddAuthentication(options =>
|
||||||
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
|
{
|
||||||
{
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
options.Authority = authOptions.Authority;
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
||||||
options.IntrospectionEndpoint = authOptions.IntrospectionUrl;
|
})
|
||||||
options.ClientId = authOptions.ApiResourceName;
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||||
options.ClientSecret = authOptions.ApiResourceSecret;
|
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
|
||||||
|
{
|
||||||
|
options.Authority = authOptions.Authority;
|
||||||
|
|
||||||
options.NameClaimType = "sub";
|
options.ClientId = authOptions.ClientId;
|
||||||
options.DiscoveryPolicy.RequireHttps = authOptions.RequireHttpsMetadata;
|
options.ClientSecret = authOptions.ClientSecret;
|
||||||
options.DiscoveryPolicy.ValidateEndpoints = false;
|
options.SignedOutCallbackPath = "/signout-callback-oidc";
|
||||||
options.EnableCaching = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddAuthorization();
|
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;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddLiteCharmsApiSecurity(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var configSection = configuration.GetSection(nameof(LiteCharmsSettings));
|
||||||
|
|
||||||
|
var authOptions = new LiteCharmsSettings();
|
||||||
|
configSection.Bind(authOptions);
|
||||||
|
|
||||||
|
services.Configure<LiteCharmsSettings>(configSection);
|
||||||
|
|
||||||
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.Authority = authOptions.Authority;
|
||||||
|
options.Audience = authOptions.Audience;
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidIssuer = authOptions.Authority,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidateIssuer = true,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
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<LiteCharmsSettings> 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)
|
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
@@ -124,5 +200,4 @@ public static class Api
|
|||||||
|
|
||||||
public static string ToEndpointName(this Type target, string? annotation = "") =>
|
public static string ToEndpointName(this Type target, string? annotation = "") =>
|
||||||
$"{target.Name.Replace("Endpoint", string.Empty)}{annotation}".ToLower(CultureInfo.CurrentCulture);
|
$"{target.Name.Replace("Endpoint", string.Empty)}{annotation}".ToLower(CultureInfo.CurrentCulture);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,9 @@
|
|||||||
<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.Cookies"/>
|
||||||
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/>
|
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user