Repository navigation
Description
Part of pgadmin-org#9261. Combines and reworks pgadmin-org#10585 (simonj1337, PostgreSQL 18 OAuth using the pgAdmin login token) and pgadmin-org#10641 (AshiqAbdulkhader, Microsoft Entra ID for Azure Database for PostgreSQL), following dpage's review comments of 2026-11-04 and 2026-11-05 on both PRs.
Work lands on the local branch bak/entra-postgres-auth, cut from master, with linear history (no merge commits). It is not pushed and no PR is opened until reviewed.
Problem Statement
In server mode, pgAdmin users who log in through an OAuth2 identity provider still have to obtain a separate database credential to connect to servers that accept the same identity. For PostgreSQL 18 servers that validate OAuth bearer tokens, they have to fetch a token by hand. For Azure Database for PostgreSQL with Microsoft Entra ID, they have to run the Azure CLI to get a token and paste it in as a password, and then do it again when the token expires.
Two open PRs each solve half of this. They change the same connect, cancel and password-prompt paths in different ways. pgadmin-org#10585 stores its token mode in connection_params, which master now refuses because only exact libpq keywords are allowed there (pgadmin-org#10650). pgadmin-org#10641 adds its own authentication column, a second Microsoft sign-in (device code or browser) using the Azure CLI's client ID, and an encrypted token cache that needs a desktop keyring, so it fails in Docker and on headless servers.
Solution
Each server gets a server authentication method: password, oauth_direct, oauth_exchange or entra. Every method other than password pairs a token provider (where the access token comes from) with a token delivery (how it reaches the server):
| Server authentication method | Token provider | Token delivery |
|---|---|---|
password |
none | none |
oauth_direct |
pgAdmin login token | OAUTHBEARER |
oauth_exchange |
Token exchange (RFC 8693) | OAUTHBEARER |
entra |
Entra On-Behalf-Of | In place of a password |
When a pgAdmin user connects to a server that uses a token method, pgAdmin gets a fresh access token from their current OAuth2 login for each new physical connection, with no second sign-in. When the identity provider requires the user to sign in again (MFA, Conditional Access, consent, or an expired login), the connect dialog offers "Sign in again", which renews the pgAdmin login through the same OAuth2 provider.
User Stories
- As a pgAdmin user logged in through OAuth2, I want to connect to a PostgreSQL 18 server with my pgAdmin login token, so that I don't need a separate database password.
- As a pgAdmin user, I want to pick the server authentication method from one list in the server dialog, so that I don't have to know which hidden connection parameter controls it.
- As a pgAdmin user, I want
passwordto stay the default for new and existing servers, so that nothing changes for me unless I opt in. - As a pgAdmin user, I want
oauth_directto send my pgAdmin login token to the server, so that servers trusting the same issuer accept me. - As a security-conscious pgAdmin user, I want
oauth_exchangeto swap my login token for one scoped to the database, so that the server never receives a token it could reuse elsewhere. - As a pgAdmin user, I want token exchange to name the database as the token's audience, so that the token it returns is limited to the database.
- As an administrator, I want token exchange to authenticate with pgAdmin's own client credentials, so that someone holding only a user's access token cannot repeat the exchange.
- As a pgAdmin user, I want pgAdmin to refuse to send my token when the server's configured
oauth_issuerdoesn't match my login provider's issuer, so that my token never goes to a server configured for a different identity provider. - As a pgAdmin user, I want tokens that are about to expire to be refreshed before connecting, so that my connections don't fail on expiry.
- As a pgAdmin user, I want a clear error, not a database password prompt, when an OAuth connection fails for a token reason, so that I'm not asked for a password I don't have.
- As a pgAdmin user, I want the SSH password prompt to keep working on token-authenticated servers reached through a tunnel, so that tunnelled servers still connect.
- As a pgAdmin user, I want the SSH tunnel stopped when a token error aborts a connection, so that no tunnels are left running.
- As a pgAdmin user, I want query cancellation and connection resets on token-authenticated servers to use a fresh token, so that Cancel and Reset work after the original token has expired.
- As a pgAdmin user, I want the Query Tool and other asynchronous connections to authenticate the same way as the Object Explorer, so that every tool works with the server authentication method.
- As an administrator, I want a clear error when
oauth_directoroauth_exchangeis used without libpq 18 or with an unsupported psycopg implementation, so that I know what to upgrade. - As an administrator, I want
oauth_client_id,oauth_issuerandoauth_scopeto stay ordinary libpq connection parameters, so that they pass the connection parameter validator. - As an administrator sharing a server, I want each non-owner's server authentication method to default to
password, so that no non-owner's login token goes to a host they didn't choose. - As a non-owner of a shared server, I want to opt in to a token method myself, so that I decide whether my own login token is used.
- As an owner of a shared server, I want my own server authentication method to keep applying to me, so that sharing doesn't change how I connect.
- As an administrator, I want Kerberos combined with any server authentication method other than
passwordrejected when a server is saved or imported, so that a server never has two conflicting ways to authenticate. - As a pgAdmin user, I want the Kerberos option disabled in the dialog while a token method is selected, so that I can't build a configuration that will be refused.
- As an administrator, I want
servers.jsonexport to includeAuthMethod, so that a server authentication method survives export and import. - As an administrator, I want a
servers.jsonfile withoutAuthMethodto import aspassword, so that old exports keep working. - As an administrator, I want an unknown
AuthMethodrejected on import with a clear message, so that typos don't silently import as password servers. - As a pgAdmin user logged in through Microsoft Entra ID, I want to connect to Azure Database for PostgreSQL with method
entra, so that I don't have to run the Azure CLI and paste a token. - As a pgAdmin user, I want the Entra token obtained On-Behalf-Of my pgAdmin login, so that I sign in to Microsoft only once.
- As an administrator, I want Entra to work in Docker and on headless servers, so that I don't need a desktop keyring or token cache.
- As an administrator, I want to enable Entra per OAuth2 provider with a single
OAUTH2_CONFIGkey, so that only the provider registered for On-Behalf-Of can offer it. - As an administrator, I want
entraoffered only in server mode, when the user's current login provider has On-Behalf-Of enabled, so that users never pick a method that can't work for them. - As an administrator, I want the docs to explain that the pgAdmin app registration must expose its own API scope and that login must request it, so that On-Behalf-Of gets a token with the right audience.
- As a pgAdmin user, I want
entraservers to requiresslmode=verify-fullon save, on import and at connect, so that my Entra token is only sent to a server whose identity is verified. - As a pgAdmin user on a private endpoint with custom DNS, I want
entrato work with any host name, so that private networking is not blocked by a host-name check. - As a pgAdmin user, I want backup, restore, maintenance and other background utilities to receive
sslrootcert, so that they verify TLS the same way the main connection does. - As a pgAdmin user, I want background utilities on token-authenticated servers to receive a fresh token, so that they authenticate like the main connection.
- As a pgAdmin user whose organization requires me to sign in again, I want the server and Query Tool connect dialogs to offer "Sign in again", so that I can renew my login and retry without hunting for the logout button.
- As a pgAdmin user, I want token-delivered-as-password connections to work with any libpq and psycopg implementation, so that
entraisn't held to the libpq 18 requirement. - As a pgAdmin user, I want tokens never written to logs or error messages, so that anyone who can read the logs can't reuse them.
- As a pgAdmin user, I want no database password saved for token-authenticated servers, so that stale tokens aren't stored as passwords.
- As a maintainer, I want a later server authentication method to plug in as a new token provider plus an existing token delivery, so that each new identity provider doesn't need its own connection path.
- As a maintainer, I want all the new tests to run under
runtests.py, so that CI actually runs them. - As a pgAdmin user, I want the docs to describe each server authentication method, its requirements and its security trade-offs, so that I can choose the right one.
Implementation Decisions
- Schema.
ServerandSharedServereach get anauth_methodcolumn: a short string, not null, defaultpassword. Allowed values are checked in the application, not by a SQL enum. One Alembic migration adds both columns, with a schema version bump. It doesn't convert any old key, sinceoauth_pgadmin_token_modewas never onmaster. - Values.
password,oauth_directandoauth_exchangeare introduced with the feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 work, andentrais added with the Entra work. Kerberos stays the existing boolean and is not a server authentication method. - Removing the old key. The token mode no longer lives in
connection_params.oauth_client_id,oauth_issuerandoauth_scoperemain there as real libpq 18 keywords. There is no allowlist of pgAdmin-only connection parameter keys. - Token provider seam. A small registry maps each server authentication method to a token provider and a token delivery. A token provider is a callable that, given the server's connection parameters and the current request's login, returns an access token or raises an
OAuthTokenErrorsubtype. The providers are pgAdmin login token (direct), token exchange and Entra On-Behalf-Of. - Token delivery seam. OAUTHBEARER installs the libpq 18 auth-data hook and scopes the token around the connect, async connect, reset and cancel calls. Password delivery passes the token as the connection's password for that physical connection only and never stores it. Only OAUTHBEARER enforces the libpq 18 version check and the psycopg implementation checks (
python, andcon Linux). - Driver integration. The connection class asks the registry for the server's provider and delivery at every point that opens a physical connection: initial connect, async connect, reset and cancel. Token errors stop the SSH tunnel and return a token-specific response, not a database password prompt. SSH password prompts are kept.
- Issuer check.
oauth_directandoauth_exchangerefuse to supply a token unlessoauth_issuerequals theissuerin the login provider's metadata. - Token exchange. It is an RFC 8693 request authenticated with the login client's own client credentials. It names the target with
audiencetaken fromoauth_client_id, sends the configuredoauth_scope, requires verified HTTPS, and never falls back to direct. - Entra On-Behalf-Of. It is a single
jwt-bearergrant to the tenant's token endpoint, which comes from the login provider's issuer metadata. It uses the login client's credentials, with the user's login access token as the assertion and the Azure PostgreSQL resource scope. It uses the same HTTP client path as token exchange, with noazure-identitydependency, no device-code or browser sign-in, and no token cache. - Entra configuration. Each
OAUTH2_CONFIGentry gets an optionalOAUTH2_ENTRA_OBO_ENABLEDkey, default false.entrais only available in server mode, and only when the current user's login provider has the key set. The server API refuses to saveentraotherwise. Feat/entra postgres auth pgadmin-org/pgadmin4#10641's top-levelENTRA_DB_*settings are not added. - Sign in again. An
interaction_required,invalid_grantor expired-login response from the identity provider becomes a distinct connect response that the server connect dialog and Query Tool connect dialog show as "Sign in again". Clicking it renews the pgAdmin login through the same OAuth2 provider. This reuses Feat/entra postgres auth pgadmin-org/pgadmin4#10641's prompt UI. - TLS.
entrarequiressslmode=verify-full. This is checked when a server is saved through the API, onservers.jsonimport and at connect time. There is no Azure host-name check. - Kerberos exclusivity. The servers API (create and update) and the
servers.jsonimport rejectkerberos_conntrue together with anyauth_methodother thanpassword. servers.json. Exports and imports carry anAuthMethodkey. A missing key meanspassword, and an unknown value fails the import.- Background utilities.
sslrootcertand the token for token-authenticated servers are passed through to the background process environment. - Shared servers. A non-owner's
auth_methodlives on theirSharedServerrow, defaults topassword, and is never copied from the owner's server. - Server dialog. It has a single server authentication method selector. The OAuth parameter fields show only for
oauth_directandoauth_exchange.entrais listed only when the backend reports it as available. Kerberos is disabled while a token method is selected. - Commit structure on
bak/entra-postgres-auth.- A squashed commit of feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 authored by simonj1337, rebased onto
master, keeping both upstreamPermissionErrorhandling andOAuthTokenErrorhandling, and bothsslkeylogfileand the OAuth key inSENSITIVE_CONN_KEYS. - One commit per dpage request on feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585: the
auth_methodcolumn and migration, the provider/delivery seam,servers.jsonand Kerberos validation, and a test that saves a server with a token method. - Entra rework commits that reuse the surviving parts of Feat/entra postgres auth pgadmin-org/pgadmin4#10641 with
Co-Authored-By: AshiqAbdulkhader, referencing "Part of Enable Entra ID authentication for PostgreSQL servers in pgAdmin pgadmin-org/pgadmin4#9261".
- A squashed commit of feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 authored by simonj1337, rebased onto
Testing Decisions
- Tests check externally visible behaviour at the highest available seam: what the HTTP API accepts or returns, what reaches libpq, and what the dialog offers. They don't test private helpers. Simon's helper-level tests move to these seams where possible.
- Every new test subclasses
BaseTestGenerator(using_SkipServerSetUpMixinwhere no database server is needed) so thatruntests.pycollects it. - Seam 1: the servers REST API and
servers.jsonimport, through pgAdmin's test client and the SQLite config database. Covers saving and reading back each server authentication method (including the test dpage asked for, which saves a server with a token method enabled), non-owners defaulting topassword, Kerberos exclusivity,verify-fullforentra, refusingentrawithout On-Behalf-Of, andAuthMethodimport/export including missing and unknown values. Prior art: the existing servers tests and Simon'stest_shared_server_oauth. - Seam 2: the psycopg3 driver's
Connection.connect()(plus reset, cancel and async connect). Only the outer boundaries are faked: the identity provider token endpoint, the login token in the session, andpsycopg.connect/ the libpq hook. It checks the token in OAUTHBEARER context versus the token aspassword, the exchange and On-Behalf-Of request bodies (grant type, client authentication, audience or scope), the issuer check, password delivery not depending on libpq 18 or the psycopg implementation, the SSH tunnel stopping on token errors,interaction_requiredturning into the sign-in-again response, and fresh tokens on reset and cancel. Prior art: Simon'stest_connection_oauth,test_reset_errorandtest_check_ssh_mock_connect, and the RDS tests. - Seam 3: the server dialog schema, through the existing
server.ui.spec.js. It checks method options andentraavailability, OAuth field visibility, Kerberos disabling, and the sign-in-again prompt in the connect dialog. Prior art: the existing schema specs and Feat/entra postgres auth pgadmin-org/pgadmin4#10641's JS tests. - Verification runs the Python regression tests for the affected packages and the JS tests locally, in a virtualenv with
web/requirements.txtinstalled andyarn installrun inweb/.
Out of Scope
- Entra or other token methods in desktop mode, or for pgAdmin users not logged in through OAuth2.
- Device-code or browser Microsoft sign-in, the Azure CLI client ID, and any token cache or keyring.
- Making Kerberos a server authentication method.
- Background reconnects without an authenticated pgAdmin request.
- Replacing the ctypes libpq adapter with native psycopg OAuth hooks (pending Add PostgreSQL 18 OAuth support psycopg/psycopg#1190).
- Pushing the branch or opening or updating upstream PRs.
Further Notes
- Glossary terms used here (server authentication method, token provider, token delivery, auth source, shared server, owner, non-owner) are defined in
GLOSSARY.md. - The On-Behalf-Of access token must have pgAdmin's own app registration as its audience, so the Entra
OAUTH2_SCOPEmust include pgAdmin's exposed API scope. Check this against Microsoft's On-Behalf-Of documentation when writing the docs. - Upstream reviews: dpage on feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 (2026-10-15, 2026-10-26, 2026-11-04, 2026-11-05) and on Feat/entra postgres auth pgadmin-org/pgadmin4#10641 (2026-11-05).
Activity