use of net.petafuel.styx.spi.tokentypemapper.spi.TokenTypeMapperSPI in project styx by petafuel.
the class AccessTokenFilter method checkToken.
@Override
public boolean checkToken(String tokenHash) {
AccessToken accessToken;
try {
accessToken = PersistentAccessToken.get(tokenHash);
} catch (PersistenceEmptyResultSetException persistenceEmptyResultSetException) {
// if there was no matching token found in the database, always return unauthorized
ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.UNAUTHORIZED, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
throw new StyxException(responseEntity);
}
if (accessToken.getLastUsedOn() == null && (TimeUnit.MILLISECONDS.toSeconds(new Date().getTime() - accessToken.getCreatedAt().getTime())) > accessToken.getExpiresIn()) {
MasterToken masterToken = PersistentClientApp.get(accessToken.getClientMasterToken());
LOG.warn("Access token expired before first usage, invalidated. master={}, access_token_created={}, serviceBinding={}", masterToken.getName(), accessToken.getCreatedAt(), accessToken.getServiceType());
PersistentAccessToken.setValid(tokenHash, false);
return false;
}
// get master token and check restrictions
MasterToken masterToken = PersistentClientApp.get(accessToken.getClientMasterToken());
checkRestrictions(masterToken, accessToken.getServiceType());
// check if maxUsages is reached
checkMaxUsages(masterToken, accessToken);
// log necessary token information
LOG.info("Request sent with following token information: accessToken={} valid={} serviceType={} usages={} clientReference={} createdAt={} masterTokenName={} masterTokenEnabled={}", accessToken.getId(), accessToken.isValid(), accessToken.getServiceType(), accessToken.getUsages(), accessToken.getClientReference(), accessToken.getCreatedAt(), masterToken.getName(), masterToken.isEnabled());
// get service requirements from Target-Resource class or method
List<XS2ATokenType> serviceRequirements = null;
if (ri.getResourceMethod().getAnnotation(CheckAccessToken.class) != null) {
serviceRequirements = Arrays.asList(ri.getResourceMethod().getAnnotation(CheckAccessToken.class).allowedServices());
} else if (ri.getResourceClass().getAnnotation(CheckAccessToken.class) != null) {
serviceRequirements = Arrays.asList(ri.getResourceClass().getAnnotation(CheckAccessToken.class).allowedServices());
}
// Get all TokenTypeMapperSPI implementations
List<TokenTypeMapperSPI> tokenTypeMapperImpls = new TokenTypeMapperService().providers();
TokenTypeMapperSPI concreteTokenTypeMapper = tokenTypeMapperImpls.stream().filter(tokenTypeMapperSPI -> tokenTypeMapperSPI.getMapping(accessToken.getServiceType()) != null).findFirst().orElse(null);
if (concreteTokenTypeMapper == null || (serviceRequirements != null && !serviceRequirements.contains(concreteTokenTypeMapper.getMapping(accessToken.getServiceType())))) {
if (concreteTokenTypeMapper == null) {
LOG.error("There was not TokenTypeMapperSPI implementation found within the classpath, tokens cannot be validated against access controll");
}
ResponseEntity responseEntity = new ResponseEntity(ResponseConstant.STYX_TOKEN_ACCESS_VIOLATION, ResponseCategory.ERROR, ResponseOrigin.CLIENT);
throw new StyxException(responseEntity);
}
// update lastUsedOn and increase usages of accessToken
if (ri.getResourceClass().isAnnotationPresent(CheckAccessToken.class) && ri.getResourceClass().getAnnotation(CheckAccessToken.class).incrementUsage()) {
PersistentAccessToken.updateLastUsedOn(tokenHash);
}
return accessToken.isValid() && masterToken.isEnabled();
}
use of net.petafuel.styx.spi.tokentypemapper.spi.TokenTypeMapperSPI in project styx by petafuel.
the class TokenTypeValidator method initialize.
@Override
public void initialize(ValidateTokenType constraintAnnotation) {
valueList = new ArrayList<>();
List<TokenTypeMapperSPI> tokenTypeMapperProviders = new TokenTypeMapperService().providers();
if (tokenTypeMapperProviders.isEmpty()) {
LOG.warn("There were no TokenTypeMapperServices found as SPI implementations");
}
tokenTypeMapperProviders.forEach(provider -> valueList.addAll(provider.getAllowedServiceTypes()));
}
Aggregations