io.quarkus.oidc.runtime.DefaultTokenIntrospectionUserInfoCache Maven / Gradle / Ivy
Show all versions of quarkus-oidc Show documentation
package io.quarkus.oidc.runtime;
import jakarta.enterprise.event.Observes;
import io.quarkus.oidc.OidcRequestContext;
import io.quarkus.oidc.OidcTenantConfig;
import io.quarkus.oidc.TokenIntrospection;
import io.quarkus.oidc.TokenIntrospectionCache;
import io.quarkus.oidc.UserInfo;
import io.quarkus.oidc.UserInfoCache;
import io.quarkus.runtime.ShutdownEvent;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Vertx;
/**
* Default TokenIntrospection and UserInfo Cache implementation.
* A single cache entry can keep TokenIntrospection and/or UserInfo.
*
* In most cases it is the opaque bearer access tokens which are introspected
* but the code flow access tokens can also be introspected if they have the roles claims.
*
* In either case, if a remote request to fetch UserInfo is required then it will be the same access token
* which has been introspected which will be used to request UserInfo.
*/
public class DefaultTokenIntrospectionUserInfoCache implements TokenIntrospectionCache, UserInfoCache {
private static final Uni NULL_INTROSPECTION_UNI = Uni.createFrom().nullItem();
private static final Uni NULL_USERINFO_UNI = Uni.createFrom().nullItem();
final MemoryCache cache;
public DefaultTokenIntrospectionUserInfoCache(OidcConfig oidcConfig, Vertx vertx) {
cache = new MemoryCache(vertx, oidcConfig.tokenCache.cleanUpTimerInterval,
oidcConfig.tokenCache.timeToLive, oidcConfig.tokenCache.maxSize);
}
@Override
public Uni addIntrospection(String token, TokenIntrospection introspection, OidcTenantConfig oidcTenantConfig,
OidcRequestContext requestContext) {
CacheEntry entry = cache.get(token);
if (entry != null) {
entry.introspection = introspection;
} else {
cache.add(token, new CacheEntry(introspection));
}
return CodeAuthenticationMechanism.VOID_UNI;
}
@Override
public Uni getIntrospection(String token, OidcTenantConfig oidcConfig,
OidcRequestContext requestContext) {
CacheEntry entry = cache.get(token);
return entry == null ? NULL_INTROSPECTION_UNI : Uni.createFrom().item(entry.introspection);
}
@Override
public Uni addUserInfo(String token, UserInfo userInfo, OidcTenantConfig oidcTenantConfig,
OidcRequestContext requestContext) {
CacheEntry entry = cache.get(token);
if (entry != null) {
entry.userInfo = userInfo;
} else {
cache.add(token, new CacheEntry(userInfo));
}
return CodeAuthenticationMechanism.VOID_UNI;
}
@Override
public Uni getUserInfo(String token, OidcTenantConfig oidcConfig,
OidcRequestContext requestContext) {
CacheEntry entry = cache.get(token);
return entry == null ? NULL_USERINFO_UNI : Uni.createFrom().item(entry.userInfo);
}
private static class CacheEntry {
volatile TokenIntrospection introspection;
volatile UserInfo userInfo;
public CacheEntry(TokenIntrospection introspection) {
this.introspection = introspection;
}
public CacheEntry(UserInfo userInfo) {
this.userInfo = userInfo;
}
}
public void clearCache() {
cache.clearCache();
}
public int getCacheSize() {
return cache.getCacheSize();
}
void shutdown(@Observes ShutdownEvent event, Vertx vertx) {
cache.stopTimer(vertx);
}
}