-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add nonce to OIDC Authentication Request #7337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ca42a4
gh4442 https://github.com/spring-projects/spring-security/issues/4442…
mkheck 68c1cbf
Github issue 4442 (https://github.com/spring-projects/spring-security…
mkheck ab39bd4
Reworked gh-4442 per feedback from Joe Grandja
mkheck 595ab60
Added nonce creation, verification, & handling
mkheck 60cdf3e
Fixed outlier tests in OAuth2AuthorizationRequestRedirectFilterTests
mkheck 3441389
Resolves requested changes to PR for gh-4442
mkheck bf73846
Merge branch 'master' into master
mkheck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; | ||
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames; | ||
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames; | ||
import org.springframework.security.web.util.UrlUtils; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
import org.springframework.util.Assert; | ||
|
@@ -51,6 +52,7 @@ | |
* @author Joe Grandja | ||
* @author Rob Winch | ||
* @author Eddú Meléndez | ||
* @author Mark Heckler | ||
* @since 5.1 | ||
* @see OAuth2AuthorizationRequestResolver | ||
* @see OAuth2AuthorizationRequestRedirectFilter | ||
|
@@ -61,7 +63,7 @@ public final class DefaultOAuth2AuthorizationRequestResolver implements OAuth2Au | |
private final ClientRegistrationRepository clientRegistrationRepository; | ||
private final AntPathRequestMatcher authorizationRequestMatcher; | ||
private final StringKeyGenerator stateGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder()); | ||
private final StringKeyGenerator codeVerifierGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); | ||
private final StringKeyGenerator randomKeyGenerator = new Base64StringKeyGenerator(Base64.getUrlEncoder().withoutPadding(), 96); | ||
mkheck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Constructs a {@code DefaultOAuth2AuthorizationRequestResolver} using the provided parameters. | ||
|
@@ -118,11 +120,15 @@ private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String re | |
OAuth2AuthorizationRequest.Builder builder; | ||
if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(clientRegistration.getAuthorizationGrantType())) { | ||
builder = OAuth2AuthorizationRequest.authorizationCode(); | ||
Map<String, Object> additionalParameters = new HashMap<>(); | ||
|
||
createNonceAndHashForRequest(attributes, additionalParameters); | ||
|
||
if (ClientAuthenticationMethod.NONE.equals(clientRegistration.getClientAuthenticationMethod())) { | ||
Map<String, Object> additionalParameters = new HashMap<>(); | ||
addPkceParameters(attributes, additionalParameters); | ||
builder.additionalParameters(additionalParameters); | ||
} | ||
|
||
builder.additionalParameters(additionalParameters); | ||
} else if (AuthorizationGrantType.IMPLICIT.equals(clientRegistration.getAuthorizationGrantType())) { | ||
builder = OAuth2AuthorizationRequest.implicit(); | ||
} else { | ||
|
@@ -201,6 +207,28 @@ private static String expandRedirectUri(HttpServletRequest request, ClientRegist | |
.toUriString(); | ||
} | ||
|
||
/** | ||
* Creates nonce and its hash for use in OpenID Connect Authentication Requests | ||
* | ||
* @param attributes where {@link IdTokenClaimNames#NONCE} is stored for the token request | ||
* @param additionalParameters where hash of {@link IdTokenClaimNames#NONCE} is added to the authentication request | ||
* | ||
* @since 5.2 | ||
* @see <a target="_blank" href="https://openid.net/specs/openid-connect-core-1_0.html#NonceNotes">15.5.2. Nonce Implementation Notes</a> | ||
* @see <a target="_blank" href="https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation">3.1.3.7. ID Token Validation</a> | ||
*/ | ||
private void createNonceAndHashForRequest(Map<String, Object> attributes, Map<String, Object> additionalParameters) { | ||
mkheck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String nonce = this.randomKeyGenerator.generateKey(); | ||
attributes.put(IdTokenClaimNames.NONCE, nonce); | ||
|
||
try { | ||
String nonceHash = createHash(nonce); | ||
jgrandja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
additionalParameters.put(IdTokenClaimNames.NONCE, nonceHash); | ||
mkheck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (NoSuchAlgorithmException e) { | ||
// MAH: TODO...but what? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||
} | ||
} | ||
|
||
/** | ||
* Creates and adds additional PKCE parameters for use in the OAuth 2.0 Authorization and Access Token Requests | ||
* | ||
|
@@ -214,18 +242,18 @@ private static String expandRedirectUri(HttpServletRequest request, ClientRegist | |
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7636#section-4.2">4.2. Client Creates the Code Challenge</a> | ||
*/ | ||
private void addPkceParameters(Map<String, Object> attributes, Map<String, Object> additionalParameters) { | ||
String codeVerifier = this.codeVerifierGenerator.generateKey(); | ||
String codeVerifier = this.randomKeyGenerator.generateKey(); | ||
attributes.put(PkceParameterNames.CODE_VERIFIER, codeVerifier); | ||
try { | ||
String codeChallenge = createCodeChallenge(codeVerifier); | ||
String codeChallenge = createHash(codeVerifier); | ||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, codeChallenge); | ||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE_METHOD, "S256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
additionalParameters.put(PkceParameterNames.CODE_CHALLENGE, codeVerifier); | ||
} | ||
} | ||
|
||
private String createCodeChallenge(String codeVerifier) throws NoSuchAlgorithmException { | ||
private String createHash(String codeVerifier) throws NoSuchAlgorithmException { | ||
mkheck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
byte[] digest = md.digest(codeVerifier.getBytes(StandardCharsets.US_ASCII)); | ||
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.