-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.cs
More file actions
109 lines (92 loc) · 4.24 KB
/
AuthService.cs
File metadata and controls
109 lines (92 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using ProjectVG.Application.Models.User;
using ProjectVG.Application.Services.Credit;
using ProjectVG.Application.Services.Users;
using ProjectVG.Infrastructure.Auth;
namespace ProjectVG.Application.Services.Auth
{
public class AuthService : IAuthService
{
private readonly IUserService _userService;
private readonly ITokenService _tokenService;
private readonly ICreditManagementService _tokenManagementService;
private readonly ILogger<AuthService> _logger;
public AuthService(
IUserService userService,
ITokenService tokenService,
ICreditManagementService tokenManagementService,
ILogger<AuthService> logger)
{
_userService = userService;
_tokenService = tokenService;
_tokenManagementService = tokenManagementService;
_logger = logger;
}
public async Task<AuthResult> GuestLoginAsync(string guestId)
{
if (string.IsNullOrEmpty(guestId)) {
throw new ValidationException(ErrorCode.GUEST_ID_INVALID);
}
var user = await _userService.TryGetByProviderAsync("guest", guestId).ConfigureAwait(false);
if (user == null) {
string uuid = GenerateGuestUuid(guestId);
var createCommand = new UserCreateCommand(
Username: $"guest_{uuid}",
Email: $"guest@guest{uuid}.local",
ProviderId: guestId,
Provider: "guest"
);
user = await _userService.CreateUserAsync(createCommand).ConfigureAwait(false);
_logger.LogInformation("새 게스트 사용자 생성됨: UserId={UserId}, GuestId={GuestId}", user.Id, guestId);
}
return await FinalizeLoginAsync(user, "guest").ConfigureAwait(false);
}
private async Task<AuthResult> FinalizeLoginAsync(UserDto user, string provider)
{
// 초기 크레딧 지급
var tokenGranted = await _tokenManagementService.GrantInitialCreditsAsync(user.Id).ConfigureAwait(false);
if (tokenGranted) {
_logger.LogInformation("사용자 {UserId}에게 최초 크레딧 지급 완료", user.Id);
}
// 최종 JWT 토큰 발급
var tokens = await _tokenService.GenerateTokensAsync(user.Id).ConfigureAwait(false);
return new AuthResult {
Tokens = tokens,
User = user
};
}
public async Task<AuthResult> RefreshAccessTokenAsync(string? refreshToken)
{
if (string.IsNullOrEmpty(refreshToken)) {
throw new ValidationException(ErrorCode.TOKEN_MISSING);
}
var tokens = await _tokenService.RefreshAccessTokenAsync(refreshToken).ConfigureAwait(false);
if (tokens == null) {
throw new ValidationException(ErrorCode.TOKEN_REFRESH_FAILED);
}
var userId = await _tokenService.GetUserIdFromTokenAsync(refreshToken).ConfigureAwait(false);
var user = userId.HasValue ? await _userService.TryGetByIdAsync(userId.Value).ConfigureAwait(false) : null;
return new AuthResult {
Tokens = tokens,
User = user
};
}
public async Task<bool> LogoutAsync(string? refreshToken)
{
if (string.IsNullOrEmpty(refreshToken)) {
throw new ValidationException(ErrorCode.TOKEN_MISSING);
}
var revoked = await _tokenService.RevokeRefreshTokenAsync(refreshToken).ConfigureAwait(false);
if (revoked) {
var userId = await _tokenService.GetUserIdFromTokenAsync(refreshToken).ConfigureAwait(false);
}
return revoked;
}
private static string GenerateGuestUuid(string providerUserId)
{
using var sha256 = System.Security.Cryptography.SHA256.Create();
var hash = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(providerUserId));
var hashString = Convert.ToHexString(hash);
return hashString.Substring(0, Math.Min(hashString.Length, 16)).ToLowerInvariant();
}
}
}