Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aff6fcabf4 | |||
| a50830ffaa | |||
| ee6f8a283e | |||
| 8140b5fe65 | |||
| fda97db5fa | |||
| 9285cedfa9 | |||
| 29574f4df0 | |||
| 343874551a | |||
| b4a48c9cbf |
@@ -363,3 +363,4 @@ MigrationBackup/
|
||||
FodyWeavers.xsd
|
||||
/LiteCharms.Features.Tests/http/http-client.env.json
|
||||
/LiteCharms.Features.Tests/http/midrandshop-api/http-client.env.json
|
||||
/LiteCharms.Features.Tests/http/authentik/http-client.env.json
|
||||
|
||||
@@ -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}}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace LiteCharms.Features.Api.Configuration;
|
||||
|
||||
public sealed class AuthentikSettings
|
||||
{
|
||||
public string? Authority { 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; }
|
||||
|
||||
public bool BypassSslErrors { get; set; }
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public sealed class OpenApiBearerSecuritySchemeTransformer : IOpenApiDocumentTra
|
||||
{
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
|
||||
Description = "JWT Authorization header using the Bearer scheme",
|
||||
};
|
||||
|
||||
document.AddComponent("Bearer", bearerScheme);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using LiteCharms.Features.Abstractions;
|
||||
using LiteCharms.Features.Api;
|
||||
using LiteCharms.Features.Api.Configuration;
|
||||
|
||||
namespace LiteCharms.Features.Extensions;
|
||||
|
||||
@@ -8,6 +9,91 @@ 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)
|
||||
{
|
||||
var configSection = configuration.GetSection(nameof(AuthentikSettings));
|
||||
|
||||
var authOptions = new AuthentikSettings();
|
||||
configSection.Bind(authOptions);
|
||||
|
||||
services.Configure<AuthentikSettings>(configSection);
|
||||
|
||||
services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
|
||||
.AddOAuth2Introspection(OAuth2IntrospectionDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
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 =>
|
||||
policy.RequireClaim(authOptions.RequiredClaimName, authOptions.RequiredClaimNameValue));
|
||||
}
|
||||
else
|
||||
services.AddAuthorization();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHttpClient();
|
||||
|
||||
services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1);
|
||||
options.ReportApiVersions = true;
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
|
||||
new QueryStringApiVersionReader("version"),
|
||||
new QueryStringApiVersionReader("version"),
|
||||
new MediaTypeApiVersionReader("version"));
|
||||
})
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
var urls = configuration["ASPNETCORE_URLS"] ?? configuration["Urls"];
|
||||
var healthUrl = "http://localhost:8080/health";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(urls))
|
||||
{
|
||||
string firstUrl = urls.Split(';').FirstOrDefault(s => s.Contains("http://"))!
|
||||
.Replace("0.0.0.0", "localhost")
|
||||
.Replace("*", "localhost")
|
||||
.Replace("+", "localhost");
|
||||
|
||||
healthUrl = $"{firstUrl.TrimEnd('/')}/health";
|
||||
}
|
||||
|
||||
services.AddHealthChecksUI(setup =>
|
||||
{
|
||||
setup.SetNotifyUnHealthyOneTimeUntilChange();
|
||||
setup.AddHealthCheckEndpoint("primary, heal", healthUrl);
|
||||
setup.SetHeaderText("Midrand Books");
|
||||
})
|
||||
.AddInMemoryStorage();
|
||||
|
||||
services.AddOutputCache(options =>
|
||||
{
|
||||
options.AddBasePolicy(builder => builder.Cache());
|
||||
options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(10);
|
||||
});
|
||||
|
||||
services.AddOpenApi(options => options.AddDocumentTransformer<OpenApiBearerSecuritySchemeTransformer>());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder MapEndpoints(this WebApplication app, IDictionary<int, RouteGroupBuilder> versionGroups)
|
||||
{
|
||||
var endpoints = app.Services.GetRequiredService<IEnumerable<IEndpoint>>();
|
||||
@@ -44,53 +130,4 @@ public static class Api
|
||||
public static string ToEndpointName(this Type target, string? annotation = "") =>
|
||||
$"{target.Name.Replace("Endpoint", string.Empty)}{annotation}".ToLower(CultureInfo.CurrentCulture);
|
||||
|
||||
public static IServiceCollection AddApiServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHttpClient();
|
||||
|
||||
services.AddApiVersioning(options =>
|
||||
{
|
||||
options.DefaultApiVersion = new ApiVersion(1);
|
||||
options.ReportApiVersions = true;
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
options.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
|
||||
new QueryStringApiVersionReader("version"),
|
||||
new QueryStringApiVersionReader("version"),
|
||||
new MediaTypeApiVersionReader("version"));
|
||||
})
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
var urls = configuration["ASPNETCORE_URLS"] ?? configuration["Urls"];
|
||||
var healthUrl = "http://localhost:8080/health";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(urls))
|
||||
{
|
||||
string firstUrl = urls.Split(';').FirstOrDefault(s => s.Contains("http://"))!
|
||||
.Replace("*", "localhost").Replace("+", "localhost");
|
||||
|
||||
healthUrl = $"{firstUrl.TrimEnd('/')}/health";
|
||||
}
|
||||
|
||||
services.AddHealthChecksUI(setup =>
|
||||
{
|
||||
setup.SetNotifyUnHealthyOneTimeUntilChange();
|
||||
setup.AddHealthCheckEndpoint("primary, heal", healthUrl);
|
||||
setup.SetHeaderText("Midrand Books");
|
||||
})
|
||||
.AddInMemoryStorage();
|
||||
|
||||
services.AddOutputCache(options =>
|
||||
{
|
||||
options.AddBasePolicy(builder => builder.Cache());
|
||||
options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(10);
|
||||
});
|
||||
|
||||
services.AddOpenApi(options => options.AddDocumentTransformer<OpenApiBearerSecuritySchemeTransformer>());
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,18 @@
|
||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Security (IODC)-->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IdentityModel.AspNetCore" Version="4.3.0" />
|
||||
<PackageReference Include="IdentityModel.AspNetCore.OAuth2introspection" Version="6.2.0" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
||||
<PackageReference Include="IdentityModel" Version="6.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.8" />
|
||||
|
||||
<Using Include="IdentityModel.AspNetCore.OAuth2Introspection"/>
|
||||
</ItemGroup>
|
||||
|
||||
<!-- API Versioning -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AccessTokenClient.Extensions" Version="5.1.0" />
|
||||
|
||||
Reference in New Issue
Block a user