Add authentication methods to access protected resources #15
@ -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; }
|
||||||
}
|
}
|
@ -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)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user