auth token endpoint added
This commit is contained in:
@@ -10,13 +10,13 @@ public class AuthPasswordHandler : IRequestHandler<AuthPasswordCommand, AuthPass
|
||||
{
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
private readonly IPasswordHasher _passwordHasher;
|
||||
private readonly ITokenGenerator _tokenGenerator;
|
||||
private readonly IAccessTokenGenerator _accessTokenGenerator;
|
||||
|
||||
public AuthPasswordHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher, ITokenGenerator tokenGenerator)
|
||||
public AuthPasswordHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher, IAccessTokenGenerator accessTokenGenerator)
|
||||
{
|
||||
_databaseContext = databaseContext;
|
||||
_passwordHasher = passwordHasher;
|
||||
_tokenGenerator = tokenGenerator;
|
||||
_accessTokenGenerator = accessTokenGenerator;
|
||||
}
|
||||
|
||||
public async Task<AuthPasswordResult> Handle(AuthPasswordCommand request, CancellationToken cancellationToken)
|
||||
@@ -32,10 +32,13 @@ public class AuthPasswordHandler : IRequestHandler<AuthPasswordCommand, AuthPass
|
||||
{
|
||||
return AuthPasswordResult.Failure();
|
||||
}
|
||||
|
||||
string accessToken = _accessTokenGenerator.GenerateAccessToken(account);
|
||||
RefreshToken refreshToken = _accessTokenGenerator.GenerateRefreshToken(request.RememberMe);
|
||||
|
||||
account.RefreshTokens.Add(refreshToken);
|
||||
await _databaseContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
string refreshToken = await _tokenGenerator.GenerateRefreshTokenAsync(account, request.RememberMe);
|
||||
string accessToken = _tokenGenerator.GenerateAccessToken(account);
|
||||
|
||||
return AuthPasswordResult.Success(refreshToken, accessToken);
|
||||
return AuthPasswordResult.Success(accessToken, refreshToken.Token.ToString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using MediatR;
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthToken;
|
||||
|
||||
public record AuthTokenCommand
|
||||
(
|
||||
string AccessToken,
|
||||
string RefreshToken
|
||||
)
|
||||
: IRequest<AuthTokenResult>;
|
||||
@@ -0,0 +1,39 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Database;
|
||||
using TimetableDesigner.Backend.Services.Authentication.Database.Model;
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthToken;
|
||||
|
||||
public class AuthTokenHandler : IRequestHandler<AuthTokenCommand, AuthTokenResult>
|
||||
{
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
private readonly IAccessTokenGenerator _accessTokenGenerator;
|
||||
|
||||
public AuthTokenHandler(DatabaseContext databaseContext, IAccessTokenGenerator accessTokenGenerator)
|
||||
{
|
||||
_databaseContext = databaseContext;
|
||||
_accessTokenGenerator = accessTokenGenerator;
|
||||
}
|
||||
|
||||
public async Task<AuthTokenResult> Handle(AuthTokenCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
RefreshToken? token = await _databaseContext.RefreshTokens
|
||||
.Include(x => x.Account)
|
||||
.FirstOrDefaultAsync(x => x.Token == Guid.Parse(request.RefreshToken), cancellationToken);
|
||||
if (token is null || token.ExpirationDate < DateTimeOffset.UtcNow || !_accessTokenGenerator.ValidateExpiredAccessToken(request.AccessToken))
|
||||
{
|
||||
return AuthTokenResult.Failure();
|
||||
}
|
||||
|
||||
string accessToken = _accessTokenGenerator.GenerateAccessToken(token.Account);
|
||||
if (token.IsExtendable)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
return AuthTokenResult.Success(refreshToken, accessToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthToken;
|
||||
|
||||
public record AuthTokenResult
|
||||
{
|
||||
public bool IsSuccess { get; }
|
||||
public string? AccessToken { get; }
|
||||
public string? RefreshToken { get; }
|
||||
|
||||
private AuthTokenResult(bool isSuccess, string? accessToken, string? refreshToken)
|
||||
{
|
||||
IsSuccess = isSuccess;
|
||||
AccessToken = accessToken;
|
||||
RefreshToken = refreshToken;
|
||||
}
|
||||
|
||||
public static AuthTokenResult Success(string accessToken, string refreshToken) =>
|
||||
new AuthTokenResult(true, accessToken, refreshToken);
|
||||
|
||||
public static AuthTokenResult Failure() =>
|
||||
new AuthTokenResult(false, null, null);
|
||||
}
|
||||
Reference in New Issue
Block a user