添加项目文件。

This commit is contained in:
2026-05-09 17:06:30 +08:00
parent c60f5fe117
commit 720ef957d4
378 changed files with 14843 additions and 0 deletions
@@ -0,0 +1,97 @@
using AutoMapper;
using IdentityService.Domain;
using IdentityService.WebApi.Applications.Dtos;
using IdentityService.WebApi.Applications.Dtos.Common;
using IM.Commons;
using IM.Jwt;
using Microsoft.Extensions.Options;
using System.Security.Claims;
namespace IdentityService.WebApi.Applications.Auth
{
public class AuthService
{
private readonly ITokenService tokenService;
private readonly IIdRepository idRepository;
private readonly IOptions<JwtOptions> jwtOptions;
private readonly IdDomainService idDomainService;
private readonly IMapper mapper;
public AuthService(ITokenService tokenService, IIdRepository idRepository,
IOptions<JwtOptions> options, IdDomainService idDomainService,
IMapper mapper
)
{
this.tokenService = tokenService;
this.idRepository = idRepository;
jwtOptions = options;
this.idDomainService = idDomainService;
this.mapper = mapper;
}
public async Task<Result<LoginResponse>> LoginAsync(string username, string password)
{
var user = await idRepository.FindByUserNameAsync(username);
if (user is null)
{
return Result<LoginResponse>.Fail(ResultCode.USER_NOT_FOUND);
}
var idResult = await idRepository.CheckForSignInAsync(user, password, true);
if (!idResult.Succeeded)
{
return Result<LoginResponse>.Fail(ResultCode.PASSWORD_ERROR);
}
var token = await BuildTokenAsync(user);
var refreshToken = await tokenService.CreateRefreshTokenAsync(user.Id);
return Result<LoginResponse>.Success(new LoginResponse(user.Id, token, refreshToken, null));
}
public async Task<Result<UserResponse?>> RegisterAsync(string userName, string password, string nickName)
{
var userResult = await idDomainService.CreateUserAsync(userName, password, nickName);
if (!userResult.Succeeded)
return Result<UserResponse?>.Fail(userResult);
var idResult = await idRepository.CreateAsync(userResult.Data!, password);
if (!idResult.Succeeded)
{
var msg = idResult.Errors.FirstOrDefault();
return Result<UserResponse?>.Fail(
ResultCode.REGISTER_ERROR,
msg?.Description ?? ResultCode.REGISTER_ERROR.GetDescription());
}
return Result<UserResponse?>.Success(mapper.Map<UserResponse>(userResult.Data));
}
public async Task<Result<LoginResponse>> RefreshAsync(string refreshToken)
{
var validateRes = await tokenService.ValidateRefreshTokenAsync(refreshToken);
if (!validateRes.ok)
{
return Result<LoginResponse>.Fail(ResultCode.AUTH_FAILED);
}
var user = await idRepository.FindByIdAsync(validateRes.userId);
if (user is null)
{
return Result<LoginResponse>.Fail(ResultCode.USER_NOT_FOUND);
}
var token = await BuildTokenAsync(user);
return Result<LoginResponse>.Success(new LoginResponse(user.Id, token, refreshToken, null));
}
private async Task<string> BuildTokenAsync(Domain.Entities.User user)
{
var roles = await idRepository.GetRolesAsync(user);
List<Claim> claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
foreach (string role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role));
}
return tokenService.GetToken(claims, jwtOptions.Value);
}
}
}