feat(infra): move authentication implementations and add ITokenGenerator

This commit is contained in:
Stevan Freeborn
2026-02-03 04:38:40 -06:00
parent c4d0bf78ab
commit a1b993bd62
6 changed files with 50 additions and 37 deletions
@@ -0,0 +1,25 @@
namespace FiscalOS.Infra.Authentication;
public sealed record JwtOptions
{
public string Issuer { get; init; } = string.Empty;
public string Audience { get; init; } = string.Empty;
public string Secret { get; init; } = string.Empty;
public int ExpiryInMinutes { get; init; } = 5;
}
public sealed record JwtOptionsSetup : IConfigureOptions<JwtOptions>
{
private const string SectionName = nameof(JwtOptions);
private readonly IConfiguration _configuration;
public JwtOptionsSetup(IConfiguration configuration)
{
_configuration = configuration;
}
public void Configure(JwtOptions options)
{
_configuration.GetSection(SectionName).Bind(options);
}
}
@@ -1,4 +1,4 @@
namespace FiscalOS.Infra.Identity;
namespace FiscalOS.Infra.Authentication;
public sealed class PasswordHasher : IPasswordHasher
{
@@ -17,6 +17,11 @@ public sealed class PasswordHasher : IPasswordHasher
return new();
}
public static PasswordHasher From(IServiceProvider serviceProvider)
{
return new();
}
public string Hash(string password)
{
var salt = RandomNumberGenerator.GetBytes(SaltSize);
@@ -0,0 +1,78 @@
namespace FiscalOS.Infra.Authentication;
public sealed class TokenGenerator : ITokenGenerator
{
private const int RefreshTokenExpiryInHours = 12;
private readonly IOptions<JwtOptions> _jwtOptions;
private readonly TimeProvider _timeProvider;
private TokenGenerator(
TimeProvider timeProvider,
IOptions<JwtOptions> jwtOptions
)
{
_timeProvider = timeProvider;
_jwtOptions = jwtOptions;
}
public static TokenGenerator From(IServiceProvider serviceProvider)
{
var timeProvider = serviceProvider.GetRequiredService<TimeProvider>();
var jwtOptions = serviceProvider.GetRequiredService<IOptions<JwtOptions>>();
return new(timeProvider, jwtOptions);
}
public static TokenGenerator From(
TimeProvider timeProvider,
IOptions<JwtOptions> jwtOptions
)
{
return new(timeProvider, jwtOptions);
}
public string GenerateAccessToken(User user)
{
var tokenHandler = new JwtSecurityTokenHandler();
var secretKeyBytes = Encoding.UTF8.GetBytes(_jwtOptions.Value.Secret);
var issuedAt = _timeProvider.GetUtcNow();
var expiresAt = issuedAt.AddMinutes(_jwtOptions.Value.ExpiryInMinutes);
List<Claim> claims = [
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
];
var descriptor = new SecurityTokenDescriptor()
{
Subject = new(claims),
IssuedAt = issuedAt.UtcDateTime,
Expires = expiresAt.UtcDateTime,
Issuer = _jwtOptions.Value.Issuer,
Audience = _jwtOptions.Value.Audience,
SigningCredentials = new(
new SymmetricSecurityKey(secretKeyBytes),
SecurityAlgorithms.HmacSha256Signature
),
};
var securityToken = tokenHandler.CreateJwtSecurityToken(descriptor);
var jwtToken = tokenHandler.WriteToken(securityToken);
return jwtToken;
}
public RefreshToken GenerateRefreshToken(User user)
{
var expiresAt = _timeProvider
.GetUtcNow()
.AddHours(RefreshTokenExpiryInHours);
return RefreshToken.From(user.Id, GenerateToken(), expiresAt);
}
private static string GenerateToken()
{
var randomBytes = RandomNumberGenerator.GetBytes(32);
var token = Convert.ToBase64String(randomBytes);
return token;
}
}
+1
View File
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>
+7 -2
View File
@@ -1,8 +1,12 @@
global using System.IdentityModel.Tokens.Jwt;
global using System.Security.Claims;
global using System.Security.Cryptography;
global using System.Text;
global using FiscalOS.Core.Authentication;
global using FiscalOS.Core.Identity;
global using FiscalOS.Infra.Authentication;
global using FiscalOS.Infra.Data;
global using FiscalOS.Infra.Identity;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.Diagnostics;
@@ -10,4 +14,5 @@ global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using Microsoft.Extensions.Options;
global using Microsoft.IdentityModel.Tokens;