add clock dependency injection
All checks were successful
CI / Maven unit tests (push) Successful in 15s
CI / Build and push images (push) Has been skipped
CI / Deploy to server (push) Has been skipped

This commit is contained in:
2026-10-07 19:03:47 +01:00
parent 8166c62bb1
commit e2a2a60075
3 changed files with 28 additions and 7 deletions

View File

@@ -0,0 +1,15 @@
package com.ddf.vodsystem.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.Clock;
@Configuration
public class TimeConfig {
@Bean
public Clock clock() {
return Clock.systemUTC();
}
}

View File

@@ -18,9 +18,10 @@ public record AuthProperties (
/** /**
* *
* @param jwt Duration object as to the length of the access token * @param jwt
* @param refreshToken Duration of refresh token * @param refreshToken
* @param tokenFamily Duration of token family * @param tokenFamily
* @param gracePeriod
*/ */
public record Expiration ( public record Expiration (
@NotNull Duration jwt, @NotNull Duration jwt,

View File

@@ -13,6 +13,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.Clock;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.HexFormat; import java.util.HexFormat;
@@ -22,6 +23,8 @@ public class TokenService {
private static final SecureRandom RNG = new SecureRandom(); private static final SecureRandom RNG = new SecureRandom();
private static final HexFormat HEX = HexFormat.of(); private static final HexFormat HEX = HexFormat.of();
private final Clock clock;
private final TokenFamilyRepository tokenFamilyRepository; private final TokenFamilyRepository tokenFamilyRepository;
private final RefreshTokenRepository refreshTokenRepository; private final RefreshTokenRepository refreshTokenRepository;
@@ -32,10 +35,12 @@ public class TokenService {
public TokenService ( public TokenService (
TokenFamilyRepository tokenFamilyRepository, TokenFamilyRepository tokenFamilyRepository,
RefreshTokenRepository refreshTokenRepository, RefreshTokenRepository refreshTokenRepository,
AuthProperties props AuthProperties props,
Clock clock
) { ) {
this.tokenFamilyRepository = tokenFamilyRepository; this.tokenFamilyRepository = tokenFamilyRepository;
this.refreshTokenRepository = refreshTokenRepository; this.refreshTokenRepository = refreshTokenRepository;
this.clock = clock;
this.refreshTokenExpirationMs = props.expiration().refreshToken(); this.refreshTokenExpirationMs = props.expiration().refreshToken();
this.tokenFamilyExpirationMs = props.expiration().tokenFamily(); this.tokenFamilyExpirationMs = props.expiration().tokenFamily();
@@ -44,7 +49,7 @@ public class TokenService {
@Transactional @Transactional
public String createSession(User user) { public String createSession(User user) {
Instant now = Instant.now(); Instant now = Instant.now(clock);
TokenFamily tokenFamily = new TokenFamily(); TokenFamily tokenFamily = new TokenFamily();
tokenFamily.setUser(user); tokenFamily.setUser(user);
@@ -75,7 +80,7 @@ public class TokenService {
throw new NotAuthenticated("Malformed refresh token"); throw new NotAuthenticated("Malformed refresh token");
} }
Instant now = Instant.now(); Instant now = Instant.now(clock);
// Check Token is real // Check Token is real
RefreshToken refreshToken = refreshTokenRepository.findByHash(hash) RefreshToken refreshToken = refreshTokenRepository.findByHash(hash)
@@ -139,7 +144,7 @@ public class TokenService {
byte[] token, byte[] token,
TokenFamily tokenFamily TokenFamily tokenFamily
) { ) {
Instant now = Instant.now(); Instant now = Instant.now(clock);
RefreshToken refreshToken = new RefreshToken(); RefreshToken refreshToken = new RefreshToken();
refreshToken.setTokenFamily(tokenFamily); refreshToken.setTokenFamily(tokenFamily);