fix(sso): SSO callback redirect for non-admin users with team memberships#4777
Open
bogdanmariusc10 wants to merge 2 commits into
Conversation
Resolves #4770 The SSO callback handler was unconditionally redirecting all authenticated users to /admin without a team_id parameter. This prevented non-admin users from accessing the platform via SSO, as the AdminAuthMiddleware requires either platform admin status or team-scoped admin permissions (which need a team_id query parameter). Changes: - Modified SSO callback to decode JWT and check user admin status - Platform admins: redirect to /admin (unchanged behavior) - Non-admin users with teams: redirect to /admin?team_id=<first_team_id> - Non-admin users without teams: redirect to / (root) - Added error handling with fallback to /admin This enables team-scoped admin access for users with admin.* permissions and provides appropriate landing pages for regular users. Tests: - Updated existing tests to use valid JWT tokens with admin status - Added test for non-admin user with team (team-scoped redirect) - Added test for non-admin user without teams (root redirect) - Added test for team service error (fallback to /admin) - All 49 SSO router tests passing Signed-off-by: Bogdan-Marius-Catanus <[email protected]>
Adds test case for invalid JWT token to cover exception handler (lines 412-415) that falls back to user_info when token decode fails. This brings diff coverage to 96% (24/25 lines covered). Signed-off-by: Bogdan-Marius-Catanus <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Related Issue
Closes #4770
📝 Summary
Fixes SSO callback redirect that was blocking non-admin users from accessing the platform. The SSO callback handler was unconditionally redirecting all authenticated users to
/adminwithout ateam_idparameter, which prevented theAdminAuthMiddlewarefrom resolving team-scoped admin permissions.The fix implements intelligent redirect logic:
is_admin=true): Redirect to/admin(unchanged behavior)/admin?team_id=<first_team_id>(enables team-scoped admin access)/(root page)/adminif team retrieval failsThis enables team members with
admin.*permissions to access the admin UI in their team context, and provides appropriate landing pages for regular users (platform_viewer,developer, etc.).🏷️ Type of Change
🧪 Verification
make lintmake testmake coverage✅ Checklist
make black isort pre-commit)📓 Notes
Implementation Details
Modified:
mcpgateway/routers/sso.py(lines 402-440)TeamManagementServiceintegration to retrieve user teamsTest Coverage:
tests/unit/mcpgateway/routers/test_sso_router.pytest_handle_sso_callback_non_admin_with_team_redirects_to_team- Non-admin user with team membership redirects to team-scoped admintest_handle_sso_callback_non_admin_no_teams_redirects_to_root- Non-admin user without teams redirects to root pagetest_handle_sso_callback_team_service_error_falls_back_to_admin- Team service error falls back to /admin redirecttest_handle_sso_callback_invalid_jwt_falls_back_to_user_info- Invalid JWT token triggers exception handler and falls back to user_infoTest Results: All 50 SSO router tests passing ✅
Impact
Before: Only platform admins could successfully use SSO login. Non-admin users were redirected to
/adminwithoutteam_id, causing a 403 error.After:
admin.*permissions can now access via SSOBackwards Compatibility
The fix is fully backwards compatible: