auth token endpoint finished
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
||||
|
||||
public interface IAccessTokenGenerator
|
||||
public interface ITokenHelper
|
||||
{
|
||||
string GenerateAccessToken(Account account);
|
||||
RefreshToken GenerateRefreshToken(bool isExtendable);
|
||||
string GenerateAccessToken(long accountId);
|
||||
bool ValidateExpiredAccessToken(string accessToken);
|
||||
DateTimeOffset CalculateRefreshTokenExpirationDate(bool isExtendable = true);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
||||
|
||||
public record PasswordHashData(
|
||||
byte[] Hash,
|
||||
string Salt
|
||||
);
|
||||
@@ -36,4 +36,9 @@ public class PasswordHasher : IPasswordHasher
|
||||
byte[] hash = hashFunction.GetBytes(32);
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record PasswordHashData(
|
||||
byte[] Hash,
|
||||
string Salt
|
||||
);
|
||||
@@ -9,18 +9,16 @@ using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegiste
|
||||
|
||||
namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
|
||||
|
||||
public class AccessTokenGenerator : IAccessTokenGenerator
|
||||
public class TokenHelper : ITokenHelper
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly DatabaseContext _databaseContext;
|
||||
|
||||
public AccessTokenGenerator(IConfiguration configuration, DatabaseContext databaseContext)
|
||||
public TokenHelper(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_databaseContext = databaseContext;
|
||||
}
|
||||
|
||||
public string GenerateAccessToken(Account account)
|
||||
public string GenerateAccessToken(long accountId)
|
||||
{
|
||||
IConfigurationSection accessTokenSettings = _configuration.GetSection("Tokens")
|
||||
.GetSection("AccessToken");
|
||||
@@ -40,7 +38,7 @@ public class AccessTokenGenerator : IAccessTokenGenerator
|
||||
Subject = new ClaimsIdentity(
|
||||
[
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, account.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, accountId.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Exp, expirationDate.UtcTicks.ToString())
|
||||
]),
|
||||
Issuer = accessTokenSettings.GetValue<string>("Issuer"),
|
||||
@@ -56,25 +54,6 @@ public class AccessTokenGenerator : IAccessTokenGenerator
|
||||
return handler.WriteToken(token);
|
||||
}
|
||||
|
||||
public RefreshToken GenerateRefreshToken(bool isExtendable)
|
||||
{
|
||||
string lifetimeSection = isExtendable ? "Extended" : "Normal";
|
||||
int lifetime = _configuration.GetSection("Tokens")
|
||||
.GetSection("RefreshToken")
|
||||
.GetSection("Lifetime")
|
||||
.GetValue<int>(lifetimeSection);
|
||||
|
||||
Guid guid = Guid.NewGuid();
|
||||
DateTimeOffset expirationDate = DateTimeOffset.UtcNow.AddMinutes(lifetime);
|
||||
|
||||
return new RefreshToken
|
||||
{
|
||||
Token = guid,
|
||||
ExpirationDate = expirationDate,
|
||||
IsExtendable = isExtendable,
|
||||
};
|
||||
}
|
||||
|
||||
public bool ValidateExpiredAccessToken(string accessToken)
|
||||
{
|
||||
IConfigurationSection accessTokenSettings = _configuration.GetSection("Tokens")
|
||||
@@ -104,4 +83,14 @@ public class AccessTokenGenerator : IAccessTokenGenerator
|
||||
|
||||
return jwtSecurityToken is not null && jwtSecurityToken.Header.Alg.Equals(algorithm, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
|
||||
public DateTimeOffset CalculateRefreshTokenExpirationDate(bool isExtendable = true)
|
||||
{
|
||||
string lifetimeSection = isExtendable ? "Extended" : "Normal";
|
||||
int lifetime = _configuration.GetSection("Tokens")
|
||||
.GetSection("RefreshToken")
|
||||
.GetSection("Lifetime")
|
||||
.GetValue<int>(lifetimeSection);
|
||||
return DateTimeOffset.UtcNow.AddMinutes(lifetime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user