Skip to content

feat(oauth2): Map OAuth2 claims to Visible Server Groups - #10234

Open
beasteers wants to merge 2 commits into
pgadmin-org:masterfrom
beasteers:oauth-group-claims
Open

feat(oauth2): Map OAuth2 claims to Visible Server Groups#10234
beasteers wants to merge 2 commits into
pgadmin-org:masterfrom
beasteers:oauth-group-claims

Conversation

@beasteers

@beasteers beasteers commented Aug 5, 2026

Copy link
Copy Markdown

Summary

Control access to server groups using OAuth2 token claims, letting you create different levels of access tied to centralized SSO roles.

Fixes #9704

What changed

Two new per-provider config keys were added to OAUTH2_CONFIG in web/config.py:

  • OAUTH2_SERVER_GROUP_CLAIM — the claim (from the ID token or userinfo profile) whose values are treated as server group names.
  • OAUTH2_SERVER_GROUP_CLAIM_MAPPING — optional dict mapping a claim value → one server group name or a list of server group names, useful when the claim carries opaque codes (e.g. readonly) that don't match group names directly.

How it works

In web/pgadmin/authenticate/oauth2.py, the new _extract_server_group_claims() resolves the allowed groups during login and stores them in session['oauth2_server_group_claims']:

  • If the claim is not configured, the session key is cleared.
  • Claim values may be a single value or a list.
  • Mapping values may be a string or a list.

In web/pgadmin/utils/server_access.py, get_server_groups_for_user_query() now appends an additional OR condition matching ServerGroup.name against the resolved claim-based groups — for OAuth2 users only. This applies alongside the existing ownership and shared-server (Server.shared) conditions.

Tests

web/pgadmin/browser/tests/test_oauth2_with_mocking.py:

  • Two new providers: oidc-server-groups-direct (no mapping) and oidc-server-groups-mapped (with mapping).
  • Two new login-success cases asserting session['oauth2_server_group_claims'] is populated correctly.
  • The existing default assertions confirm the key is absent when the claim is not configured.

Backwards compatibility

Fully opt-in. When OAUTH2_SERVER_GROUP_CLAIM is not set, behavior is unchanged.

Summary by CodeRabbit

  • New Features

    • Added OAuth2/OIDC server-group visibility controls based on identity-token or profile claims.
    • Added optional mappings from claim values to server-group names.
    • OAuth2 users now see permitted owned, shared, and claim-based server groups.
    • Outdated group visibility data is cleared when filtering is disabled.
  • Documentation

    • Documented session storage, visibility behavior, and handling of unmapped claim values.
  • Tests

    • Added coverage for direct and mapped server-group claims during successful login.

Add OAUTH2_SERVER_GROUP_CLAIM and OAUTH2_SERVER_GROUP_CLAIM_MAPPING
provider config keys. When configured, claim values determine which
server groups a user can see, applied as an additional OR condition
alongside ownership and shared-server visibility.
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 78644436-df66-41e9-818e-afd94187f3ac

📥 Commits

Reviewing files that changed from the base of the PR and between c6088c5 and 19c0599.

📒 Files selected for processing (2)
  • web/pgadmin/authenticate/oauth2.py
  • web/pgadmin/browser/tests/test_oauth2_with_mocking.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • web/pgadmin/browser/tests/test_oauth2_with_mocking.py
  • web/pgadmin/authenticate/oauth2.py

Walkthrough

OAuth2 provider settings now support server-group claims and optional mappings. Login resolves groups from ID-token or userinfo claims and stores them in the session. Server-group queries include matching OAuth2 groups. Mocked OIDC tests cover direct and mapped claims.

Changes

OAuth2 Server-Group Visibility

Layer / File(s) Summary
Claim configuration and session resolution
web/config.py, web/pgadmin/authenticate/oauth2.py
Provider configuration defines the claim name and optional mapping. Login resolves unique groups from ID-token or userinfo claims and stores or clears the session value.
Server-group access filtering
web/pgadmin/utils/server_access.py
Server-group queries include owned groups, eligible shared-server groups, and groups listed in OAuth2 session claims.
OAuth2 server-group validation
web/pgadmin/browser/tests/test_oauth2_with_mocking.py
Mocked OIDC scenarios validate direct claims, mapped claims, provider configuration, and session state.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OAuth2Provider
  participant OAuth2Login
  participant FlaskSession
  participant ServerGroupQuery
  OAuth2Provider->>OAuth2Login: Provide ID-token or userinfo claim
  OAuth2Login->>OAuth2Login: Resolve and map server groups
  OAuth2Login->>FlaskSession: Store oauth2_server_group_claims
  ServerGroupQuery->>FlaskSession: Read OAuth2 server groups
  ServerGroupQuery-->>OAuth2Login: Return accessible server groups
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the primary change: mapping OAuth2 claims to visible server groups.
Linked Issues check ✅ Passed The changes satisfy #9704 by mapping OAuth2/OIDC claims to server-group access with configuration, session handling, filtering, and tests.
Out of Scope Changes check ✅ Passed All changes support the linked issue by implementing configuration, claim resolution, access filtering, documentation, and focused tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@web/pgadmin/authenticate/oauth2.py`:
- Around line 559-563: Update the needs_userinfo decision in get_user_profile()
to also fetch userinfo when OAUTH2_SERVER_GROUP_CLAIM is configured but absent
from the ID-token claims, even when the username claim is present. Ensure the
existing server-group resolution can use the fetched profile, and add a
regression test covering a server-group claim available only in userinfo.

In `@web/pgadmin/browser/tests/test_oauth2_with_mocking.py`:
- Around line 471-478: Update the session assertion in the OAuth2 test around
expected_server_groups to compare server-group contents without requiring
ordering, using assertCountEqual for the non-None case while preserving the
existing absence assertion for None.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: fc568543-fd85-472e-b433-0aa726934cd3

📥 Commits

Reviewing files that changed from the base of the PR and between 067f7af and c6088c5.

📒 Files selected for processing (4)
  • web/config.py
  • web/pgadmin/authenticate/oauth2.py
  • web/pgadmin/browser/tests/test_oauth2_with_mocking.py
  • web/pgadmin/utils/server_access.py

Comment thread web/pgadmin/authenticate/oauth2.py
Comment thread web/pgadmin/browser/tests/test_oauth2_with_mocking.py
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.

Map OAuth claims to Server Groups

1 participant