Search in sources :

Example 6 with ActionTokenValueEntity

use of org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity in project keycloak by keycloak.

the class InfinispanActionTokenStoreProvider method put.

@Override
public void put(ActionTokenKeyModel key, Map<String, String> notes) {
    if (key == null || key.getUserId() == null || key.getActionId() == null) {
        return;
    }
    ActionTokenReducedKey tokenKey = new ActionTokenReducedKey(key.getUserId(), key.getActionId(), key.getActionVerificationNonce());
    ActionTokenValueEntity tokenValue = new ActionTokenValueEntity(notes);
    LOG.debugf("Adding used action token to actionTokens cache: %s", tokenKey.toString());
    this.tx.put(actionKeyCache, tokenKey, tokenValue, key.getExpiration() - Time.currentTime(), TimeUnit.SECONDS);
}
Also used : ActionTokenReducedKey(org.keycloak.models.sessions.infinispan.entities.ActionTokenReducedKey) ActionTokenValueEntity(org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity)

Example 7 with ActionTokenValueEntity

use of org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity in project keycloak by keycloak.

the class InfinispanSingleUseTokenStoreProvider method putIfAbsent.

@Override
public boolean putIfAbsent(String tokenId, int lifespanInSeconds) {
    ActionTokenValueEntity tokenValue = new ActionTokenValueEntity(null);
    // Rather keep the items in the cache for a bit longer
    lifespanInSeconds = lifespanInSeconds + 10;
    try {
        BasicCache<String, ActionTokenValueEntity> cache = tokenCache.get();
        long lifespanMs = InfinispanUtil.toHotrodTimeMs(cache, Time.toMillis(lifespanInSeconds));
        ActionTokenValueEntity existing = cache.putIfAbsent(tokenId, tokenValue, lifespanMs, TimeUnit.MILLISECONDS);
        return existing == null;
    } catch (HotRodClientException re) {
        // No need to retry. The hotrod (remoteCache) has some retries in itself in case of some random network error happened.
        // In case of lock conflict, we don't want to retry anyway as there was likely an attempt to use the token from different place.
        logger.debugf(re, "Failed when adding token %s", tokenId);
        return false;
    }
}
Also used : ActionTokenValueEntity(org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity) HotRodClientException(org.infinispan.client.hotrod.exceptions.HotRodClientException)

Example 8 with ActionTokenValueEntity

use of org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity in project keycloak by keycloak.

the class InfinispanTokenRevocationStoreProvider method putRevokedToken.

@Override
public void putRevokedToken(String tokenId, long lifespanSeconds) {
    Map<String, String> data = Collections.singletonMap(REVOKED_KEY, "true");
    ActionTokenValueEntity tokenValue = new ActionTokenValueEntity(data);
    try {
        BasicCache<String, ActionTokenValueEntity> cache = tokenCache.get();
        long lifespanMs = InfinispanUtil.toHotrodTimeMs(cache, Time.toMillis(lifespanSeconds + 1));
        cache.put(tokenId, tokenValue, lifespanMs, TimeUnit.MILLISECONDS);
    } catch (HotRodClientException re) {
        // No need to retry. The hotrod (remoteCache) has some retries in itself in case of some random network error happened.
        if (logger.isDebugEnabled()) {
            logger.debugf(re, "Failed when adding revoked token %s", tokenId);
        }
        throw re;
    }
}
Also used : ActionTokenValueEntity(org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity) HotRodClientException(org.infinispan.client.hotrod.exceptions.HotRodClientException)

Example 9 with ActionTokenValueEntity

use of org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity in project keycloak by keycloak.

the class InfinispanActionTokenStoreProviderFactory method initActionTokenCache.

private static Cache<ActionTokenReducedKey, ActionTokenValueEntity> initActionTokenCache(KeycloakSession session) {
    InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
    Cache<ActionTokenReducedKey, ActionTokenValueEntity> cache = connections.getCache(InfinispanConnectionProvider.ACTION_TOKEN_CACHE);
    return cache;
}
Also used : ActionTokenReducedKey(org.keycloak.models.sessions.infinispan.entities.ActionTokenReducedKey) ActionTokenValueEntity(org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider)

Example 10 with ActionTokenValueEntity

use of org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity in project keycloak by keycloak.

the class InfinispanOAuth2DeviceTokenStoreProvider method isPollingAllowed.

@Override
public boolean isPollingAllowed(OAuth2DeviceCodeModel deviceCode) {
    try {
        BasicCache<String, ActionTokenValueEntity> cache = codeCache.get();
        String key = deviceCode.serializePollingKey();
        ActionTokenValueEntity value = new ActionTokenValueEntity(null);
        ActionTokenValueEntity existing = cache.putIfAbsent(key, value, deviceCode.getPollingInterval(), TimeUnit.SECONDS);
        return existing == null;
    } catch (HotRodClientException re) {
        // In case of lock conflict, we don't want to retry anyway as there was likely an attempt to remove the code from different place.
        if (logger.isDebugEnabled()) {
            logger.debugf(re, "Failed when putting polling key for device code %s", deviceCode.getDeviceCode());
        }
        return false;
    }
}
Also used : ActionTokenValueEntity(org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity) HotRodClientException(org.infinispan.client.hotrod.exceptions.HotRodClientException)

Aggregations

ActionTokenValueEntity (org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity)18 HotRodClientException (org.infinispan.client.hotrod.exceptions.HotRodClientException)14 UUID (java.util.UUID)4 ActionTokenReducedKey (org.keycloak.models.sessions.infinispan.entities.ActionTokenReducedKey)3 OAuth2DeviceCodeModel (org.keycloak.models.OAuth2DeviceCodeModel)2 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)1 OAuth2DeviceUserCodeModel (org.keycloak.models.OAuth2DeviceUserCodeModel)1