mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 14:20:06 +01:00
feat: offload jwt generation to guhf, move auth code to new controller
This commit is contained in:
34
Controllers/GeneralUseHelperFunctions.cs
Normal file
34
Controllers/GeneralUseHelperFunctions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user