|
|
@ -3,32 +3,31 @@ using System.IdentityModel.Tokens.Jwt;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using Infrastructure.Utils;
|
|
|
|
using Infrastructure.Utils;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Infrastructure.Security;
|
|
|
|
namespace Infrastructure.Security;
|
|
|
|
|
|
|
|
|
|
|
|
public interface ITokenBuilder
|
|
|
|
public interface ITokenBuilder<TId> where TId : IEquatable<TId>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IList<Claim> GetClaimsFromUserContext(IUserContext userContext);
|
|
|
|
IList<Claim> GetClaimsFromUserContext(IUserContext<TId> userContext);
|
|
|
|
|
|
|
|
|
|
|
|
void SetUserContext(TokenValidatedContext context);
|
|
|
|
void SetUserContext(TokenValidatedContext context);
|
|
|
|
|
|
|
|
|
|
|
|
JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection<Claim> claims);
|
|
|
|
JwtTokenInfo GenerateJwtTokenInfo(IReadOnlyCollection<Claim> claims);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class TokenBuilder(
|
|
|
|
public abstract class TokenBuilderBase<TId>(
|
|
|
|
JwtOptions jwtOptions,
|
|
|
|
JwtOptions jwtOptions,
|
|
|
|
JwtSecurityTokenHandler jwtSecurityTokenHandler,
|
|
|
|
JwtSecurityTokenHandler jwtSecurityTokenHandler,
|
|
|
|
IEncryptionService encryptionService)
|
|
|
|
IEncryptionService encryptionService)
|
|
|
|
: ITokenBuilder
|
|
|
|
: ITokenBuilder<TId> where TId : IEquatable<TId>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public IList<Claim> GetClaimsFromUserContext(IUserContext userContext)
|
|
|
|
public IList<Claim> GetClaimsFromUserContext(IUserContext<TId> userContext)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var claims = new List<Claim>()
|
|
|
|
var claims = new List<Claim>()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
new(JwtRegisteredClaimNames.UniqueName, userContext.Username),
|
|
|
|
new(JwtRegisteredClaimNames.UniqueName, userContext.Username),
|
|
|
|
new(JwtRegisteredClaimNames.NameId, userContext.Id.ToString()),
|
|
|
|
new(JwtRegisteredClaimNames.NameId, userContext.Id.ToString() ?? string.Empty),
|
|
|
|
new(JwtRegisteredClaimNames.Name, userContext.Name),
|
|
|
|
new(JwtRegisteredClaimNames.Name, userContext.Name),
|
|
|
|
new(JwtRegisteredClaimNames.Email, userContext.Email),
|
|
|
|
new(JwtRegisteredClaimNames.Email, userContext.Email),
|
|
|
|
new(JwtRegisteredClaimNames.Iat,
|
|
|
|
new(JwtRegisteredClaimNames.Iat,
|
|
|
@ -42,11 +41,12 @@ public class TokenBuilder(
|
|
|
|
|
|
|
|
|
|
|
|
public void SetUserContext(TokenValidatedContext context)
|
|
|
|
public void SetUserContext(TokenValidatedContext context)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var userContext = context.HttpContext.RequestServices.GetService<IUserContext>() ??
|
|
|
|
var userContext =
|
|
|
|
throw new NullReferenceException(nameof(IUserContext));
|
|
|
|
context.HttpContext.RequestServices.GetService(typeof(IUserContext<TId>)) as IUserContext<TId> ??
|
|
|
|
|
|
|
|
throw new NullReferenceException(nameof(IUserContext<TId>));
|
|
|
|
var principal = context.Principal ?? throw new NullReferenceException(nameof(context.Principal));
|
|
|
|
var principal = context.Principal ?? throw new NullReferenceException(nameof(context.Principal));
|
|
|
|
userContext.Id = long.Parse(
|
|
|
|
var idClaim = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId);
|
|
|
|
principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId).Value);
|
|
|
|
userContext.Id = (TId)Convert.ChangeType(idClaim.Value, typeof(TId));
|
|
|
|
userContext.Username =
|
|
|
|
userContext.Username =
|
|
|
|
principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value;
|
|
|
|
principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value;
|
|
|
|
userContext.Name = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value;
|
|
|
|
userContext.Name = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value;
|
|
|
|