refresh token generation added

This commit is contained in:
2026-02-02 20:34:29 +01:00
Unverified
parent dc12ee75e5
commit ffa2d68a64
4 changed files with 25 additions and 5 deletions

View File

@@ -33,8 +33,9 @@ public class AuthPasswordHandler : IRequestHandler<AuthPasswordCommand, AuthPass
return AuthPasswordResult.Failure(); return AuthPasswordResult.Failure();
} }
string refreshToken = await _tokenGenerator.GenerateRefreshTokenAsync(account, request.RememberMe);
string accessToken = _tokenGenerator.GenerateAccessToken(account); string accessToken = _tokenGenerator.GenerateAccessToken(account);
return null; return AuthPasswordResult.Success(refreshToken, accessToken);
} }
} }

View File

@@ -5,6 +5,6 @@ namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
public interface ITokenGenerator public interface ITokenGenerator
{ {
string GenerateAccessToken(Account account); string GenerateAccessToken(Account account);
Task<string> GenerateRefreshTokenAsync(Account account); Task<string> GenerateRefreshTokenAsync(Account account, bool isExtendable);
Task<string> ExtendRefreshTokenAsync(); Task<string> ExtendRefreshTokenAsync();
} }

View File

@@ -56,9 +56,28 @@ public class TokenGenerator : ITokenGenerator
return handler.WriteToken(token); return handler.WriteToken(token);
} }
public async Task<string> GenerateRefreshTokenAsync(Account account) public async Task<string> GenerateRefreshTokenAsync(Account account, bool isExtendable)
{ {
return null; 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);
RefreshToken refreshToken = new RefreshToken
{
Token = guid,
ExpirationDate = expirationDate,
IsExtendable = isExtendable,
AccountId = account.Id,
};
await _databaseContext.RefreshTokens.AddAsync(refreshToken);
await _databaseContext.SaveChangesAsync();
return guid.ToString();
} }
public async Task<string> ExtendRefreshTokenAsync() public async Task<string> ExtendRefreshTokenAsync()

View File

@@ -8,5 +8,5 @@ public class Account
public string PasswordSalt { get; set; } = null!; public string PasswordSalt { get; set; } = null!;
public uint Version { get; set; } public uint Version { get; set; }
public virtual IEnumerable<RefreshToken> RefreshTokens { get; set; } = new List<RefreshToken>(); public virtual ICollection<RefreshToken> RefreshTokens { get; set; } = new List<RefreshToken>();
} }