-
Notifications
You must be signed in to change notification settings - Fork 8.7k
refactor cnf verification of refresh tokens #50927
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,51 +347,85 @@ public RefreshToken verifyRefreshToken(KeycloakSession session, RealmModel realm | |
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token. Token client and authorized client don't match"); | ||
| } | ||
|
|
||
| // KEYCLOAK-6771 Certificate Bound Token | ||
| if (OIDCAdvancedConfigWrapper.fromClientModel(client).isUseMtlsHokToken()) { | ||
| if (!MtlsHoKTokenUtil.verifyTokenBindingWithClientCertificate(refreshToken, request, session)) { | ||
| throw new OAuthErrorException(OAuthErrorException.UNAUTHORIZED_CLIENT, MtlsHoKTokenUtil.CERT_VERIFY_ERROR_DESC); | ||
| } | ||
| verifyConfirmationBinding(session, client, request, refreshToken); | ||
|
|
||
| return refreshToken; | ||
|
|
||
| } catch (JWSInputException e) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token", e); | ||
| } | ||
| } | ||
|
|
||
| private void verifyConfirmationBinding(KeycloakSession session, ClientModel client, HttpRequest request, RefreshToken refreshToken) throws OAuthErrorException { | ||
| AccessToken.Confirmation cnf = refreshToken.getConfirmation(); | ||
| OIDCAdvancedConfigWrapper clientConfig = OIDCAdvancedConfigWrapper.fromClientModel(client); | ||
| ABCAResult abcaResult = session.getAttribute(ABCAResult.ABCA_RESULT, ABCAResult.class); | ||
|
|
||
| // MTLS required but token has no certificate thumbprint | ||
| if (clientConfig.isUseMtlsHokToken() && (cnf == null || cnf.getCertThumbprint() == null)) { | ||
| throw new OAuthErrorException(OAuthErrorException.UNAUTHORIZED_CLIENT, MtlsHoKTokenUtil.CERT_VERIFY_ERROR_DESC); | ||
| } | ||
|
|
||
| // DPoP required for public client but token has no DPoP binding (RFC 9449 §5) | ||
| if (clientConfig.isUseDPoP() && client.isPublicClient()) { | ||
| boolean dpopBound = cnf != null && cnf.getKeyThumbprint() != null && (cnf.getJktType() == null || DPOP_JKT_TYPE.equals(cnf.getJktType())); | ||
| if (!dpopBound) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "DPoP proof key binding is required"); | ||
| } | ||
| } | ||
|
|
||
| // Verify RefreshToken Confirmation | ||
| // | ||
| AccessToken.Confirmation cnf = refreshToken.getConfirmation(); | ||
| if (cnf != null && cnf.getKeyThumbprint() != null) { | ||
| String jktType = Optional.ofNullable(cnf.getJktType()).orElse(DPOP_JKT_TYPE); | ||
| switch (jktType) { | ||
| case ABCA_JKT_TYPE -> { | ||
| ABCAResult abcaResult = session.getAttribute(ABCAResult.ABCA_RESULT, ABCAResult.class); | ||
| if (abcaResult == null) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token. Client attestation must be used with this refresh token"); | ||
| } | ||
| JWK jwk = abcaResult.getAttestationJwt().getConfirmation().getJwk(); | ||
| String thumbprint = JWKSUtils.computeThumbprint(jwk); | ||
| if (!Objects.equals(thumbprint, cnf.getKeyThumbprint())) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Attestation-Based Key mismatch"); | ||
| } | ||
| // ABCA attestation present but token has no attestation binding | ||
| if (abcaResult != null) { | ||
| boolean abcaBound = cnf != null && cnf.getKeyThumbprint() != null && ABCA_JKT_TYPE.equals(cnf.getJktType()); | ||
| if (!abcaBound) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Attestation-based key binding is required for this refresh token"); | ||
|
Comment on lines
+377
to
+381
Contributor
Author
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. DPoP and ABCA are alternatives to MTLS HoK, not complements.
Comment on lines
+377
to
+381
Contributor
Author
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. DPoP and ABCA are alternatives to MTLS HoK, not complements.
Comment on lines
+377
to
+381
|
||
| } | ||
| } | ||
|
|
||
| // No confirmation claim, nothing to verify | ||
| if (cnf == null) { | ||
| return; | ||
| } | ||
|
|
||
| // cnf present but no known binding type | ||
| if (cnf.getCertThumbprint() == null && cnf.getKeyThumbprint() == null) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token confirmation"); | ||
| } | ||
|
|
||
| // Verify MTLS certificate binding (x5t#S256) | ||
| if (cnf.getCertThumbprint() != null) { | ||
| if (!MtlsHoKTokenUtil.verifyTokenBindingWithClientCertificate(refreshToken, request, session)) { | ||
| throw new OAuthErrorException(OAuthErrorException.UNAUTHORIZED_CLIENT, MtlsHoKTokenUtil.CERT_VERIFY_ERROR_DESC); | ||
| } | ||
| } | ||
|
|
||
| // Verify key binding (DPoP or ABCA) | ||
| if (cnf.getKeyThumbprint() != null) { | ||
| String jktType = Optional.ofNullable(cnf.getJktType()).orElse(DPOP_JKT_TYPE); | ||
| switch (jktType) { | ||
| case ABCA_JKT_TYPE -> { | ||
| if (abcaResult == null) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token. Client attestation must be used with this refresh token"); | ||
| } | ||
| case DPOP_JKT_TYPE -> { | ||
| DPoP dPoP = session.getAttribute(DPoPUtil.DPOP_SESSION_ATTRIBUTE, DPoP.class); | ||
| if (dPoP == null) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "DPoP proof is missing"); | ||
| } | ||
| try { | ||
| DPoPUtil.validateBinding(refreshToken, dPoP); | ||
| } catch (VerificationException ex) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, ex.getMessage()); | ||
| } | ||
| JWK jwk = abcaResult.getAttestationJwt().getConfirmation().getJwk(); | ||
| String thumbprint = JWKSUtils.computeThumbprint(jwk); | ||
| if (!Objects.equals(thumbprint, cnf.getKeyThumbprint())) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Attestation-Based Key mismatch"); | ||
| } | ||
| default -> { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Unknown jkt type: " + jktType); | ||
| } | ||
| case DPOP_JKT_TYPE -> { | ||
| DPoP dPoP = session.getAttribute(DPoPUtil.DPOP_SESSION_ATTRIBUTE, DPoP.class); | ||
| if (dPoP == null) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "DPoP proof is missing"); | ||
| } | ||
| try { | ||
| DPoPUtil.validateBinding(refreshToken, dPoP); | ||
| } catch (VerificationException ex) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, ex.getMessage()); | ||
| } | ||
| } | ||
| default -> throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Unknown jkt type: " + jktType); | ||
| } | ||
|
|
||
| return refreshToken; | ||
|
|
||
| } catch (JWSInputException e) { | ||
| throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Invalid refresh token", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DPoP and ABCA are alternatives to MTLS HoK, not complements.