parent
7cc16c9d71
commit
d14e6de1f2
@ -1,11 +1,14 @@
|
|||||||
|
using Infrastructure.HttpUserContext;
|
||||||
|
|
||||||
namespace Infrastructure.Extensions;
|
namespace Infrastructure.Extensions;
|
||||||
|
|
||||||
public static class UserContextSetup
|
public static class UserContextSetup
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddDefaultUserContext(this IServiceCollection services)
|
public static IServiceCollection AddDefaultUserContext<T>(this IServiceCollection services) where T : IEquatable<T>
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(services);
|
ArgumentNullException.ThrowIfNull(services);
|
||||||
services.TryAddScoped(typeof(IUserContext<>), typeof(DefaultUserContext));
|
services.AddHttpContextAccessor();
|
||||||
|
services.TryAddScoped(typeof(IUserContext<T>), typeof(UserContext<T>));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Infrastructure.Utils;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace Infrastructure.HttpUserContext;
|
||||||
|
|
||||||
|
public class UserContext<TId>(
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
JwtOptions jwtOptions,
|
||||||
|
IEncryptionService encryptionService,
|
||||||
|
JwtSecurityTokenHandler jwtSecurityTokenHandler)
|
||||||
|
: IUserContext<TId> where TId : IEquatable<TId>
|
||||||
|
{
|
||||||
|
private readonly ClaimsPrincipal principal = httpContextAccessor?.HttpContext?.User;
|
||||||
|
|
||||||
|
public TId Id => GetIdFromClaims();
|
||||||
|
|
||||||
|
public string Username => principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.UniqueName).Value;
|
||||||
|
|
||||||
|
public string Name => principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Name).Value;
|
||||||
|
|
||||||
|
public string Email => principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Email).Value;
|
||||||
|
|
||||||
|
public string[] RoleIds => principal.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToArray();
|
||||||
|
|
||||||
|
public string RemoteIpAddress => httpContextAccessor.HttpContext?.GetRequestIp()!;
|
||||||
|
|
||||||
|
public JwtTokenInfo GenerateTokenInfo()
|
||||||
|
{
|
||||||
|
var claims = GetClaimsFromUserContext();
|
||||||
|
var jwtToken = new JwtSecurityToken(
|
||||||
|
issuer: jwtOptions.Issuer,
|
||||||
|
audience: jwtOptions.Audience,
|
||||||
|
claims: claims,
|
||||||
|
notBefore: DateTime.Now,
|
||||||
|
expires: DateTime.Now.AddSeconds(jwtOptions.Expiration),
|
||||||
|
signingCredentials: jwtOptions.SigningCredentials);
|
||||||
|
var token = jwtSecurityTokenHandler.WriteToken(jwtToken);
|
||||||
|
token = encryptionService.Encrypt(token);
|
||||||
|
return new JwtTokenInfo(token, jwtOptions.Expiration,
|
||||||
|
JwtBearerDefaults.AuthenticationScheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IList<Claim> GetClaimsFromUserContext()
|
||||||
|
{
|
||||||
|
var claims = new List<Claim>()
|
||||||
|
{
|
||||||
|
new(JwtRegisteredClaimNames.UniqueName, Username),
|
||||||
|
new(JwtRegisteredClaimNames.NameId, Id.ToString() ?? string.Empty),
|
||||||
|
new(JwtRegisteredClaimNames.Name, Name),
|
||||||
|
new(JwtRegisteredClaimNames.Email, Email),
|
||||||
|
new(JwtRegisteredClaimNames.Iat,
|
||||||
|
EpochTime.GetIntDate(DateTime.Now).ToString(CultureInfo.InvariantCulture),
|
||||||
|
ClaimValueTypes.Integer64),
|
||||||
|
new(JwtRegisteredClaimNames.Exp, jwtOptions.Expiration.ToString())
|
||||||
|
};
|
||||||
|
claims.AddRange(RoleIds.Select(rId => new Claim(ClaimTypes.Role, rId)));
|
||||||
|
return claims;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TId GetIdFromClaims()
|
||||||
|
{
|
||||||
|
var idClaim = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.NameId);
|
||||||
|
return (TId)Convert.ChangeType(idClaim.Value, typeof(TId));
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +0,0 @@
|
|||||||
namespace Infrastructure.Security;
|
|
||||||
|
|
||||||
public class DefaultTokenBuilder(
|
|
||||||
JwtOptions jwtOptions,
|
|
||||||
JwtSecurityTokenHandler jwtSecurityTokenHandler,
|
|
||||||
IEncryptionService encryptionService)
|
|
||||||
: TokenBuilderBase<long>(jwtOptions, jwtSecurityTokenHandler, encryptionService)
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
namespace Infrastructure.Security;
|
|
||||||
|
|
||||||
public class DefaultUserContext : UserContextBase<long>
|
|
||||||
{
|
|
||||||
}
|
|
Loading…
Reference in new issue