|
| 1 | +package com.cloudmedia.identity.auth.service; |
| 2 | + |
| 3 | +import com.cloudmedia.identity.auth.config.AuthProperties; |
| 4 | +import com.cloudmedia.identity.auth.token.JwtAccessTokenService; |
| 5 | +import com.cloudmedia.identity.auth.token.RefreshTokenGenerator; |
| 6 | +import com.cloudmedia.identity.auth.token.RefreshTokenHasher; |
| 7 | +import com.cloudmedia.identity.error.ApiException; |
| 8 | +import com.cloudmedia.identity.persistence.entity.RefreshTokenEntity; |
| 9 | +import com.cloudmedia.identity.persistence.entity.SessionEntity; |
| 10 | +import com.cloudmedia.identity.persistence.repository.RefreshTokenRepository; |
| 11 | +import java.time.Instant; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.time.ZoneOffset; |
| 14 | +import java.util.List; |
| 15 | +import java.util.UUID; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.stereotype.Service; |
| 18 | +import org.springframework.transaction.annotation.Transactional; |
| 19 | + |
| 20 | +@Service |
| 21 | +public class AuthRefreshService implements AuthRefreshUseCase { |
| 22 | + |
| 23 | + private final AuthProperties authProperties; |
| 24 | + private final JwtAccessTokenService jwtAccessTokenService; |
| 25 | + private final RefreshTokenGenerator refreshTokenGenerator; |
| 26 | + private final RefreshTokenHasher refreshTokenHasher; |
| 27 | + private final RefreshTokenRepository refreshTokenRepository; |
| 28 | + private final SessionLifecycleService sessionLifecycleService; |
| 29 | + |
| 30 | + public AuthRefreshService(AuthProperties authProperties, JwtAccessTokenService jwtAccessTokenService, |
| 31 | + RefreshTokenGenerator refreshTokenGenerator, RefreshTokenHasher refreshTokenHasher, |
| 32 | + RefreshTokenRepository refreshTokenRepository, SessionLifecycleService sessionLifecycleService) { |
| 33 | + this.authProperties = authProperties; |
| 34 | + this.jwtAccessTokenService = jwtAccessTokenService; |
| 35 | + this.refreshTokenGenerator = refreshTokenGenerator; |
| 36 | + this.refreshTokenHasher = refreshTokenHasher; |
| 37 | + this.refreshTokenRepository = refreshTokenRepository; |
| 38 | + this.sessionLifecycleService = sessionLifecycleService; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + @Transactional |
| 43 | + public RefreshResult rotateRefreshToken(String rawRefreshToken) { |
| 44 | + LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC); |
| 45 | + String tokenHash = refreshTokenHasher.hash(rawRefreshToken); |
| 46 | + |
| 47 | + RefreshTokenEntity currentToken = refreshTokenRepository.findByTokenHash(tokenHash) |
| 48 | + .orElseThrow(() -> unauthorized("REFRESH_TOKEN_INVALID", "Refresh token is invalid")); |
| 49 | + |
| 50 | + if (currentToken.getRevokedAt() != null) { |
| 51 | + handleReuseDetection(currentToken, now); |
| 52 | + throw unauthorized("REFRESH_TOKEN_REUSED", "Refresh token reuse detected"); |
| 53 | + } |
| 54 | + |
| 55 | + SessionEntity session = currentToken.getSession(); |
| 56 | + if (session.getRevokedAt() != null || currentToken.getExpiresAt().isBefore(now) |
| 57 | + || session.getExpiresAt().isBefore(now)) { |
| 58 | + throw unauthorized("REFRESH_TOKEN_EXPIRED", "Refresh token is expired or revoked"); |
| 59 | + } |
| 60 | + |
| 61 | + String newRawRefreshToken = refreshTokenGenerator.generate(); |
| 62 | + RefreshTokenEntity newToken = new RefreshTokenEntity(); |
| 63 | + newToken.setId(UUID.randomUUID().toString()); |
| 64 | + newToken.setSession(session); |
| 65 | + newToken.setTokenHash(refreshTokenHasher.hash(newRawRefreshToken)); |
| 66 | + newToken.setFamilyId(currentToken.getFamilyId()); |
| 67 | + newToken.setParentToken(currentToken); |
| 68 | + newToken.setIssuedAt(now); |
| 69 | + newToken.setExpiresAt(now.plus(authProperties.getRefreshTokenTtl())); |
| 70 | + |
| 71 | + RefreshTokenEntity persistedNewToken = refreshTokenRepository.save(newToken); |
| 72 | + |
| 73 | + currentToken.setRevokedAt(now); |
| 74 | + currentToken.setReplacedBy(persistedNewToken); |
| 75 | + refreshTokenRepository.save(currentToken); |
| 76 | + |
| 77 | + String accessToken = jwtAccessTokenService.issueAccessToken(session.getUser().getId(), session.getId(), |
| 78 | + Instant.now()); |
| 79 | + return new RefreshResult(accessToken, newRawRefreshToken, session.getId()); |
| 80 | + } |
| 81 | + |
| 82 | + private void handleReuseDetection(RefreshTokenEntity currentToken, LocalDateTime now) { |
| 83 | + List<RefreshTokenEntity> activeFamilyTokens = refreshTokenRepository |
| 84 | + .findByFamilyIdAndRevokedAtIsNull(currentToken.getFamilyId()); |
| 85 | + for (RefreshTokenEntity token : activeFamilyTokens) { |
| 86 | + token.setRevokedAt(now); |
| 87 | + } |
| 88 | + refreshTokenRepository.saveAll(activeFamilyTokens); |
| 89 | + |
| 90 | + sessionLifecycleService.revokeSessionAndActiveTokens(currentToken.getSession(), now); |
| 91 | + } |
| 92 | + |
| 93 | + private ApiException unauthorized(String code, String message) { |
| 94 | + return new ApiException(HttpStatus.UNAUTHORIZED, code, message, null); |
| 95 | + } |
| 96 | +} |
0 commit comments