Fi caching auth tokens

This commit is contained in:
2024-01-24 18:24:02 +01:00
parent 724c69c6c5
commit ca35777f31
2 changed files with 11 additions and 28 deletions

View File

@@ -162,7 +162,6 @@ public class AuthService {
// Ricreo token di accesso e refresh
StbAuthToken stbAuthToken = refreshTokenService.createRefreshTokenIfNotExists(
null,
username,
password,
String.valueOf(userData.getUser().getKeyGroup()),

View File

@@ -91,15 +91,15 @@ public class RefreshTokenService {
user.setOperation(OperationType.SELECT_OBJECT);
entityProcessor.processEntity(user, multiDBTransactionManager);
applicationEventPublisher.publishEvent(new TokenExpireEvent(profileDb, stbAuthToken.getAccessToken()));
StbAuthToken newRefreshToken = this.createRefreshTokenIfNotExists(
stbAuthToken,
user.getUserName(),
user.getPassword(),
String.valueOf(user.getKeyGroup()),
profileDb);
profileDb,
stbAuthToken.getDeviceId());
applicationEventPublisher.publishEvent(new TokenExpireEvent(profileDb, stbAuthToken.getAccessToken()));
applicationEventPublisher.publishEvent(new TokenCreateEvent(profileDb, newRefreshToken));
return new JwtResponse()
@@ -110,48 +110,32 @@ public class RefreshTokenService {
}
public void verifyExpiration(StbAuthToken token) {
if (token.getExpiryDate() != null && token.getExpiryDate().compareTo(LocalDateTime.now()) < 0) {
if (token.getExpiryDate() != null && token.getExpiryDate().isBefore(LocalDateTime.now())) {
throw new TokenRefreshException(token.getRefreshToken(), "Token scaduto. Esegui un nuovo accesso.");
}
}
public StbAuthToken createRefreshTokenIfNotExists(StbAuthToken oldToken, String username, String password, String keyGroup, String profileDb) throws Exception {
return this.createRefreshTokenIfNotExists(oldToken, username, password, keyGroup, profileDb, null);
}
public StbAuthToken createRefreshTokenIfNotExists(StbAuthToken oldToken, String username, String password, String keyGroup, String profileDb, Long deviceId) throws Exception {
StbAuthToken newToken = oldToken != null ? (StbAuthToken) oldToken.clone() : null;
public StbAuthToken createRefreshTokenIfNotExists(String username, String password, String keyGroup, String profileDb, Long deviceId) throws Exception {
LocalDateTime expiryDate = LocalDateTime.now().plusDays(settingsModel.getRefreshTokenExpireDays());
if (newToken == null && deviceId == null)
if (deviceId == null)
throw new Exception("Impossibile creare un refresh token");
if (newToken == null) {
newToken = new StbAuthToken()
.setDeviceId(deviceId)
.setUserName(username);
newToken.setOperation(OperationType.INSERT);
} else {
newToken
.setOperation(OperationType.UPDATE);
}
UsernamePasswordAuthenticationToken authenticationToken =
createAuthenticationToken(username, password, keyGroup, newToken.getDeviceId() == null ? deviceId : newToken.getDeviceId());
createAuthenticationToken(username, password, keyGroup, deviceId);
Authentication authentication = authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
newToken
StbAuthToken newToken = new StbAuthToken()
.setDeviceId(deviceId)
.setUserName(username)
.setExpiryDate(expiryDate)
.setRefreshToken(Encoders.BASE64.encode(Keys.secretKeyFor(SignatureAlgorithm.HS512).getEncoded()))
.setAccessToken(accessTokenProvider.createToken(profileDb));
newToken.setOperation(OperationType.INSERT);
entityProcessor.processEntity(newToken, multiDBTransactionManager);