feat(infra): move authentication implementations and add ITokenGenerator
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FiscalOS.API.Login;
|
||||
|
||||
internal 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;
|
||||
}
|
||||
|
||||
internal 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,67 +0,0 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
using FiscalOS.Core.Identity;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace FiscalOS.API.Login;
|
||||
|
||||
internal sealed class TokenService(
|
||||
TimeProvider timeProvider,
|
||||
IOptions<JwtOptions> jwtOptions
|
||||
)
|
||||
{
|
||||
private const int RefreshTokenExpiryInHours = 12;
|
||||
private readonly JwtOptions _jwtOptions = jwtOptions.Value;
|
||||
private readonly TimeProvider _timeProvider = timeProvider;
|
||||
|
||||
public string GenerateAccessToken(User user)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var secretKeyBytes = Encoding.UTF8.GetBytes(_jwtOptions.Secret);
|
||||
var issuedAt = _timeProvider.GetUtcNow();
|
||||
var expiresAt = issuedAt.AddMinutes(_jwtOptions.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.Issuer,
|
||||
Audience = _jwtOptions.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user