72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
namespace ActiveAllocator.API.Authorization;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using ActiveAllocator.API.Entities;
|
|
using ActiveAllocator.API.Helpers;
|
|
|
|
public interface IJwtUtils
|
|
{
|
|
public string GenerateToken(User user);
|
|
public int? ValidateToken(string token);
|
|
}
|
|
|
|
public class JwtUtils : IJwtUtils
|
|
{
|
|
private readonly AppSettings _appSettings;
|
|
|
|
public JwtUtils(IOptions<AppSettings> appSettings)
|
|
{
|
|
_appSettings = appSettings.Value;
|
|
}
|
|
|
|
public string GenerateToken(User user)
|
|
{
|
|
// generate token that is valid for 7 days
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString()) }),
|
|
Expires = DateTime.UtcNow.AddDays(7),
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
|
};
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
return tokenHandler.WriteToken(token);
|
|
}
|
|
|
|
public int? ValidateToken(string token)
|
|
{
|
|
if (token == null)
|
|
return null;
|
|
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
|
try
|
|
{
|
|
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
|
|
ClockSkew = TimeSpan.Zero
|
|
}, out SecurityToken validatedToken);
|
|
|
|
var jwtToken = (JwtSecurityToken)validatedToken;
|
|
var userId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);
|
|
|
|
// return user id from JWT token if validation successful
|
|
return userId;
|
|
}
|
|
catch
|
|
{
|
|
// return null if validation fails
|
|
return null;
|
|
}
|
|
}
|
|
} |