Skip to content

Fix OIDC ID-token nonce against session - #4012

Open
joemahady-comm wants to merge 1 commit into
cloudfoundry:developfrom
joemahady-comm:TNZGOV-16057
Open

Fix OIDC ID-token nonce against session#4012
joemahady-comm wants to merge 1 commit into
cloudfoundry:developfrom
joemahady-comm:TNZGOV-16057

Conversation

@joemahady-comm

Copy link
Copy Markdown
Contributor

Fix: Implemented generation and validation of OIDC nonce parameters to prevent replay attacks during authentication.

Details: Modified ExternalOAuthProviderConfigurator to generate a random 22-character nonce and store it in the user's HTTP session before initiating an OIDC authorization request. ExternalOAuthAuthenticationManager now extracts the nonce claim from the incoming ID token, compares it against the session-stored value, and throws an InvalidTokenException if they mismatch. It properly clears the session value upon successful validation.

…ession ai-assisted=yes

Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens the external OIDC login flow by binding the ID token’s nonce claim to a per-session value, reducing replay risk during authentication against OIDC identity providers.

Changes:

  • Added a session attribute key helper for per-IdP nonce storage.
  • Generated a per-request OIDC nonce, stored it in the HTTP session, and added it to the authorization request.
  • Validated the incoming ID token nonce against the session value and cleared it on successful validation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
server/src/main/java/org/cloudfoundry/identity/uaa/util/SessionUtils.java Adds a dedicated session attribute key prefix/helper for external OAuth/OIDC nonce storage.
server/src/main/java/org/cloudfoundry/identity/uaa/provider/oauth/ExternalOAuthProviderConfigurator.java Generates an OIDC nonce, stores it in the user session, and includes it in the authz redirect URL.
server/src/main/java/org/cloudfoundry/identity/uaa/provider/oauth/ExternalOAuthAuthenticationManager.java Extracts and compares the ID token’s nonce claim against the session-stored nonce; clears the session value on success.

Comment on lines +719 to +728
if (config instanceof OIDCIdentityProviderDefinition) {
String expectedNonce = getSessionValue(SessionUtils.nonceParameterAttributeKeyForIdp(identityProvider.getOriginKey()));
if (StringUtils.hasText(expectedNonce)) {
String tokenNonce = (String) claims.get("nonce");
if (!expectedNonce.equals(tokenNonce)) {
throw new InvalidTokenException("ID token nonce does not match session nonce");
}
clearSessionValue(SessionUtils.nonceParameterAttributeKeyForIdp(identityProvider.getOriginKey()));
}
}
Comment on lines 121 to +125
if (OIDCIdentityProviderDefinition.class.equals(definition.getParameterizedClass())) {
var nonceGenerator = new RandomValueStringGenerator(22);
uriBuilder.queryParam("nonce", nonceGenerator.generate());
String nonce = nonceGenerator.generate();
SessionUtils.setStateParam(request.getSession(), SessionUtils.nonceParameterAttributeKeyForIdp(idpOriginKey), nonce);
uriBuilder.queryParam("nonce", nonce);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants