[java-idp-oidc] 03/04: JOIDC-314 - Exploit replay cache for one-time refresh tokens

Codeberg noreply at shibboleth.net
Mon Nov 23 17:10:12 UTC 2026


This is an automated email from the git hooks/post-receive script.

codeberg pushed a commit to branch main
in repository java-idp-oidc.

View the commit online:
https://codeberg.org/Shibboleth/java-idp-oidc/commit/f6100bd8aff63eeeafe31aa230fac5af4a194f69

commit f6100bd8aff63eeeafe31aa230fac5af4a194f69
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Mon Nov 23 18:55:42 2026 +0200

    JOIDC-314 - Exploit replay cache for one-time refresh tokens
    
    https://shibboleth.atlassian.net/browse/JOIDC-314
    
    - Wire EnforceRefreshTokenRotationPredicate: if it returns true, check for replay (similarly to authorization codes) and revoke chain if replay is detected
    - Cover in tests
---
 .../plugin/oidc/op/profile/impl/ValidateGrant.java | 65 +++++++++++++++++++++-
 .../oidc/op/profile/impl/ValidateGrantTest.java    | 18 ++++++
 2 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
index fa15f3bc..0569d5c4 100644
--- a/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
+++ b/idp-oidc-extension-impl/src/main/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrant.java
@@ -46,6 +46,7 @@ import net.shibboleth.idp.plugin.oidc.op.token.support.RefreshTokenClaimsSet;
 import net.shibboleth.idp.plugin.oidc.op.token.support.TokenClaimsSet;
 import net.shibboleth.idp.profile.IdPEventIds;
 import net.shibboleth.oidc.metadata.context.OIDCMetadataContext;
+import net.shibboleth.oidc.profile.config.logic.EnforceRefreshTokenRotationPredicate;
 import net.shibboleth.oidc.profile.config.logic.RefreshTokensEnabledPredicate;
 import net.shibboleth.oidc.profile.config.navigate.RefreshTokenChainLifetimeLookupFunction;
 import net.shibboleth.oidc.profile.config.navigate.RevocationLifetimeLookupFunction;
@@ -101,6 +102,9 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
     /** Predicate used to indicate whether the authorization code or refresh token is revoked. */
     @Nullable private BiPredicate<ProfileRequestContext,JWTClaimsSet> tokenRevocationCondition;
 
+    /** Strategy used to determine whether to revoke refresh tokens once they're used. */
+    @Nonnull private Predicate<ProfileRequestContext> enforceRefreshTokenRotationCondition;
+
     /** The RelyingPartyContext to operate on. */
     @Nullable private RelyingPartyContext rpCtx;
 
@@ -117,6 +121,7 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         ((RevocationLifetimeLookupFunction) chainRevocationLifetimeLookupStrategy).setUseActiveProfileOnly(false);
         refreshTokenChainLifetimeLookupStrategy = new RefreshTokenChainLifetimeLookupFunction();
         dpopProofThumbprintLookupStrategy = new DefaultDPoPProofThumbprintLookupFunction();
+        enforceRefreshTokenRotationCondition = new EnforceRefreshTokenRotationPredicate();
     }
 
     /**
@@ -212,6 +217,18 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         tokenRevocationCondition = condition;
     }
 
+    /**
+     * Set the condition used to determine whether to revoke refresh tokens once they're used.
+     * 
+     * @param condition condition to apply
+     * @since 4.4.0
+     */
+    public void setEnforceRefreshTokenRotationCondition(@Nonnull final Predicate<ProfileRequestContext> condition) {
+        checkSetterPreconditions();
+
+        enforceRefreshTokenRotationCondition = Constraint.isNotNull(condition, "Condition cannot be null");
+    }
+
     /** {@inheritDoc} */
     @Override
     protected void doInitialize() throws ComponentInitializationException {
@@ -410,6 +427,13 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         } else {
             oidcResponseContext.setDpopProofJwkThumbprint(proofThumbprint);
         }
+
+        if (GrantType.REFRESH_TOKEN.equals(grant.getType()) &&
+                enforceRefreshTokenRotationCondition.test(profileRequestContext) &&
+                !checkRefreshTokenReplay(tokenClaimsSet, profileRequestContext)) {
+            ActionSupport.buildEvent(profileRequestContext, OidcEventIds.INVALID_GRANT);
+            return;
+        }
     }
 // Checkstyle: CyclomaticComplexity|MethodLength|ReturnCount ON
 
@@ -448,5 +472,44 @@ public class ValidateGrant extends AbstractOIDCTokenResponseAction {
         }
         return revocationCache.revoke(RevocationCacheContexts.AUTHORIZATION_CODE, id, lifetime);
     }
