fix: align backend APIs and upload flow
This commit is contained in:
@@ -4,6 +4,7 @@ using IdentityService.WebApi.Applications.Dtos;
|
||||
using IdentityService.WebApi.Applications.Dtos.Common;
|
||||
using IM.Commons;
|
||||
using IM.Jwt;
|
||||
using IM.InitCommon.Management;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Security.Claims;
|
||||
|
||||
@@ -16,10 +17,11 @@ namespace IdentityService.WebApi.Applications.Auth
|
||||
private readonly IOptions<JwtOptions> jwtOptions;
|
||||
private readonly IdDomainService idDomainService;
|
||||
private readonly IMapper mapper;
|
||||
private readonly RuntimePolicy runtime;
|
||||
|
||||
public AuthService(ITokenService tokenService, IIdRepository idRepository,
|
||||
IOptions<JwtOptions> options, IdDomainService idDomainService,
|
||||
IMapper mapper
|
||||
IMapper mapper, RuntimePolicy runtime
|
||||
)
|
||||
{
|
||||
this.tokenService = tokenService;
|
||||
@@ -27,6 +29,7 @@ namespace IdentityService.WebApi.Applications.Auth
|
||||
jwtOptions = options;
|
||||
this.idDomainService = idDomainService;
|
||||
this.mapper = mapper;
|
||||
this.runtime = runtime;
|
||||
}
|
||||
|
||||
public async Task<Result<LoginResponse>> LoginAsync(string username, string password)
|
||||
@@ -37,17 +40,20 @@ namespace IdentityService.WebApi.Applications.Auth
|
||||
{
|
||||
return Result<LoginResponse>.Fail(ResultCode.USER_NOT_FOUND);
|
||||
}
|
||||
if (user.Status != UserState.Normal || user.IsDeleted) return Result<LoginResponse>.Fail(ResultCode.AUTH_FAILED);
|
||||
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);
|
||||
var refreshToken = await tokenService.CreateRefreshTokenAsync(user.Id, stamp: user.SecurityStamp, days: runtime.Current.ClientRefreshDays);
|
||||
return Result<LoginResponse>.Success(new LoginResponse(user.Id, token, refreshToken, null, user.UserName, user.NickName,user.Avatar, user.CreationTime));
|
||||
}
|
||||
public async Task<Result<UserResponse?>> RegisterAsync(string userName, string password, string nickName)
|
||||
{
|
||||
if (!runtime.Current.RegistrationEnabled) return Result<UserResponse?>.Fail(ResultCode.PERMISSION_DENIED, "平台暂未开放注册");
|
||||
if (password.Length < runtime.Current.PasswordMinLength) return Result<UserResponse?>.Fail(ResultCode.PARAMETER_ERROR, "密码不符合当前最小长度要求");
|
||||
var userResult = await idDomainService.CreateUserAsync(userName, password, nickName);
|
||||
if (!userResult.Succeeded)
|
||||
return Result<UserResponse?>.Fail(userResult);
|
||||
@@ -78,20 +84,24 @@ namespace IdentityService.WebApi.Applications.Auth
|
||||
return Result<LoginResponse>.Fail(ResultCode.USER_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (user.Status != UserState.Normal || user.IsDeleted || validateRes.stamp != user.SecurityStamp) return Result<LoginResponse>.Fail(ResultCode.AUTH_FAILED);
|
||||
var token = await BuildTokenAsync(user);
|
||||
|
||||
return Result<LoginResponse>.Success(new LoginResponse(user.Id, token, refreshToken, null, user.UserName, user.NickName, user.Avatar, user.CreationTime));
|
||||
await tokenService.RevokeRefreshTokenAsync(refreshToken);
|
||||
var nextRefresh = await tokenService.CreateRefreshTokenAsync(user.Id, stamp: user.SecurityStamp, days: runtime.Current.ClientRefreshDays);
|
||||
return Result<LoginResponse>.Success(new LoginResponse(user.Id, token, nextRefresh, null, user.UserName, user.NickName, user.Avatar, user.CreationTime));
|
||||
}
|
||||
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()));
|
||||
claims.Add(new Claim("session_stamp", user.SecurityStamp ?? ""));
|
||||
foreach (string role in roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
return tokenService.GetToken(claims, jwtOptions.Value);
|
||||
var original = jwtOptions.Value;
|
||||
return tokenService.GetToken(claims, new JwtOptions { Key = original.Key, Issuer = original.Issuer, Audience = original.Audience, RefreshTokenDays = original.RefreshTokenDays, AccessTokenMinutes = runtime.Current.ClientAccessMinutes > 0 ? runtime.Current.ClientAccessMinutes : original.AccessTokenMinutes });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user