feat: offload jwt generation to guhf, move auth code to new controller

This commit is contained in:
2025-07-15 11:19:59 +02:00
parent b6dc1ce2cd
commit d0fc4e5ef2
4 changed files with 78 additions and 89 deletions

View File

@@ -0,0 +1,34 @@
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace QuotifyBE.Controllers;
public class GeneralUseHelpers
{
public string GenerateJwtToken(string username, IConfiguration appsettings)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, username),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var key = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(appsettings["JwtSecret"]!)
// won't be null here - otherwise Program.cs wouldn't start
);
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: appsettings["DomainName"]!,
audience: appsettings["DomainName"]!,
claims: claims,
expires: DateTime.Now.AddDays(7),
signingCredentials: creds
);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}