[java-oidc-common] branch main updated: JCOMOIDC-191 - PKCE Code Challenge Method Plain Allowed When OP Supports S256
Codeberg
noreply at shibboleth.net
Thu Nov 19 15:44:42 UTC 2026
This is an automated email from the git hooks/post-receive script.
codeberg pushed a commit to branch main
in repository java-oidc-common.
View the commit online:
https://codeberg.org/Shibboleth/java-oidc-common/commit/15f641de6c0fae9b8ea506d59b26ec872cb0e140
The following commit(s) were added to refs/heads/main by this push:
new 15f641de JCOMOIDC-191 - PKCE Code Challenge Method Plain Allowed When OP Supports S256
15f641de is described below
commit 15f641de6c0fae9b8ea506d59b26ec872cb0e140
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Thu Nov 19 15:44:31 2026 +0000
JCOMOIDC-191 - PKCE Code Challenge Method Plain Allowed When OP Supports
S256
- PLAIN would only ever be used if the deployer allowed it in profile
config and the OP only supports PLAIN.
https://shibboleth.atlassian.net/browse/JCOMOIDC-191
---
.../impl/AddPKCECodeVerifierAndChallenge.java | 23 +++-
.../impl/AddPKCECodeVerifierAndChallengeTest.java | 148 +++++++++++++++++++++
2 files changed, 169 insertions(+), 2 deletions(-)
diff --git a/oidc-common-profile-impl/src/main/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallenge.java b/oidc-common-profile-impl/src/main/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallenge.java
index 79a14b4b..dde3f9f0 100644
--- a/oidc-common-profile-impl/src/main/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallenge.java
+++ b/oidc-common-profile-impl/src/main/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallenge.java
@@ -18,6 +18,7 @@ package net.shibboleth.oidc.profile.messaging.handler.impl;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
+import java.util.List;
import javax.annotation.Nonnull;
@@ -57,8 +58,25 @@ public class AddPKCECodeVerifierAndChallenge
log.trace("{} PKCE enabled, adding code_challenge to request", getLogPrefix());
- final CodeChallengeMethod method =
- pkceOptions.isAllowPlain() ? CodeChallengeMethod.PLAIN : CodeChallengeMethod.S256;
+ final List<com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod> supportedMethods =
+ getProviderMetadata().getCodeChallengeMethods();
+
+ log.trace("{} Provider supports '{}' PKCE code challenge methods", getLogPrefix(), supportedMethods);
+
+ // Default, always support S256 because as a client we can.
+ CodeChallengeMethod method = CodeChallengeMethod.S256;
+
+ final boolean supportsS256 =
+ supportedMethods == null ? false:
+ supportedMethods.contains(com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod.S256);
+ final boolean supportsPlain =
+ supportedMethods == null ? false:
+ supportedMethods.contains(com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod.PLAIN);
+
+ // Only switch to PLAIN if allowPlain is true and the OP allows it.
+ if ((supportsPlain && !supportsS256) && pkceOptions.isAllowPlain()) {
+ method = CodeChallengeMethod.PLAIN;
+ }
final String codeVerifier = generateCodeVerifier(32);
final String challenge = computeCodeChallenge(codeVerifier, method);
@@ -69,6 +87,7 @@ public class AddPKCECodeVerifierAndChallenge
log.trace("{} Derived code challenge '...{}'", getLogPrefix(),
challenge.substring(challenge.length()-3));
}
+ log.trace("{} PKCE challenge created using method '{}'", getLogPrefix(), method);
getAuthenticationRequest().setCodeVerifier(codeVerifier);
getAuthenticationRequest().setCodeChallenge(challenge);
getAuthenticationRequest().setCodeChallengeMethod(method);
diff --git a/oidc-common-profile-impl/src/test/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallengeTest.java b/oidc-common-profile-impl/src/test/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallengeTest.java
new file mode 100644
index 00000000..6d02a651
--- /dev/null
+++ b/oidc-common-profile-impl/src/test/java/net/shibboleth/oidc/profile/messaging/handler/impl/AddPKCECodeVerifierAndChallengeTest.java
@@ -0,0 +1,148 @@
+
+package net.shibboleth.oidc.profile.messaging.handler.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+import java.net.URI;
+import java.util.List;
+
+import org.opensaml.messaging.context.MessageContext;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.nimbusds.oauth2.sdk.id.ClientID;
+import com.nimbusds.oauth2.sdk.id.Issuer;
+import com.nimbusds.oauth2.sdk.pkce.CodeChallengeMethod;
+import com.nimbusds.openid.connect.sdk.SubjectType;
+import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata;
+
+import net.shibboleth.oidc.metadata.context.OIDCProviderMetadataContext;
+import net.shibboleth.oidc.profile.core.OIDCAuthenticationRequest;
+import net.shibboleth.oidc.profile.messaging.PKCEOptions;
+import net.shibboleth.oidc.profile.messaging.context.OIDCPeerEntityContext;
+import net.shibboleth.shared.logic.PredicateSupport;
+
+/**
+ * Tests for {@link AddPKCECodeVerifierAndChallenge}.
+ */
+public class AddPKCECodeVerifierAndChallengeTest {
+
+ private AddPKCECodeVerifierAndChallenge handler;
+
+ private MessageContext msgCtx;
+
+ private OIDCProviderMetadata metadata;
+
+ private OIDCAuthenticationRequest request;
+
+ @BeforeMethod
+ public void setup() throws Exception {
+ msgCtx = new MessageContext();
+ request = new OIDCAuthenticationRequest(new ClientID("test-client"));
+ metadata = new OIDCProviderMetadata(new Issuer("https://op.example.com"),
+ List.of(SubjectType.PUBLIC), new URI("https://op.example.com/jwks"));
+ final OIDCProviderMetadataContext mdCtx = new OIDCProviderMetadataContext();
+ mdCtx.setProviderInformation(metadata);
+ final OIDCPeerEntityContext peerCtx = new OIDCPeerEntityContext();
+ peerCtx.addSubcontext(mdCtx);
+ msgCtx.addSubcontext(peerCtx);
+ msgCtx.setMessage(request);
+
+ handler = new AddPKCECodeVerifierAndChallenge();
+ }
+
+ @Test
+ public void testNullProviderSupportMethods() throws Exception {
+ metadata.setCodeChallengeMethods(null);
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, false));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+
+ @Test
+ public void testS256Supported_S256Used() throws Exception {
+ metadata.setCodeChallengeMethods(List.of(CodeChallengeMethod.S256));
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, false));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+ @Test
+ public void test256NotSupported_S256Used() throws Exception {
+ metadata.setCodeChallengeMethods(List.of(CodeChallengeMethod.PLAIN));
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, false));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+ /* The ONLY branch that supports PLAIN.*/
+ @Test
+ public void testS256NotSupported_PlainAllowed_PlainUsed() throws Exception {
+ metadata.setCodeChallengeMethods(List.of(CodeChallengeMethod.PLAIN));
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, true));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.PLAIN.getValue());
+ }
+
+ @Test
+ public void testS256Supported_PlainAllowed_S256Used() throws Exception {
+ metadata.setCodeChallengeMethods(List.of(CodeChallengeMethod.S256));
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, true));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+ @Test
+ public void testS256AndPlainSupported_PlainAllowed_S256Used() throws Exception {
+ metadata.setCodeChallengeMethods(List.of(CodeChallengeMethod.S256, CodeChallengeMethod.PLAIN));
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, true));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+ @Test
+ public void testNullSupported_PlainAllowed_S256Used() throws Exception {
+ metadata.setCodeChallengeMethods(null);
+ handler.setActivationCondition(PredicateSupport.alwaysTrue());
+ handler.setParameterValueLookupStrategy(msg -> new PKCEOptions(true, true));
+ handler.initialize();
+
+ handler.invoke(msgCtx);
+
+ assertNotNull(request.getCodeChallenge());
+ assertEquals(request.getCodeChallengeMethod().getValue(), CodeChallengeMethod.S256.getValue());
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list