feat(postgres): support SASL OAUTHBEARER authentication - #4400
Open
geoHeil wants to merge 1 commit into
Open
Conversation
PostgreSQL 18 added the `oauth` HBA method, which authenticates over SASL
OAUTHBEARER (RFC 7628) instead of a password. `sasl::authenticate` accepted
only SCRAM-SHA-256 and SCRAM-SHA-256-PLUS, so such a server was unreachable.
This is the token-first flow only: the caller supplies a bearer token and the
driver performs the SASL exchange. SQLx does not contact an identity provider,
so the discovery and device-authorization flows of RFC 7628 are not
implemented; this matches the scope other drivers limit themselves to.
let options = PgConnectOptions::new().oauth_token_provider(|| async {
Ok(refresh_my_token().await?)
});
The provider is called once per connection attempt rather than stored, so a
pool that reconnects after the token expired presents a fresh one.
`oauth_token` sets a fixed token where that is enough.
Notes:
- OAUTHBEARER defines no channel binding and the server rejects the `p`
specifier, so the gs2 header is always `n,,` and this does not go through
the `-PLUS` machinery.
- The token is checked against the `b64token` grammar of RFC 6750 before it
reaches the wire, which is what the server enforces too. The grammar
excludes the kvsep byte, whitespace and NUL, so a token that passes cannot
forge additional key/value pairs or truncate the message.
- `PgOAuthToken` has a hand-written `Debug` that prints nothing, because
`PgConnectOptions` derives `Debug` and a bearer token is a credential. The
token also never appears in an error.
- No connection-string option is added: a URL ends up in shell history, in
`ps` output and in logs.
- `SaslInitialResponse` now carries the mechanism name instead of a `plus`
flag that was always `false`.
- No new dependencies.
geoHeil
marked this pull request as draft
August 29, 2026 09:30
geoHeil
marked this pull request as ready for review
August 29, 2026 11:24
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #4399.
PG18 docker verified
audmatchesselect current_user, version()returnedtestuser | PostgreSQL 18.6audnames another serviceError::Database, code 28000audclaim absentaudis an array containing the expected value