8000 Support OAuth 2.0 SAML 2.0 Bearer Assertion grant (RFC 7522) by devin-ai-integration[bot] · Pull Request #1 · RaghavGK/keycloakgk · GitHub
[go: up one dir, main page]

Skip to content

Support OAuth 2.0 SAML 2.0 Bearer Assertion grant (RFC 7522) - #1

Open
devin-ai-integration[bot] wants to merge 5 commits into
mainfrom
devin/1795881977-saml2-bearer-grant
Open

devin-ai-integration[bot] wants to merge 5 commits into
mainfrom
devin/1795881977-saml2-bearer-grant

Conversation

@devin-ai-integration
@devin-ai-integration devin-ai-integration Bot commented Nov 28, 2026 •
Copy link
Copy Markdown

Adds grant_type=urn:ietf:params:oauth:grant-type:saml2-bearer to the OIDC token endpoint.

Request: grant_type=urn:ietf:params:oauth:grant-type:saml2-bearer&assertion=<base64url SAML Assertion>, plus normal client authentication.

Processing (Saml2BearerGrantType / Saml2BearerAssertionValidator):

  • The client must have oauth2.saml2.bearer.grant.enabled=true. A new Admin Console checkbox under Capability config sets it. Otherwise the request fails with unauthorized_client.
  • The assertion must be a single <saml:Assertion>. Its Issuer must match the idpEntityId of exactly one enabled, non-link-only SAML identity provider in the realm.
  • The enveloped signature on the assertion must cover the assertion and verify against that IdP's keys (configured certificates, or keys from its metadata URL).
  • RFC 7522 §3 checks:
    • AudienceRestriction must contain the token endpoint (frontend or backend URL) or realm issuer URL.
    • There must be a bearer SubjectConfirmation whose Recipient is the token endpoint and whose NotOnOrAfter has not passed.
    • Conditions are enforced with the IdP's allowed clock skew.
  • Replay protection: the issuer:ID pair is stored in SingleUseObjectProvider until the assertion's latest valid expiry.
  • The subject is resolved using the IdP's principal type (NameID or attribute) to the user federated-linked to that IdP. No JIT provisioning.
  • The user must exist, be enabled, not be locked out by brute force, and have no required actions (required-action triggers such as email verification are evaluated). A transient user session is created and tokens are issued through OAuth2GrantTypeBase.createTokenResponse.
  • Failures return invalid_grant (or invalid_request when the parameter is missing) and fire an event.

Also:

  • The grant is added to grant_types_supported in discovery, and the expected counts in AbstractWellKnownProviderTest are updated.
  • New constants in OAuth2Constants, plus the attribute in OIDCConfigAttributes / OIDCAdvancedConfigWrapper.

Fixes for pre-existing breakage on main (requested to be included here):

  • services didn't compile: UsernamePasswordForm, ConditionalOtpFormAuthenticator and CookieAuthenticator used org.apache.commons.lang3.StringUtils, which services doesn't depend on. Replaced with Keycloak's org.keycloak.utils.StringUtil and a plain indexOf for substringBefore; no new dependency.
  • Login NPE with the user cache enabled: UsernamePasswordForm and CookieAuthenticator called getConsentByClient(null, ...). They now pass the current realm.
  • model/jpa didn't compile because of an incomplete client-secret encryption customization in JpaRealmProvider (commit 919fbd3798). Encryption isn't required, so JpaRealmProvider is restored to its state before that commit. The rest of that commit is unchanged.

Testing: Saml2BearerAssertionValidatorTest has 13 unit tests (valid assertion, realm-issuer audience, standard base64, backend token endpoint, unsigned, wrong key, tampered, expired, wrong or missing audience, wrong recipient, non-bearer confirmation, non-assertion document). All pass. End-to-end runs against a locally built server passed. The SAML grant was tested with the Admin Console toggle and real signed assertions (success, replay, tampered, untrusted, disabled client). A later clean build without workarounds also passed: login with the user cache enabled, the grant plus replay, and a new client's client_credentials. See the PR comments.

Link to Devin session: https://app.devin.ai/sessions/6329efa8c1364cf5abf704442c263bc1
Open in Devin Desktop: https://app.devin.ai/desktop/session/6329efa8c1364cf5abf704442c263bc1?variant=devin
Requested by: @RaghavGK


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

Adds the urn:ietf:params:oauth:grant-type:saml2-bearer grant type to the
token endpoint. Assertions must be signed by an enabled SAML identity
provider of the realm, addressed to the token endpoint, carry a bearer
subject confirmation, and are single-use. The subject is mapped to the
user linked to that identity provider. Clients opt in via the
oauth2.saml2.bearer.grant.enabled attribute.

