chore: remove OpenApi and configure JWT authentication schemes
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
|
||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
@@ -13,4 +12,4 @@
|
||||
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
|
||||
<PackageVersion Include="Spectre.Console" Version="0.54.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
[*.cs]
|
||||
|
||||
dotnet_diagnostic.CA1062.severity = none
|
||||
dotnet_diagnostic.CA5404.severity = none
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
|
||||
namespace FiscalOS.Infra.Authentication;
|
||||
|
||||
public sealed record JwtOptions
|
||||
@@ -6,6 +8,8 @@ public sealed record JwtOptions
|
||||
public string Audience { get; init; } = string.Empty;
|
||||
public string Secret { get; init; } = string.Empty;
|
||||
public int ExpiryInMinutes { get; init; } = 5;
|
||||
|
||||
public SymmetricSecurityKey Key => new(Encoding.UTF8.GetBytes(Secret));
|
||||
}
|
||||
|
||||
public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
|
||||
@@ -22,4 +26,33 @@ public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
|
||||
{
|
||||
_configuration.GetSection(SectionName).Bind(options);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class JwtBearerOptionsSetup(IOptions<JwtOptions> jwtOptions) : IConfigureNamedOptions<JwtBearerOptions>
|
||||
{
|
||||
private readonly JwtOptions _jwtOptions = jwtOptions.Value;
|
||||
|
||||
public void Configure(string? name, JwtBearerOptions options)
|
||||
{
|
||||
options.TokenValidationParameters = new()
|
||||
{
|
||||
ValidIssuer = _jwtOptions.Issuer,
|
||||
ValidAudience = _jwtOptions.Audience,
|
||||
IssuerSigningKey = _jwtOptions.Key,
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidateLifetime = true,
|
||||
};
|
||||
|
||||
if (name is Schemes.AllowExpiredTokens)
|
||||
{
|
||||
options.TokenValidationParameters.ValidateLifetime = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Configure(JwtBearerOptions options)
|
||||
{
|
||||
Configure(Options.DefaultName, options);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,17 @@ public static class ServiceCollectionExtensions
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(TimeProvider.System);
|
||||
|
||||
services.ConfigureOptions<JwtOptionsSetup>();
|
||||
services.ConfigureOptions<JwtBearerOptionsSetup>();
|
||||
|
||||
services.AddSingleton<ITokenGenerator, TokenGenerator>(TokenGenerator.From);
|
||||
services.AddSingleton<IPasswordHasher, PasswordHasher>(PasswordHasher.From);
|
||||
|
||||
services.ConfigureOptions<AppDbContextOptionsSetup>();
|
||||
services.AddDbContext<AppDbContext>();
|
||||
services.AddHostedService<MigrationService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user