Add authentication methods to access protected resources #15

Merged
Wesser merged 13 commits from feat/auth into release/v1.0.0 2024-06-28 23:14:18 +03:00
2 changed files with 14 additions and 11 deletions
Showing only changes of commit 039d323643 - Show all commits

View File

@ -5,6 +5,7 @@ namespace Mirea.Api.Security.Common.Dto.Responses;
public class AuthTokenResponse public class AuthTokenResponse
{ {
public required string AccessToken { get; set; } public required string AccessToken { get; set; }
public DateTime AccessExpiresIn { get; set; }
public required string RefreshToken { get; set; } public required string RefreshToken { get; set; }
public DateTime ExpiresIn { get; set; } public DateTime RefreshExpiresIn { get; set; }
} }

View File

@ -34,7 +34,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
public async Task<AuthTokenResponse> GenerateAuthTokensAsync(TokenRequest request, string userId, CancellationToken cancellation = default) public async Task<AuthTokenResponse> GenerateAuthTokensAsync(TokenRequest request, string userId, CancellationToken cancellation = default)
{ {
var refreshToken = GenerateRefreshToken(); var refreshToken = GenerateRefreshToken();
var accessToken = GenerateAccessToken(userId); var (token, expireIn) = GenerateAccessToken(userId);
var authTokenStruct = new AuthToken var authTokenStruct = new AuthToken
{ {
@ -43,16 +43,17 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
RefreshToken = refreshToken, RefreshToken = refreshToken,
UserAgent = request.UserAgent, UserAgent = request.UserAgent,
UserId = userId, UserId = userId,
AccessToken = accessToken.Token AccessToken = token
}; };
await SetAuthTokenDataToCache(request.Fingerprint, authTokenStruct, cancellation); await SetAuthTokenDataToCache(request.Fingerprint, authTokenStruct, cancellation);
return new AuthTokenResponse return new AuthTokenResponse
{ {
AccessToken = accessToken.Token, AccessToken = token,
ExpiresIn = accessToken.ExpireIn, AccessExpiresIn = expireIn,
RefreshToken = authTokenStruct.RefreshToken RefreshToken = authTokenStruct.RefreshToken,
RefreshExpiresIn = DateTime.UtcNow.Add(Lifetime),
}; };
} }
@ -77,17 +78,18 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
throw new SecurityException(request.Fingerprint); throw new SecurityException(request.Fingerprint);
} }
var accessToken = GenerateAccessToken(authToken.UserId); var (token, expireIn) = GenerateAccessToken(authToken.UserId);
await RevokeAccessToken(authToken.AccessToken); await RevokeAccessToken(authToken.AccessToken);
authToken.AccessToken = accessToken.Token; authToken.AccessToken = token;
await SetAuthTokenDataToCache(request.Fingerprint, authToken, cancellation); await SetAuthTokenDataToCache(request.Fingerprint, authToken, cancellation);
return new AuthTokenResponse return new AuthTokenResponse
{ {
AccessToken = accessToken.Token, AccessToken = token,
ExpiresIn = accessToken.ExpireIn, AccessExpiresIn = expireIn,
RefreshToken = GenerateRefreshToken() RefreshToken = GenerateRefreshToken(),
RefreshExpiresIn = DateTime.UtcNow.Add(Lifetime)
}; };
} }