feat(oauth2): Map OAuth2 claims to Visible Server Groups - #10234
feat(oauth2): Map OAuth2 claims to Visible Server Groups#10234beasteers wants to merge 2 commits into
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughOAuth2 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. ChangesOAuth2 Server-Group Visibility
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
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (4)
web/config.pyweb/pgadmin/authenticate/oauth2.pyweb/pgadmin/browser/tests/test_oauth2_with_mocking.pyweb/pgadmin/utils/server_access.py
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_CONFIGinweb/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 insession['oauth2_server_group_claims']:In
web/pgadmin/utils/server_access.py,get_server_groups_for_user_query()now appends an additionalORcondition matchingServerGroup.nameagainst 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:oidc-server-groups-direct(no mapping) andoidc-server-groups-mapped(with mapping).session['oauth2_server_group_claims']is populated correctly.Backwards compatibility
Fully opt-in. When
OAUTH2_SERVER_GROUP_CLAIMis not set, behavior is unchanged.Summary by CodeRabbit
New Features
Documentation
Tests