Skip to content

Server authentication methods: PostgreSQL 18 OAuth and Entra On-Behalf-Of #2

Open

Description

@draoncc

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

  1. 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.
  2. 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.
  3. As a pgAdmin user, I want password to stay the default for new and existing servers, so that nothing changes for me unless I opt in.
  4. As a pgAdmin user, I want oauth_direct to send my pgAdmin login token to the server, so that servers trusting the same issuer accept me.
  5. As a security-conscious pgAdmin user, I want oauth_exchange to swap my login token for one scoped to the database, so that the server never receives a token it could reuse elsewhere.
  6. 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.
  7. 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.
  8. As a pgAdmin user, I want pgAdmin to refuse to send my token when the server's configured oauth_issuer doesn't match my login provider's issuer, so that my token never goes to a server configured for a different identity provider.
  9. 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.
  10. 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.
  11. 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.
  12. As a pgAdmin user, I want the SSH tunnel stopped when a token error aborts a connection, so that no tunnels are left running.
  13. 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.
  14. 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.
  15. As an administrator, I want a clear error when oauth_direct or oauth_exchange is used without libpq 18 or with an unsupported psycopg implementation, so that I know what to upgrade.
  16. As an administrator, I want oauth_client_id, oauth_issuer and oauth_scope to stay ordinary libpq connection parameters, so that they pass the connection parameter validator.
  17. 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.
  18. 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.
  19. 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.
  20. As an administrator, I want Kerberos combined with any server authentication method other than password rejected when a server is saved or imported, so that a server never has two conflicting ways to authenticate.
  21. 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.
  22. As an administrator, I want servers.json export to include AuthMethod, so that a server authentication method survives export and import.
  23. As an administrator, I want a servers.json file without AuthMethod to import as password, so that old exports keep working.
  24. As an administrator, I want an unknown AuthMethod rejected on import with a clear message, so that typos don't silently import as password servers.
  25. 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.
  26. 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.
  27. 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.
  28. As an administrator, I want to enable Entra per OAuth2 provider with a single OAUTH2_CONFIG key, so that only the provider registered for On-Behalf-Of can offer it.
  29. As an administrator, I want entra offered 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.
  30. 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.
  31. As a pgAdmin user, I want entra servers to require sslmode=verify-full on save, on import and at connect, so that my Entra token is only sent to a server whose identity is verified.
  32. As a pgAdmin user on a private endpoint with custom DNS, I want entra to work with any host name, so that private networking is not blocked by a host-name check.
  33. 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.
  34. 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.
  35. 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.
  36. As a pgAdmin user, I want token-delivered-as-password connections to work with any libpq and psycopg implementation, so that entra isn't held to the libpq 18 requirement.
  37. 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.
  38. As a pgAdmin user, I want no database password saved for token-authenticated servers, so that stale tokens aren't stored as passwords.
  39. 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.
  40. As a maintainer, I want all the new tests to run under runtests.py, so that CI actually runs them.
  41. 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. Server and SharedServer each get an auth_method column: a short string, not null, default password. 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, since oauth_pgadmin_token_mode was never on master.
  • Values. password, oauth_direct and oauth_exchange are introduced with the feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 work, and entra is 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_issuer and oauth_scope remain 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 OAuthTokenError subtype. 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, and c on 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_direct and oauth_exchange refuse to supply a token unless oauth_issuer equals the issuer in 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 audience taken from oauth_client_id, sends the configured oauth_scope, requires verified HTTPS, and never falls back to direct.
  • Entra On-Behalf-Of. It is a single jwt-bearer grant 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 no azure-identity dependency, no device-code or browser sign-in, and no token cache.
  • Entra configuration. Each OAUTH2_CONFIG entry gets an optional OAUTH2_ENTRA_OBO_ENABLED key, default false. entra is only available in server mode, and only when the current user's login provider has the key set. The server API refuses to save entra otherwise. Feat/entra postgres auth pgadmin-org/pgadmin4#10641's top-level ENTRA_DB_* settings are not added.
  • Sign in again. An interaction_required, invalid_grant or 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. entra requires sslmode=verify-full. This is checked when a server is saved through the API, on servers.json import and at connect time. There is no Azure host-name check.
  • Kerberos exclusivity. The servers API (create and update) and the servers.json import reject kerberos_conn true together with any auth_method other than password.
  • servers.json. Exports and imports carry an AuthMethod key. A missing key means password, and an unknown value fails the import.
  • Background utilities. sslrootcert and the token for token-authenticated servers are passed through to the background process environment.
  • Shared servers. A non-owner's auth_method lives on their SharedServer row, defaults to password, 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_direct and oauth_exchange. entra is 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.
    1. A squashed commit of feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585 authored by simonj1337, rebased onto master, keeping both upstream PermissionError handling and OAuthTokenError handling, and both sslkeylogfile and the OAuth key in SENSITIVE_CONN_KEYS.
    2. One commit per dpage request on feat: Support pgAdmin OAuth tokens for PostgreSQL authentication pgadmin-org/pgadmin4#10585: the auth_method column and migration, the provider/delivery seam, servers.json and Kerberos validation, and a test that saves a server with a token method.
    3. 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".

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 _SkipServerSetUpMixin where no database server is needed) so that runtests.py collects it.
  • Seam 1: the servers REST API and servers.json import, 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 to password, Kerberos exclusivity, verify-full for entra, refusing entra without On-Behalf-Of, and AuthMethod import/export including missing and unknown values. Prior art: the existing servers tests and Simon's test_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, and psycopg.connect / the libpq hook. It checks the token in OAUTHBEARER context versus the token as password, 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_required turning into the sign-in-again response, and fresh tokens on reset and cancel. Prior art: Simon's test_connection_oauth, test_reset_error and test_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 and entra availability, 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.txt installed and yarn install run in web/.

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

Activity

  1. added
    ready-for-agent
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions