2026-01-30 00:18:25 +01:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
using TimetableDesigner.Backend.Services.Authentication.Database;
|
|
|
|
|
|
using TimetableDesigner.Backend.Services.Authentication.Database.Model;
|
|
|
|
|
|
using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames;
|
2026-01-20 02:14:01 +01:00
|
|
|
|
|
|
|
|
|
|
namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
|
|
|
|
|
|
2026-01-30 00:18:25 +01:00
|
|
|
|
public class TokenGenerator : ITokenGenerator
|
2026-01-20 02:14:01 +01:00
|
|
|
|
{
|
2026-01-30 00:18:25 +01:00
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
private readonly DatabaseContext _databaseContext;
|
|
|
|
|
|
|
2026-01-20 02:14:01 +01:00
|
|
|
|
public TokenGenerator(IConfiguration configuration, DatabaseContext databaseContext)
|
|
|
|
|
|
{
|
2026-01-30 00:18:25 +01:00
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
_databaseContext = databaseContext;
|
2026-01-20 02:14:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GenerateAccessToken(Account account)
|
|
|
|
|
|
{
|
2026-01-30 00:18:25 +01:00
|
|
|
|
IConfigurationSection accessTokenSettings = _configuration.GetSection("Tokens")
|
|
|
|
|
|
.GetSection("AccessToken");
|
|
|
|
|
|
|
|
|
|
|
|
int lifetime = accessTokenSettings.GetSection("Lifetime")
|
|
|
|
|
|
.GetValue<int>("Normal");
|
|
|
|
|
|
DateTimeOffset expirationDate = DateTimeOffset.UtcNow.AddMinutes(lifetime);
|
2026-01-20 02:14:01 +01:00
|
|
|
|
|
2026-01-30 00:18:25 +01:00
|
|
|
|
string stringKey = accessTokenSettings.GetValue<string>("Key")!;
|
|
|
|
|
|
byte[] encodedKey = Encoding.UTF8.GetBytes(stringKey);
|
|
|
|
|
|
SymmetricSecurityKey key = new SymmetricSecurityKey(encodedKey);
|
|
|
|
|
|
|
|
|
|
|
|
string algorithm = accessTokenSettings.GetValue<string>("Algorithm")!;
|
|
|
|
|
|
|
|
|
|
|
|
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
|
|
|
|
|
|
{
|
|
|
|
|
|
Subject = new ClaimsIdentity(
|
|
|
|
|
|
[
|
|
|
|
|
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
|
|
|
|
|
new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString()),
|
|
|
|
|
|
new Claim(JwtRegisteredClaimNames.Exp, expirationDate.UtcTicks.ToString())
|
|
|
|
|
|
]),
|
|
|
|
|
|
Issuer = accessTokenSettings.GetValue<string>("Issuer"),
|
|
|
|
|
|
Audience = accessTokenSettings.GetValue<string>("Audience"),
|
|
|
|
|
|
SigningCredentials = new SigningCredentials(key, algorithm),
|
|
|
|
|
|
Expires = expirationDate.UtcDateTime,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
|
|
|
|
|
|
handler.InboundClaimTypeMap.Clear();
|
|
|
|
|
|
SecurityToken token = handler.CreateToken(descriptor);
|
|
|
|
|
|
|
|
|
|
|
|
return handler.WriteToken(token);
|
2026-01-20 02:14:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> GenerateRefreshTokenAsync(Account account)
|
|
|
|
|
|
{
|
2026-01-30 00:18:25 +01:00
|
|
|
|
return null;
|
2026-01-20 02:14:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> ExtendRefreshTokenAsync()
|
|
|
|
|
|
{
|
2026-01-30 00:18:25 +01:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2026-01-20 02:14:01 +01:00
|
|
|
|
}
|