Skip to content

Commit

Permalink
fix(oxauth): removed deprecated ClientTokens and SessionTokens classe…
Browse files Browse the repository at this point in the history
…s which leads to growing load on cache service #86

fix(oxauth): removed deprecated ClientTokens and SessionTokens classes which leads to growing load on cache service #86
  • Loading branch information
yuriyz authored Jan 23, 2025
2 parents d1c11a6 + c70d9a9 commit 487e68a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ public List<AuthorizationGrant> getAuthorizationGrant(String clientId) {
try {
final List<TokenLdap> entries = new ArrayList<TokenLdap>();
entries.addAll(grantService.getGrantsOfClient(clientId));
entries.addAll(grantService.getCacheClientTokensEntries(clientId));

for (TokenLdap t : entries) {
final AuthorizationGrant grant = asGrant(t);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.apache.commons.lang.StringUtils;
import org.gluu.oxauth.model.common.AuthorizationGrant;
import org.gluu.oxauth.model.common.CacheGrant;
import org.gluu.oxauth.model.common.ClientTokens;
import org.gluu.oxauth.model.common.SessionTokens;
import org.gluu.oxauth.model.config.StaticConfiguration;
import org.gluu.oxauth.model.configuration.AppConfiguration;
import org.gluu.oxauth.model.ldap.TokenLdap;
Expand Down Expand Up @@ -109,9 +107,6 @@ private boolean shouldPutInCache(TokenType tokenType, boolean isImplicitFlow) {

public void persist(TokenLdap token) {
if (shouldPutInCache(token.getTokenTypeEnum(), token.isImplicitFlow())) {
ClientTokens clientTokens = getCacheClientTokens(token.getClientId());
clientTokens.getTokenHashes().add(token.getTokenCode());

int expiration = appConfiguration.getDynamicRegistrationExpirationTime(); // fallback to client's lifetime
switch (token.getTokenTypeEnum()) {
case ID_TOKEN:
Expand All @@ -129,6 +124,11 @@ public void persist(TokenLdap token) {
lifetime = client.getAccessTokenLifetime();
}
expiration = lifetime;

// because of `SessionTokens` drop we ALWAYS persist access_token into DB to be able
// to query it by sessionDn (when /end_session is called or for other cases
// when we need to get all session's tokens )
ldapEntryManager.persist(token);
break;
case AUTHORIZATION_CODE:
expiration = appConfiguration.getAuthorizationCodeLifetime();
Expand All @@ -137,40 +137,12 @@ public void persist(TokenLdap token) {

token.setIsFromCache(true);
cacheService.put(expiration, token.getTokenCode(), token);
cacheService.put(expiration, clientTokens.cacheKey(), clientTokens);

if (StringUtils.isNotBlank(token.getSessionDn())) {
SessionTokens sessionTokens = getCacheSessionTokens(token.getSessionDn());
sessionTokens.getTokenHashes().add(token.getTokenCode());

cacheService.put(expiration, sessionTokens.cacheKey(), sessionTokens);
}
return;
}

ldapEntryManager.persist(token);
}

public ClientTokens getCacheClientTokens(String clientId) {
ClientTokens clientTokens = new ClientTokens(clientId);
Object o = cacheService.get(clientTokens.cacheKey());
if (o instanceof ClientTokens) {
return (ClientTokens) o;
} else {
return clientTokens;
}
}

public SessionTokens getCacheSessionTokens(String sessionDn) {
SessionTokens sessionTokens = new SessionTokens(sessionDn);
Object o = cacheService.get(sessionTokens.cacheKey());
if (o instanceof SessionTokens) {
return (SessionTokens) o;
} else {
return sessionTokens;
}
}

public void remove(TokenLdap p_token) {
if (p_token.isFromCache()) {
cacheService.remove(p_token.getTokenCode());
Expand Down Expand Up @@ -277,7 +249,6 @@ public List<TokenLdap> getGrantsBySessionDn(String sessionDn) {
if (ldapGrants != null) {
grants.addAll(ldapGrants);
}
grants.addAll(getGrantsFromCacheBySessionDn(sessionDn));
} catch (Exception e) {
logException(e);
}
Expand All @@ -292,24 +263,6 @@ private void logException(Exception e) {
}
}

public List<TokenLdap> getGrantsFromCacheBySessionDn(String sessionDn) {
if (StringUtils.isBlank(sessionDn)) {
return Collections.emptyList();
}
return getCacheTokensEntries(getCacheSessionTokens(sessionDn).getTokenHashes());
}

public List<TokenLdap> getCacheClientTokensEntries(String clientId) {
if (cacheConfiguration.getCacheProviderType() == CacheProviderType.NATIVE_PERSISTENCE) {
return Collections.emptyList();
}
Object o = cacheService.get(new ClientTokens(clientId).cacheKey());
if (o instanceof ClientTokens) {
return getCacheTokensEntries(((ClientTokens) o).getTokenHashes());
}
return Collections.emptyList();
}

public List<TokenLdap> getCacheTokensEntries(Set<String> tokenHashes) {
List<TokenLdap> tokens = new ArrayList<>();

Expand Down

0 comments on commit 487e68a

Please sign in to comment.