feat(tokens): add admin-delegated token creation with user_email parameter#4487
Open
bogdanmariusc10 wants to merge 1 commit intomainfrom
Conversation
Add optional user_email parameter to POST /tokens endpoint to enable platform admins to create tokens on behalf of other users. This resolves the portal architecture blocker where admin portals need to create tokens for logged-in users. Changes: - Add user_email (Optional[EmailStr]) to TokenCreateRequest schema - Implement admin authorization check requiring un-narrowed platform admin - Add audit logging for admin-delegated token creation operations - Maintain all security invariants (Management Plane isolation, interactive session requirement, un-narrowed admin access) Security: - API tokens still blocked from token management (Management Plane isolation) - Requires is_admin=True AND token_teams=None (un-narrowed admin) - Narrowed admin sessions cannot delegate token creation - Email validation via Pydantic EmailStr type - Audit trail via structured logging Tests: - Add 7 comprehensive tests for admin delegation scenarios - Update 2 existing tests for backward compatibility - All 84 tests passing (77 existing + 7 new) Closes #4389 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 #4389
📝 Summary
This PR adds admin-delegated token creation capability to the
POST /tokensendpoint by introducing an optionaluser_emailparameter. This enables platform admins to create API tokens on behalf of other users, resolving a critical portal architecture blocker where admin portals need to create tokens for logged-in users.Key Changes:
user_email(Optional[EmailStr]) parameter toTokenCreateRequestschemaSecurity Model:
is_admin=TrueANDtoken_teams=None(un-narrowed admin)EmailStrtype🏷️ Type of Change
🧪 Verification
make lintmake testmake coverage✅ Checklist
make black isort pre-commit)📓 Notes
Implementation Details
Files Modified:
mcpgateway/schemas.py- Addeduser_emailparametermcpgateway/routers/tokens.py- Admin authorization logictests/unit/mcpgateway/routers/test_tokens.py- Updated 2 tests for compatibilitytests/unit/mcpgateway/routers/test_tokens_admin_delegation.py- New test file with 7 test casesSecurity Invariants Verified
✅ Management Plane Isolation:
_require_authenticated_session()called before any logicauth_method == "api_token"with 403 Forbidden✅ Un-narrowed Admin Requirement: Lines 144-151
token_teams is not Noneand rejects with 403✅ Audit Trail: Lines 153-157
logger.info()for all delegated operations✅ Email Validation: Schema line 6783
EmailStrtype enforces valid email formatUsage Example
Backward Compatibility
✅ Fully backward compatible -
user_emailis optional and defaults toNone✅ Existing API calls work unchanged
✅ No breaking changes to existing functionality