Search in sources :

Example 1 with ApiTokenValidityUpdateEntity

use of com.gw2auth.oauth2.server.repository.apitoken.ApiTokenValidityUpdateEntity in project oauth2-server by gw2auth.

the class ApiTokenServiceImpl method checkTokenValidity.

@Scheduled(fixedRate = 1000L * 60L * 5L)
public void checkTokenValidity() {
    final Instant now = this.clock.instant();
    final Instant offset = now.minus(VALIDITY_CHECK_INTERVAL);
    final List<ApiTokenEntity> tokensToCheck = this.apiTokenRepository.findAllByLastValidCheckTimeLTE(offset, VALIDITY_CHECK_BATCH_SIZE);
    final List<ApiTokenValidityUpdateEntity> updateEntities = new ArrayList<>(tokensToCheck.size());
    final int[] counts = new int[3];
    for (ApiTokenEntity apiTokenEntity : tokensToCheck) {
        Boolean isValidState;
        try {
            this.gw2ApiService.getTokenInfo(apiTokenEntity.gw2ApiToken());
            isValidState = true;
            counts[0]++;
        } catch (InvalidApiTokenException e) {
            isValidState = false;
            counts[1]++;
        } catch (Gw2ApiServiceException e) {
            isValidState = null;
            counts[2]++;
        }
        if (isValidState != null) {
            updateEntities.add(new ApiTokenValidityUpdateEntity(apiTokenEntity.accountId(), apiTokenEntity.gw2AccountId(), isValidState));
        }
    }
    this.apiTokenRepository.updateApiTokensValid(now, updateEntities);
    LOG.info("updated API-Token validity for {} API-Tokens; valid={} invalid={} unknown={}", tokensToCheck.size(), counts[0], counts[1], counts[2]);
}
Also used : ApiTokenEntity(com.gw2auth.oauth2.server.repository.apitoken.ApiTokenEntity) ApiTokenValidityUpdateEntity(com.gw2auth.oauth2.server.repository.apitoken.ApiTokenValidityUpdateEntity) Instant(java.time.Instant) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

ApiTokenEntity (com.gw2auth.oauth2.server.repository.apitoken.ApiTokenEntity)1 ApiTokenValidityUpdateEntity (com.gw2auth.oauth2.server.repository.apitoken.ApiTokenValidityUpdateEntity)1 Instant (java.time.Instant)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1