|
|
|
@ -6,6 +6,7 @@ import io.entgra.token.validator.dto.AccessTokenDTO;
|
|
|
|
|
import io.entgra.token.validator.dto.TokenDTO;
|
|
|
|
|
import io.entgra.token.validator.dto.ValidationInfoDTO;
|
|
|
|
|
import io.entgra.token.validator.entities.Token;
|
|
|
|
|
import io.entgra.token.validator.exception.TokenCacheException;
|
|
|
|
|
import io.entgra.token.validator.exception.ValidationException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@ -21,13 +22,16 @@ public class TokenService {
|
|
|
|
|
public TokenDAO tokenDAO;
|
|
|
|
|
|
|
|
|
|
public ValidationInfoDTO validateToken(AccessTokenDTO accessTokenDTO) throws ValidationException {
|
|
|
|
|
Token retrievedToken = tokenDAO.findByAccessToken(accessTokenDTO.getAccessToken());
|
|
|
|
|
Token cachedToken = getFromCache(mapToToken(accessTokenDTO));
|
|
|
|
|
Token retrievedToken = cachedToken != null ? cachedToken:
|
|
|
|
|
tokenDAO.findByAccessToken(accessTokenDTO.getAccessToken());
|
|
|
|
|
if (retrievedToken == null) {
|
|
|
|
|
String msg = "Can't validate the access token";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new ValidationException(msg);
|
|
|
|
|
}
|
|
|
|
|
TokenCache.getInstance().add(retrievedToken);
|
|
|
|
|
|
|
|
|
|
if (cachedToken == null) addToCache(retrievedToken);
|
|
|
|
|
String tenantDomain = extractTenantDomain(retrievedToken.getSubjectIdentifier(),
|
|
|
|
|
retrievedToken.getTenantId());
|
|
|
|
|
log.info("Validate the access token owns by " + retrievedToken.getAuthzUser() + "@" + tenantDomain);
|
|
|
|
@ -37,7 +41,7 @@ public class TokenService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TokenDTO renew(TokenDTO tokenDTO) throws ValidationException {
|
|
|
|
|
Token cachedToken = TokenCache.getInstance().get(tokenDTO);
|
|
|
|
|
Token cachedToken = getFromCache(mapToToken(tokenDTO));
|
|
|
|
|
Token retrievedToken = cachedToken != null ? cachedToken :
|
|
|
|
|
tokenDAO.findByRefreshToken(tokenDTO.getRefreshToken());
|
|
|
|
|
if (retrievedToken == null) {
|
|
|
|
@ -45,6 +49,8 @@ public class TokenService {
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new ValidationException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cachedToken == null) addToCache(retrievedToken);
|
|
|
|
|
int expiresIn = retrievedToken.getValidityPeriod() > 0 ?
|
|
|
|
|
(int) retrievedToken.getValidityPeriod() / 1000 : 0;
|
|
|
|
|
log.info("Successfully renewed the token");
|
|
|
|
@ -60,4 +66,44 @@ public class TokenService {
|
|
|
|
|
|
|
|
|
|
return subjectIdentifier.split("@")[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Token mapToToken(AccessTokenDTO accessTokenDTO) {
|
|
|
|
|
if (accessTokenDTO == null) return null;
|
|
|
|
|
|
|
|
|
|
Token token = new Token();
|
|
|
|
|
if (accessTokenDTO.getAccessToken() != null)
|
|
|
|
|
token.setAccessToken(accessTokenDTO.getAccessToken());
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Token mapToToken(TokenDTO tokenDTO) {
|
|
|
|
|
if (tokenDTO == null) return null;
|
|
|
|
|
|
|
|
|
|
Token token = new Token();
|
|
|
|
|
if (tokenDTO.getAccessToken() != null)
|
|
|
|
|
token.setAccessToken(tokenDTO.getAccessToken());
|
|
|
|
|
if (tokenDTO.getRefreshToken() != null)
|
|
|
|
|
token.setRefreshToken(tokenDTO.getRefreshToken());
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addToCache(Token token) throws ValidationException{
|
|
|
|
|
try {
|
|
|
|
|
TokenCache.getInstance().add(token);
|
|
|
|
|
} catch (TokenCacheException e) {
|
|
|
|
|
String msg = "Error occurred while caching";
|
|
|
|
|
throw new ValidationException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Token getFromCache(Token token) throws ValidationException {
|
|
|
|
|
try {
|
|
|
|
|
return TokenCache.getInstance().get(token);
|
|
|
|
|
} catch (TokenCacheException e) {
|
|
|
|
|
String msg = "Error occurred while getting the cached token";
|
|
|
|
|
throw new ValidationException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|