This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Adding new endpoint to refresh token
- Loading branch information
Showing
13 changed files
with
196 additions
and
76 deletions.
There are no files selected for viewing
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
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
9 changes: 4 additions & 5 deletions
9
....TokenManager/TokenManager.Application.Services/Commands/Users/LoginUserCommandHandler.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 |
---|---|---|
@@ -1,25 +1,24 @@ | ||
using MediatR; | ||
using TokenManager.Application.Services.Mappers; | ||
using TokenManager.Application.Services.Responses; | ||
using TokenManager.Domain.Entities; | ||
using TokenManager.Domain.Interfaces; | ||
|
||
namespace TokenManager.Application.Services.Commands.Users | ||
{ | ||
public class LoginUserCommandHandler(IUserRepository userRepository) : IRequestHandler<LoginUserCommand, Result<TokenResponse>> | ||
public class LoginUserCommandHandler(IUserRepository userRepository) : IRequestHandler<LoginUserCommand, ResponseResult<TokenDetailsResponse>> | ||
{ | ||
private readonly IUserRepository _userRepository = userRepository; | ||
|
||
public async Task<Result<TokenResponse>> Handle(LoginUserCommand request, CancellationToken cancellationToken) | ||
public async Task<ResponseResult<TokenDetailsResponse>> Handle(LoginUserCommand request, CancellationToken cancellationToken) | ||
{ | ||
var user = request.LoginUser.ToDomain(); | ||
var tokenDetailsResult = await _userRepository.LoginAsync(request.Tenant, user); | ||
if (tokenDetailsResult.IsSuccess) | ||
{ | ||
return Result<TokenResponse>.Success(tokenDetailsResult.Value.ToTokenResponse()); | ||
return tokenDetailsResult.ToTokenResponse(); | ||
} | ||
|
||
return Result<TokenResponse>.Failure(tokenDetailsResult.Error); | ||
return ResponseResult<TokenDetailsResponse>.Failure(tokenDetailsResult.Error); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...loak.TokenManager/TokenManager.Application.Services/Commands/Users/RefreshTokenCommand.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,7 @@ | ||
using MediatR; | ||
using TokenManager.Application.Services.Responses; | ||
|
||
namespace TokenManager.Application.Services.Commands.Users | ||
{ | ||
public record RefreshTokenCommand(string Tenant, string RefreshToken) : IRequest<ResponseResult<TokenDetailsResponse>>; | ||
} |
18 changes: 18 additions & 0 deletions
18
...kenManager/TokenManager.Application.Services/Commands/Users/RefreshTokenCommandHandler.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,18 @@ | ||
using MediatR; | ||
using TokenManager.Application.Services.Mappers; | ||
using TokenManager.Application.Services.Responses; | ||
using TokenManager.Domain.Interfaces; | ||
|
||
namespace TokenManager.Application.Services.Commands.Users | ||
{ | ||
public class RefreshTokenCommandHandler(IUserRepository userRepository) : IRequestHandler<RefreshTokenCommand, ResponseResult<TokenDetailsResponse>> | ||
{ | ||
private readonly IUserRepository _userRepository = userRepository; | ||
|
||
public async Task<ResponseResult<TokenDetailsResponse>> Handle(RefreshTokenCommand request, CancellationToken cancellationToken) | ||
{ | ||
var tokeDetails = await _userRepository.RefreshTokenAsync(request.Tenant, request.RefreshToken); | ||
return tokeDetails.ToTokenResponse(); | ||
} | ||
} | ||
} |
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
57 changes: 53 additions & 4 deletions
57
...ijuca.Keycloak.TokenManager/TokenManager.Application.Services/Responses/ResponseResult.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 |
---|---|---|
@@ -1,8 +1,57 @@ | ||
namespace TokenManager.Application.Services.Responses | ||
using TokenManager.Domain.Entities; | ||
|
||
namespace TokenManager.Application.Services.Responses | ||
{ | ||
public class ResponseResult<T> | ||
public class ResponseResult | ||
{ | ||
public T? Result { get; set; } | ||
public string? DetailMessage { get; set; } | ||
protected ResponseResult(bool isSuccess, Error error) | ||
{ | ||
if (isSuccess && error != Error.None || | ||
!isSuccess && error == Error.None) | ||
{ | ||
throw new ArgumentException("Invalid error", nameof(error)); | ||
} | ||
|
||
IsSuccess = isSuccess; | ||
Error = error; | ||
} | ||
|
||
public bool IsSuccess { get; } | ||
|
||
public bool IsFailure => !IsSuccess; | ||
|
||
public Error Error { get; } | ||
|
||
public static ResponseResult Success() => new(true, Error.None); | ||
|
||
public static ResponseResult Failure(Error error) => new(false, error); | ||
} | ||
|
||
public class ResponseResult<T> : Result | ||
{ | ||
private readonly T _result; | ||
|
||
private ResponseResult(T value, bool isSuccess, Error error) | ||
: base(isSuccess, error) | ||
{ | ||
_result = value; | ||
} | ||
|
||
public T Result | ||
{ | ||
get | ||
{ | ||
if (!IsSuccess) | ||
{ | ||
throw new InvalidOperationException("No value available for failure result."); | ||
} | ||
|
||
return _result; | ||
} | ||
} | ||
|
||
public static ResponseResult<T> Success(T value) => new(value, true, Error.None); | ||
|
||
public static new ResponseResult<T> Failure(Error error) => new(default!, false, error); | ||
} | ||
} |
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
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
Oops, something went wrong.