-    
+
+    /**
+     * Checks that the refresh token has not been used before by exploiting {@link #replayCache}.
+     * 
+     * @param tokenClaimsSet the claims set used for fetching identifiers
+     * @param profileRequestContext profile request context
+     * @return true if the replay-check passed, false otherwise
+     * @since 4.4.0
+     */
+    protected boolean checkRefreshTokenReplay(@Nonnull final TokenClaimsSet tokenClaimsSet,
+            @Nonnull final ProfileRequestContext profileRequestContext) {
+        final String cacheContext = getClass().getName() + ".refreshTokens";
+        final String rootJti = tokenClaimsSet.getRootTokenIdentifier();
+
+        final String claimsSetJti = tokenClaimsSet.getID();
+        assert claimsSetJti != null;
+        assert cacheContext != null;
+        if (!replayCache.check(cacheContext, claimsSetJti, tokenClaimsSet.getExp())) {
+            log.warn("{} Replay detected of refresh token {} when rotation is enabled: revoking the chain",
+                    getLogPrefix(), claimsSetJti);
+            final String rootJtiToUse;
+            if (StringSupport.trimOrNull(rootJti) == null) {
+                log.warn("{} No root token identifier returned, using JWT id for checking revocation status",
+                        getLogPrefix());
+                rootJtiToUse = tokenClaimsSet.getID();
+            } else {
+                rootJtiToUse = rootJti;
+            }
+            assert rootJtiToUse != null;
+
+            if (!revokeChain(rootJtiToUse,
+                    chainRevocationLifetimeLookupStrategy.apply(profileRequestContext))) {
+                log.error("{} Fatal error, unable to save replayed refresh token to revocation cache",
+                        getLogPrefix());
+            }
+            return false;
+        }
+        log.debug("{} Refresh token replay check successful", getLogPrefix());
+        return true;
+    }
 }
diff --git a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
index 70b637f1..74b3b63b 100644
--- a/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
+++ b/idp-oidc-extension-impl/src/test/java/net/shibboleth/idp/plugin/oidc/op/profile/impl/ValidateGrantTest.java
@@ -33,6 +33,7 @@ import java.time.Duration;
 import java.time.Instant;
 import java.util.Collection;
 import java.util.function.Function;
+import java.util.function.Predicate;
 
 import org.mockito.Mockito;
 import org.opensaml.profile.context.ProfileRequestContext;
@@ -71,6 +72,9 @@ public class ValidateGrantTest extends BaseOIDCResponseActionTest {
     
     String rootTokenId;
 
+    @SuppressWarnings("unchecked")
+    Predicate<ProfileRequestContext> enforceRefreshTokenRotation = Mockito.mock(Predicate.class);
+
     @BeforeMethod
     protected void setupStorage() throws ComponentInitializationException {
         storageService = new MemoryStorageService();
@@ -126,6 +130,7 @@ public class ValidateGrantTest extends BaseOIDCResponseActionTest {
         } else {
             action.setRefreshTokensEnabledPredicate(PredicateSupport.alwaysFalse());
         }
+        action.setEnforceRefreshTokenRotationCondition(enforceRefreshTokenRotation);
         action.initialize();
         final TokenRequest req = new TokenRequest(callback, new ClientID(clientId),
                 new AuthorizationCodeGrant(new AuthorizationCode("obsolete"), callback), null);
@@ -259,10 +264,23 @@ public class ValidateGrantTest extends BaseOIDCResponseActionTest {
                 new RefreshTokenGrant(new RefreshToken("obsolete")), null);
         profileRequestCtx.ensureInboundMessageContext().setMessage(req);
         profileRequestCtx.ensureOutboundMessageContext().ensureSubcontext(OIDCAuthenticationResponseContext.class).setAuthorizationGrantClaimsSet(rfClaims);;
+        Mockito.when(enforceRefreshTokenRotation.test(Mockito.any())).thenReturn(false);
         ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
         ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
     }
 
+    @Test
+    public void testRefreshTokenReplayedRotationEnforced() throws Exception {
+        init();
+        final TokenRequest req = new TokenRequest(callback, new ClientID(clientId),
+                new RefreshTokenGrant(new RefreshToken("obsolete")), null);
+        profileRequestCtx.ensureInboundMessageContext().setMessage(req);
+        profileRequestCtx.ensureOutboundMessageContext().ensureSubcontext(OIDCAuthenticationResponseContext.class).setAuthorizationGrantClaimsSet(rfClaims);;
+        Mockito.when(enforceRefreshTokenRotation.test(Mockito.any())).thenReturn(true);
+        ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
+        ActionTestingSupport.assertEvent(action.execute(requestCtx), OidcEventIds.INVALID_GRANT);
+    }
+
     @Test
     public void testRefreshTokenAuthorizationGrantRevoked() throws Exception {
         final RevocationCache revocationCache = buildRevocationCache();

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list