Skip to content

feat(postgres): support SASL OAUTHBEARER authentication - #4400

Open
geoHeil wants to merge 1 commit into
transact-rs:mainfrom
geoHeil:feat/pg-oauthbearer
Open

feat(postgres): support SASL OAUTHBEARER authentication#4400
geoHeil wants to merge 1 commit into
transact-rs:mainfrom
geoHeil:feat/pg-oauthbearer

Conversation

@geoHeil

@geoHeil geoHeil commented Aug 29, 2026

Copy link
Copy Markdown

Closes #4399.

PG18 docker verified

Case Result
aud matches ACCEPTEDselect current_user, version() returned testuser | PostgreSQL 18.6
aud names another service REFUSEDError::Database, code 28000
aud claim absent REFUSED — same
aud is an array containing the expected value ACCEPTED

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
geoHeil marked this pull request as draft August 29, 2026 09:30
@geoHeil
geoHeil marked this pull request as ready for review August 29, 2026 11:24
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.

Postgres: support SASL OAUTHBEARER (PostgreSQL 18 oauth authentication method)

1 participant