diff --git a/src/main/java/com/ddf/vodsystem/configuration/TimeConfig.java b/src/main/java/com/ddf/vodsystem/configuration/TimeConfig.java new file mode 100644 index 0000000..5e40187 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/configuration/TimeConfig.java @@ -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(); + } +} diff --git a/src/main/java/com/ddf/vodsystem/dto/properties/AuthProperties.java b/src/main/java/com/ddf/vodsystem/dto/properties/AuthProperties.java index 194b40f..da9fd31 100644 --- a/src/main/java/com/ddf/vodsystem/dto/properties/AuthProperties.java +++ b/src/main/java/com/ddf/vodsystem/dto/properties/AuthProperties.java @@ -18,9 +18,10 @@ public record AuthProperties ( /** * - * @param jwt Duration object as to the length of the access token - * @param refreshToken Duration of refresh token - * @param tokenFamily Duration of token family + * @param jwt + * @param refreshToken + * @param tokenFamily + * @param gracePeriod */ public record Expiration ( @NotNull Duration jwt, diff --git a/src/main/java/com/ddf/vodsystem/security/TokenService.java b/src/main/java/com/ddf/vodsystem/security/TokenService.java index b79eeb6..3c6abab 100644 --- a/src/main/java/com/ddf/vodsystem/security/TokenService.java +++ b/src/main/java/com/ddf/vodsystem/security/TokenService.java @@ -13,6 +13,7 @@ import org.springframework.transaction.annotation.Transactional; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.util.HexFormat; @@ -22,6 +23,8 @@ public class TokenService { private static final SecureRandom RNG = new SecureRandom(); private static final HexFormat HEX = HexFormat.of(); + private final Clock clock; + private final TokenFamilyRepository tokenFamilyRepository; private final RefreshTokenRepository refreshTokenRepository; @@ -32,10 +35,12 @@ public class TokenService { public TokenService ( TokenFamilyRepository tokenFamilyRepository, RefreshTokenRepository refreshTokenRepository, - AuthProperties props + AuthProperties props, + Clock clock ) { this.tokenFamilyRepository = tokenFamilyRepository; this.refreshTokenRepository = refreshTokenRepository; + this.clock = clock; this.refreshTokenExpirationMs = props.expiration().refreshToken(); this.tokenFamilyExpirationMs = props.expiration().tokenFamily(); @@ -44,7 +49,7 @@ public class TokenService { @Transactional public String createSession(User user) { - Instant now = Instant.now(); + Instant now = Instant.now(clock); TokenFamily tokenFamily = new TokenFamily(); tokenFamily.setUser(user); @@ -75,7 +80,7 @@ public class TokenService { throw new NotAuthenticated("Malformed refresh token"); } - Instant now = Instant.now(); + Instant now = Instant.now(clock); // Check Token is real RefreshToken refreshToken = refreshTokenRepository.findByHash(hash) @@ -139,7 +144,7 @@ public class TokenService { byte[] token, TokenFamily tokenFamily ) { - Instant now = Instant.now(); + Instant now = Instant.now(clock); RefreshToken refreshToken = new RefreshToken(); refreshToken.setTokenFamily(tokenFamily);