mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 07:06:34 +00:00
add postgres
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Azaion.Common.Configs;
|
||||
using Azaion.Common.Entities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Azaion.Services;
|
||||
|
||||
public interface IAuthService
|
||||
{
|
||||
User? CurrentUser { get; }
|
||||
string CreateToken(User user);
|
||||
}
|
||||
|
||||
public class AuthService(IHttpContextAccessor httpContextAccessor, IOptions<JwtConfig> jwtConfig) : IAuthService
|
||||
{
|
||||
public User? CurrentUser
|
||||
{
|
||||
get
|
||||
{
|
||||
var claims = httpContextAccessor.HttpContext?.User.Claims.ToDictionary(x => x.Type);
|
||||
if (claims == null)
|
||||
return null;
|
||||
|
||||
if (!Enum.TryParse(claims[ClaimTypes.Role].Value, out RoleEnum role))
|
||||
throw new ApplicationException("Invalid role");
|
||||
|
||||
return new User
|
||||
{
|
||||
Id = Guid.Parse(claims[ClaimTypes.NameIdentifier].Value),
|
||||
Email = claims[ClaimTypes.Name].Value,
|
||||
Role = role,
|
||||
HardwareId = claims[Constants.HARDWARE_ID].Value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public string CreateToken(User user)
|
||||
{
|
||||
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.Value.Secret));
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity([
|
||||
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, user.Email),
|
||||
new Claim(ClaimTypes.Role, user.Role.ToString()),
|
||||
new Claim(Constants.HARDWARE_ID, user.HardwareId)
|
||||
]),
|
||||
Expires = DateTime.UtcNow.AddHours(jwtConfig.Value.TokenLifetimeHours),
|
||||
Issuer = jwtConfig.Value.Issuer,
|
||||
Audience = jwtConfig.Value.Audience,
|
||||
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,8 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,46 +1,23 @@
|
||||
using System.Security.Claims;
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.Configs;
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.Entities;
|
||||
using Azaion.Common.Requests;
|
||||
using LinqToDB;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Azaion.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
User? CurrentUser { get; }
|
||||
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
||||
Task<User> ValidateUser(string username, string password, string? hardwareId = null, CancellationToken cancellationToken = default);
|
||||
Task UpdateHardwareId(string username, string hardwareId, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public class UserService(IDbFactory dbFactory, IHttpContextAccessor httpContextAccessor) : IUserService
|
||||
public class UserService(IDbFactory dbFactory) : IUserService
|
||||
{
|
||||
public User? CurrentUser
|
||||
{
|
||||
get
|
||||
{
|
||||
var claims = httpContextAccessor.HttpContext?.User.Claims.ToDictionary(x => x.Type);
|
||||
if (claims == null)
|
||||
return null;
|
||||
|
||||
Enum.TryParse(claims[ClaimTypes.Role].Value, out RoleEnum role);
|
||||
return new User
|
||||
{
|
||||
Id = claims[ClaimTypes.NameIdentifier].Value,
|
||||
Email = claims[ClaimTypes.Name].Value,
|
||||
Role = role,
|
||||
HardwareId = claims[Constants.HARDWARE_ID].Value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await dbFactory.Run(async db =>
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
{
|
||||
var existingUser = await db.Users.FirstOrDefaultAsync(u => u.Email == request.Email, token: cancellationToken);
|
||||
if (existingUser != null)
|
||||
@@ -75,6 +52,6 @@ public class UserService(IDbFactory dbFactory, IHttpContextAccessor httpContextA
|
||||
});
|
||||
|
||||
public async Task UpdateHardwareId(string username, string hardwareId, CancellationToken cancellationToken = default) =>
|
||||
await dbFactory.Run(async db =>
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
await db.Users.UpdateAsync(x => x.Email == username, u => new User { HardwareId = hardwareId}, token: cancellationToken));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user