WIP: feat(security): implement refresh tokens #49

Closed
dylan wants to merge 25 commits from feat/add-refresh-tokens into master
3 changed files with 28 additions and 7 deletions
Showing only changes of commit e2a2a60075 - Show all commits

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 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,

View File

@@ -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);