Signed-off-by: gk.raghavendra.gk <gk.raghavendra.gk@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
Copy link
Copy Markdown
Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

RaghavGK and others added 2 commits November 28, 2026 16:10
- Ignore link-only SAML identity providers
- Evaluate required action triggers (e.g. verify email) before issuing tokens
- Keep replay entries until the latest valid bearer confirmation expires, rounding up
- Accept assertions addressed to the backend token endpoint URL

Signed-off-by: gk.raghavendra.gk <gk.raghavendra.gk@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ntext

Signed-off-by: gk.raghavendra.gk <gk.raghavendra.gk@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration devin-ai-integration Bot left a comment •
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 5 findings. Devin automatically fixed all 5 findings.

Fixed by Devin (5)
  • Backchannel SAML grants reject valid assertions — Recipient and audience are validated against both frontend and backend token endpoint URLs; unit test added.
  • Link-only providers can issue login tokens — Link-only SAML IdPs are now excluded from issuer lookup.
  • Unevaluated email verification allows token issuance — Required action triggers are evaluated (as in the ROPC grant) and both user and auth-session required actions block issuance.
  • Replay protection expires before assertion validity — Replay entry lifespan now uses the latest valid bearer confirmation expiry (capped by Conditions) and rounds up to whole seconds.
  • Scope-based profile requirements bypassed — Scope client note and context auth session are now set before evaluating required action triggers, so scope-bound profile attributes are enforced.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration
Copy link
Copy Markdown
Author
End-to-end SAML bearer grant verification at ca48e26923
  • Admin Console: The Capability config checkbox saved and was still checked after a reload.
  • Opt-in on: A real RSA-SHA256 assertion returned HTTP 200 with access and refresh tokens for the linked user. Keycloak recorded a matching LOGIN event.
  • Opt-in off: Saving the checkbox off returned HTTP 400 unauthorized_client.

Saved SAML bearer grant checkbox after reload

Endpoint coverage and setup caveats
  • Rejections: Each case returned the expected OAuth error: replay, tampering, wrong key, unknown/link-only/disabled IdP, wrong audience or recipient, expired, unlinked/disabled user, required action, unverified email, missing assertion.
  • Discovery: It lists the grant.
  • Signature checks: Both configured-certificate and metadata-based checks worked.
  • Setup: The local build needed temporary fixes for two build blockers already on main (services missing commons-lang3; JpaRealmProvider compile errors). The fixes were removed afterwards.
  • Login workaround: The browser run disabled the user cache to get past a login null-pointer error that also exists on main. The full endpoint suite ran with the cache back on.

Written by Devin

- Replace commons-lang3 StringUtils (not a services dependency) with Keycloak StringUtil
- Pass the realm to getConsentByClient in UsernamePasswordForm and CookieAuthenticator (NPE with user cache)
- JpaRealmProvider: drop duplicate session field, define TRANSFORMATION, import Base64

Signed-off-by: gk.raghavendra.gk <gk.raghavendra.gk@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
devin-ai-integration[bot]

This comment was marked as resolved.

@devin-ai-integration
Copy link
Copy Markdown
Author
Clean regression run at e9f10737

The unmodified distribution built, and Admin Console login worked with the default user cache enabled. A fresh signed SAML assertion issued tokens for the linked user, and replaying the same assertion returned HTTP 400 invalid_grant. A new confidential client's generated secret issued a service-account token with CLIENT_ENCRYPTION_KEY unset.

Admin Console confirms creation of the confidential client

Additional negative control

A wrong client secret was rejected with HTTP 401 unauthorized_client. That error code comes from the existing ClientIdAndSecretAuthenticator, which this PR doesn't change, so this is existing Keycloak behavior and not a regression.

Written by Devin

Restores JpaRealmProvider to its state before 919fbd3; encryption is not required.

Signed-off-by: gk.raghavendra.gk <gk.raghavendra.gk@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration devin-ai-integration Bot left a comment
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration
Copy link
Copy Markdown
Author
Client secret rotation verified at 368b588 (encryption code removed)

I created a confidential client with a service account and regenerated its secret in the Admin Console. The original secret worked before regeneration and the new secret worked after it. Both a wrong secret and the old secret returned HTTP 401 with no token.

Admin Console confirms secret regeneration

Build, login, and SAML regression

The clean distribution built without workarounds. Admin Console login worked with the default user cache. A fresh signed SAML assertion issued tokens for the linked user, and replaying the same assertion returned HTTP 400 invalid_grant.

Written by Devin

7E47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0