add clock dependency injection
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user