-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AccessDataCacheService for cachiing, authservice for auth requests
- Loading branch information
1 parent
fc23c59
commit a816322
Showing
14 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
AuthService/Models/AccessDataCache/Requests/RecacheUserRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using AuthService.Services.Models; | ||
|
||
namespace AuthService.Models.AccessDataCache.Requests; | ||
|
||
public class RecacheUserRequest : UserAccessData; |
6 changes: 6 additions & 0 deletions
6
AuthService/Models/AccessDataCache/Responses/RecacheUserResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace AuthService.Models.AccessDataCache.Responses; | ||
|
||
public class RecacheUserResponse | ||
{ | ||
public bool IsSuccess { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
AuthService/Models/Auth/Requests/ValidateRefreshTokenRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
AuthService/Models/Auth/Responses/ValidateAccessTokenResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
AuthService/Models/Auth/Responses/ValidateRefreshTokenResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
AuthService/Services/AccessDataCache/IAccessDataCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using AuthService.Models.AccessDataCache.Requests; | ||
using AuthService.Models.AccessDataCache.Responses; | ||
using AuthService.Services.Models; | ||
|
||
namespace AuthService.Services.AccessDataCache; | ||
|
||
/// <summary> | ||
/// IAccessDataCacheService служит для кэширования данных доступа пользователя. | ||
/// </summary> | ||
public interface IAccessDataCacheService | ||
{ | ||
|
||
/// <summary> | ||
/// Возвращает данные доступа пользователя по имени пользователя. | ||
/// </summary> | ||
Task<UserAccessData?> Get(string username); | ||
|
||
/// <summary> | ||
/// Позволяет запросить данные доступа пользователя у UserService, при этом автоматически кеширует их. | ||
/// </summary> | ||
Task<UserAccessData?> RequestAndCacheUser(string username); | ||
|
||
/// <summary> | ||
/// Открытый эндпоинт для микросервисов, который позволяет кешировать данные доступа пользователя (в том числе повторно, если они были обновлены). | ||
/// </summary> | ||
Task<RecacheUserResponse> RecacheUser(RecacheUserRequest user); | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using AuthService.Exceptions.Auth; | ||
using AuthService.Models; | ||
using AuthService.Models.Authentication.Requests; | ||
using AuthService.Models.Authentication.Responses; | ||
using AuthService.Services.AccessDataCache; | ||
using AuthService.Services.Jwt; | ||
|
||
namespace AuthService.Services.Authentication; | ||
|
||
// TODO: we need more loggers in this code | ||
public class AuthenticationService(IJwtService jwtService, IAccessDataCacheService accessDataCacheService, ILogger<AuthenticationService> logger) : IAuthenticationService | ||
{ | ||
private readonly IJwtService _jwtService = jwtService; | ||
private readonly IAccessDataCacheService _adcs = accessDataCacheService; | ||
private readonly ILogger<AuthenticationService> _logger = logger; | ||
|
||
public async Task<RefreshResponse> Refresh(RefreshRequest request) | ||
{ | ||
string token = request.RefreshToken; | ||
try | ||
{ | ||
_logger.LogDebug("Validating refresh token via jwtService..."); | ||
var user = _jwtService.ValidateRefreshToken(token); | ||
|
||
if (user == null) | ||
{ | ||
_logger.LogDebug("Token validation was not successful"); | ||
throw new InvalidTokenException("Invalid refresh token"); | ||
} | ||
|
||
_logger.LogDebug("Token was successfully validated, retrieving user data from ADCS..."); | ||
|
||
// FIXME: seperate error handling for event where user is not present in cache | ||
var accessData = await _adcs.Get(user.Username) ?? throw new UserNotFoundException($"User not found: {user.Username}"); | ||
|
||
_logger.LogDebug("User data received, creating tokens via jwtService..."); | ||
|
||
return new RefreshResponse | ||
{ | ||
AccessToken = _jwtService.GenerateAccessToken(accessData), | ||
RefreshToken = _jwtService.GenerateRefreshToken(accessData) | ||
}; | ||
} | ||
catch (Exception) | ||
{ | ||
_logger.LogDebug("Refresh token was rejected due to an error"); | ||
throw new InvalidTokenException("Invalid refresh token"); | ||
} | ||
} | ||
|
||
public Task<ValidatedUser> ValidateAccessToken(ValidateAccessTokenRequest request) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<ValidatedUser> ValidateRefreshToken(ValidateRefreshTokenRequest request) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using AuthService.Models; | ||
using AuthService.Models.Authentication.Requests; | ||
using AuthService.Models.Authentication.Responses; | ||
|
||
namespace AuthService.Services.Authentication; | ||
|
||
/// <summary> | ||
/// AuthService служит для аутентификации пользователя. | ||
/// </summary> | ||
public interface IAuthenticationService | ||
{ | ||
public Task<ValidatedUser> ValidateAccessToken(ValidateAccessTokenRequest request); | ||
public Task<ValidatedUser> ValidateRefreshToken(ValidateRefreshTokenRequest request); | ||
public Task<RefreshResponse> Refresh(RefreshRequest request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters