Skip to content

Commit 3ca618d

Browse files
author
Paul Bourhis
committed
Enhance OAUTH2 and OIDC authentication support with improved claims handling and configuration options
1 parent 74c8b2d commit 3ca618d

5 files changed

Lines changed: 654 additions & 184 deletions

File tree

docs/en_US/oauth2.rst

Lines changed: 142 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
.. _oauth2:
22

3-
*****************************************
4-
`Enabling OAUTH2 Authentication`:index:
5-
*****************************************
3+
*******************************************************
4+
`Enabling OAUTH2 and OIDC Authentication`:index:
5+
*******************************************************
66

77

8-
To enable OAUTH2 authentication for pgAdmin, you must configure the OAUTH2
9-
settings in the *config_local.py* or *config_system.py* file (see the
10-
:ref:`config.py <config_py>` documentation) on the system where pgAdmin is
11-
installed in Server mode. You can copy these settings from *config.py* file
12-
and modify the values for the following parameters:
8+
To enable OAUTH2 or OpenID Connect (OIDC) authentication for pgAdmin, you must
9+
configure the OAUTH2 settings in the *config_local.py* or *config_system.py*
10+
file (see the :ref:`config.py <config_py>` documentation) on the system where
11+
pgAdmin is installed in Server mode. You can copy these settings from *config.py*
12+
file and modify the values for the following parameters.
13+
14+
OAuth2 vs OpenID Connect (OIDC)
15+
================================
16+
17+
pgAdmin supports both OAuth2 and OIDC authentication protocols:
18+
19+
**OAuth2** is an authorization framework that allows third-party applications to
20+
obtain limited access to user accounts. When using OAuth2, pgAdmin must explicitly
21+
call the provider's userinfo endpoint to retrieve user profile information.
22+
23+
**OpenID Connect (OIDC)** is an identity layer built on top of OAuth2 that provides
24+
standardized user authentication and profile information. When using OIDC, user
25+
identity information is included directly in the ID token, which is more efficient
26+
and secure.
27+
28+
.. note::
29+
When **OAUTH2_SERVER_METADATA_URL** is configured, pgAdmin treats the provider
30+
as an OIDC provider and will:
31+
32+
- Use ID token claims for user identity (sub, email, preferred_username)
33+
- Skip the userinfo endpoint call when ID token contains sufficient information
34+
- Validate the ID token automatically using the provider's public keys
35+
36+
This is the **recommended approach** for modern identity providers like
37+
Microsoft Entra ID (Azure AD), Google, Keycloak, Auth0, and Okta.
1338

1439

1540
.. _AzureAD: https://learn.microsoft.com/en-us/security/zero-trust/develop/configure-tokens-group-claims-app-roles
@@ -32,20 +57,17 @@ and modify the values for the following parameters:
3257
"OAUTH2_CLIENT_SECRET", "Oauth2 Client Secret"
3358
"OAUTH2_TOKEN_URL", "Oauth2 Access Token endpoint"
3459
"OAUTH2_AUTHORIZATION_URL", "Endpoint for user authorization"
35-
"OAUTH2_SERVER_METADATA_URL", "Server metadata url for your OAuth2 provider"
60+
"OAUTH2_SERVER_METADATA_URL", "**OIDC Discovery URL** (recommended for OIDC providers). When set, pgAdmin will use OIDC flow with automatic ID token validation and user claims from the ID token. Example: *https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration*. When using this parameter, OAUTH2_TOKEN_URL and OAUTH2_AUTHORIZATION_URL are optional as they will be discovered automatically."
3661
"OAUTH2_API_BASE_URL", "Oauth2 base URL endpoint to make requests simple, ex: *https://api.github.com/*"
37-
"OAUTH2_USERINFO_ENDPOINT", "User Endpoint, ex: *user* (for github, or *user/emails* if the user's email address is private) and *userinfo* (for google),"
38-
"OAUTH2_SCOPE", "Oauth scope, ex: 'openid email profile'. Note that an 'email' claim is required in the resulting profile."
62+
"OAUTH2_USERINFO_ENDPOINT", "User Endpoint, ex: *user* (for github, or *user/emails* if the user's email address is private) and *userinfo* (for google). **For OIDC providers**, this is optional if the ID token contains sufficient claims (email, preferred_username, or sub)."
63+
"OAUTH2_SCOPE", "Oauth scope, ex: 'openid email profile'. **For OIDC providers**, include 'openid' scope to receive an ID token."
3964
"OAUTH2_ICON", "The Font-awesome icon to be placed on the oauth2 button, ex: fa-github"
4065
"OAUTH2_BUTTON_COLOR", "Oauth2 button color"
41-
"OAUTH2_USERNAME_CLAIM", "The claim which is used for the username. If the value is empty
42-
the email is used as username, but if a value is provided, the claim has to exist. Ex: *oid* (for AzureAD), *email* (for Github)"
66+
"OAUTH2_USERNAME_CLAIM", "The claim which is used for the username. If the value is empty, **for OIDC providers** pgAdmin will use: 1) email, 2) preferred_username, or 3) sub (in that order). **For OAuth2 providers** without OIDC, email is required. Ex: *oid* (for AzureAD), *email* (for Github), *preferred_username* (for Keycloak)"
4367
"OAUTH2_AUTO_CREATE_USER", "Set the value to *True* if you want to automatically
4468
create a pgAdmin user corresponding to a successfully authenticated Oauth2 user.
4569
Please note that password is not stored in the pgAdmin database."
46-
"OAUTH2_ADDITIONAL_CLAIMS", "If a dictionary is provided, pgAdmin will check for a matching key and value on the userinfo endpoint
47-
and in the Id Token. In case there is no match with the provided config, the user will receive an authorization error.
48-
Useful for checking AzureAD_ *wids* or *groups*, GitLab_ *owner*, *maintainer* and *reporter* claims."
70+
"OAUTH2_ADDITIONAL_CLAIMS", "If a dictionary is provided, pgAdmin will check for a matching key and value on the **ID token first** (for OIDC providers), then fall back to the userinfo endpoint response. In case there is no match with the provided config, the user will receive an authorization error. Useful for checking AzureAD_ *wids* or *groups*, GitLab_ *owner*, *maintainer* and *reporter* claims."
4971
"OAUTH2_SSL_CERT_VERIFICATION", "Set this variable to False to disable SSL certificate verification for OAuth2 provider.
5072
This may need to set False, in case of self-signed certificates."
5173
"OAUTH2_CHALLENGE_METHOD", "Enable PKCE workflow. PKCE method name, only *S256* is supported"
@@ -83,3 +105,107 @@ Ref: https://oauth.net/2/pkce
83105

84106
To enable PKCE workflow, set the configuration parameters OAUTH2_CHALLENGE_METHOD to *S256* and OAUTH2_RESPONSE_TYPE to *code*.
85107
Both parameters are mandatory to enable PKCE workflow.
108+
109+
OIDC Configuration Examples
110+
============================
111+
112+
Using OIDC with Discovery Metadata (Recommended)
113+
-------------------------------------------------
114+
115+
When using OIDC providers, configure the **OAUTH2_SERVER_METADATA_URL** parameter
116+
to enable automatic discovery and ID token validation:
117+
118+
.. code-block:: python
119+
120+
OAUTH2_CONFIG = [{
121+
'OAUTH2_NAME': 'my-oidc-provider',
122+
'OAUTH2_DISPLAY_NAME': 'My OIDC Provider',
123+
'OAUTH2_CLIENT_ID': 'your-client-id',
124+
'OAUTH2_CLIENT_SECRET': 'your-client-secret',
125+
'OAUTH2_SERVER_METADATA_URL': 'https://provider.example.com/.well-known/openid-configuration',
126+
'OAUTH2_SCOPE': 'openid email profile',
127+
# OAUTH2_USERINFO_ENDPOINT is optional when using OIDC
128+
# Token and authorization URLs are discovered automatically
129+
}]
130+
131+
With this configuration:
132+
133+
- pgAdmin will use the OIDC discovery endpoint to automatically find token and authorization URLs
134+
- User identity will be extracted from ID token claims (sub, email, preferred_username)
135+
- The userinfo endpoint will only be called as a fallback if ID token lacks required claims
136+
- ID token will be automatically validated using the provider's public keys
137+
138+
Username Resolution for OIDC
139+
-----------------------------
140+
141+
When **OAUTH2_SERVER_METADATA_URL** is configured (OIDC mode), pgAdmin will
142+
resolve the username in the following order:
143+
144+
1. **OAUTH2_USERNAME_CLAIM** (if configured) - checks ID token first, then userinfo
145+
2. **email** claim from ID token or userinfo endpoint
146+
3. **preferred_username** claim from ID token (standard OIDC claim)
147+
4. **sub** claim from ID token (always present in OIDC, used as last resort)
148+
149+
Example with custom username claim:
150+
151+
.. code-block:: python
152+
153+
OAUTH2_CONFIG = [{
154+
# ... other config ...
155+
'OAUTH2_USERNAME_CLAIM': 'preferred_username',
156+
# pgAdmin will use 'preferred_username' from ID token for the username
157+
}]
158+
159+
Example without custom claim (uses automatic fallback):
160+
161+
.. code-block:: python
162+
163+
OAUTH2_CONFIG = [{
164+
# ... other config ...
165+
# No OAUTH2_USERNAME_CLAIM specified
166+
# pgAdmin will try: email -> preferred_username -> sub
167+
}]
168+
169+
Additional Claims Authorization with OIDC
170+
------------------------------------------
171+
172+
When using **OAUTH2_ADDITIONAL_CLAIMS** with OIDC providers, pgAdmin will:
173+
174+
1. Check the ID token claims first (more secure, no additional network call)
175+
2. Fall back to userinfo endpoint response if needed
176+
177+
Example:
178+
179+
.. code-block:: python
180+
181+
OAUTH2_CONFIG = [{
182+
# ... other config ...
183+
'OAUTH2_ADDITIONAL_CLAIMS': {
184+
'groups': ['admin-group', 'pgadmin-users'],
185+
'roles': ['database-admin']
186+
},
187+
# pgAdmin will check these claims in ID token first,
188+
# then userinfo endpoint if not found
189+
}]
190+
191+
Legacy OAuth2 Configuration (Without OIDC)
192+
-------------------------------------------
193+
194+
For providers that don't support OIDC discovery, configure all endpoints manually:
195+
196+
.. code-block:: python
197+
198+
OAUTH2_CONFIG = [{
199+
'OAUTH2_NAME': 'github',
200+
'OAUTH2_DISPLAY_NAME': 'GitHub',
201+
'OAUTH2_CLIENT_ID': 'your-client-id',
202+
'OAUTH2_CLIENT_SECRET': 'your-client-secret',
203+
'OAUTH2_TOKEN_URL': 'https://github.com/login/oauth/access_token',
204+
'OAUTH2_AUTHORIZATION_URL': 'https://github.com/login/oauth/authorize',
205+
'OAUTH2_API_BASE_URL': 'https://api.github.com/',
206+
'OAUTH2_USERINFO_ENDPOINT': 'user',
207+
'OAUTH2_SCOPE': 'user:email',
208+
# No OAUTH2_SERVER_METADATA_URL - pure OAuth2 mode
209+
}]
210+
211+
In this mode, user identity is retrieved only from the userinfo endpoint.

web/pgadmin/authenticate/__init__.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,27 @@ def as_dict(self):
227227
return res
228228

229229
def update_auth_sources(self):
230-
for auth_src in [KERBEROS, OAUTH2]:
231-
if auth_src in self.auth_sources:
232-
if 'internal_button' in request.form:
230+
# Only mutate the ordered list of auth sources when a user explicitly
231+
# selected an auth method on the login form.
232+
#
233+
# Without this guard, a plain internal login POST (email/password) can
234+
# incorrectly drop INTERNAL/LDAP and try OAUTH2 first, which then fails
235+
# because no oauth2 provider button was provided.
236+
if request.method != 'POST':
237+
return
238+
239+
if 'internal_button' in request.form:
240+
for auth_src in [KERBEROS, OAUTH2]:
241+
if auth_src in self.auth_sources:
233242
self.auth_sources.remove(auth_src)
234-
else:
235-
if INTERNAL in self.auth_sources:
236-
self.auth_sources.remove(INTERNAL)
237-
if LDAP in self.auth_sources:
238-
self.auth_sources.remove(LDAP)
243+
return
244+
245+
if 'oauth2_button' in request.form:
246+
if INTERNAL in self.auth_sources:
247+
self.auth_sources.remove(INTERNAL)
248+
if LDAP in self.auth_sources:
249+
self.auth_sources.remove(LDAP)
250+
return
239251

240252
def set_current_source(self, source):
241253
self.current_source = source

0 commit comments

Comments
 (0)