chore: remove OpenApi and configure JWT authentication schemes
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.2" />
|
<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">
|
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.2">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
[*.cs]
|
[*.cs]
|
||||||
|
|
||||||
dotnet_diagnostic.CA1062.severity = none
|
dotnet_diagnostic.CA1062.severity = none
|
||||||
|
dotnet_diagnostic.CA5404.severity = none
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
|
||||||
namespace FiscalOS.Infra.Authentication;
|
namespace FiscalOS.Infra.Authentication;
|
||||||
|
|
||||||
public sealed record JwtOptions
|
public sealed record JwtOptions
|
||||||
@@ -6,6 +8,8 @@ public sealed record JwtOptions
|
|||||||
public string Audience { get; init; } = string.Empty;
|
public string Audience { get; init; } = string.Empty;
|
||||||
public string Secret { get; init; } = string.Empty;
|
public string Secret { get; init; } = string.Empty;
|
||||||
public int ExpiryInMinutes { get; init; } = 5;
|
public int ExpiryInMinutes { get; init; } = 5;
|
||||||
|
|
||||||
|
public SymmetricSecurityKey Key => new(Encoding.UTF8.GetBytes(Secret));
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
|
public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
|
||||||
@@ -23,3 +27,32 @@ public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
|
|||||||
_configuration.GetSection(SectionName).Bind(options);
|
_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)
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton(TimeProvider.System);
|
services.AddSingleton(TimeProvider.System);
|
||||||
|
|
||||||
services.ConfigureOptions<JwtOptionsSetup>();
|
services.ConfigureOptions<JwtOptionsSetup>();
|
||||||
|
services.ConfigureOptions<JwtBearerOptionsSetup>();
|
||||||
|
|
||||||
services.AddSingleton<ITokenGenerator, TokenGenerator>(TokenGenerator.From);
|
services.AddSingleton<ITokenGenerator, TokenGenerator>(TokenGenerator.From);
|
||||||
services.AddSingleton<IPasswordHasher, PasswordHasher>(PasswordHasher.From);
|
services.AddSingleton<IPasswordHasher, PasswordHasher>(PasswordHasher.From);
|
||||||
|
|
||||||
services.ConfigureOptions<AppDbContextOptionsSetup>();
|
services.ConfigureOptions<AppDbContextOptionsSetup>();
|
||||||
services.AddDbContext<AppDbContext>();
|
services.AddDbContext<AppDbContext>();
|
||||||
services.AddHostedService<MigrationService>();
|
services.AddHostedService<MigrationService>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user