OAuth Password Grant Flow Implementation - closes #1109 #1168
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.
OAuth Password Grant Flow Implementation - closes #1109
Overview
Implemented complete support for OAuth 2.0 Resource Owner Password Credentials Grant (RFC 6749 Section 4.3) to enable Keycloak-style authentication integration.
Fixes: #1109
Problem Statement
Users could not integrate MCP Gateway with Keycloak OAuth2 endpoints because:
client_credentials
andauthorization_code
grant types were supportedapplication/x-www-form-urlencoded
request format required by KeycloakThis caused errors like:
Solution Implemented
1. Backend Implementation
OAuth Manager (
mcpgateway/services/oauth_manager.py
)Lines 185-186: Added password grant type support
Lines 287-384: Implemented
_password_flow()
methodapplication/x-www-form-urlencoded
request via aiohttpdata
parameterKey Features:
username
,password
,token_url
client_id
,client_secret
,scopes
2. Unit Tests
Test File (
tests/unit/mcpgateway/test_oauth_manager.py
)Lines 83-184: Added 3 comprehensive test cases
test_get_access_token_password_flow_success
(lines 84-135)test_get_access_token_password_flow_missing_credentials
(lines 137-149)test_get_access_token_password_flow_form_urlencoded_response
(lines 151-184)Test Results:
3. UI Components
HTML Template (
mcpgateway/templates/admin.html
)Create Gateway Form:
Lines 4567-4569: Added password grant option to dropdown
Lines 4624-4640: Username input field
oauth-username-field-gw
oauth_username
display: none
)Lines 4642-4658: Password input field
oauth-password-field-gw
oauth_password
display: none
)Edit Gateway Form:
oauth-username-field-edit
oauth-password-field-edit
4. JavaScript Logic
Admin Script (
mcpgateway/static/admin.js
)Create Form Handler (
handleOAuthGrantTypeChange
):Edit Form Handler (
handleEditOAuthGrantTypeChange
):5. Backend Form Processing
Admin Routes (
mcpgateway/admin.py
)Create Gateway Handler:
oauth_username
from formoauth_password
from formEdit Gateway Handler:
oauth_username
from form (edit)oauth_password
from form (edit)Files Modified
mcpgateway/services/oauth_manager.py
tests/unit/mcpgateway/test_oauth_manager.py
mcpgateway/templates/admin.html
mcpgateway/static/admin.js
mcpgateway/admin.py
Total Lines Changed: ~250
Usage Example
Configuration via UI
https://keycloak.example.com/auth/realms/myrealm/protocol/openid-connect/token
systemadmin@system.com
********
mcp-gateway-client
openid profile email
Configuration via API
Token Request Format
The backend automatically sends:
Testing
Unit Tests
Code Quality
Results
Security Considerations
Backwards Compatibility
client_credentials
andauthorization_code
flows unchangedKnown Limitations
Password Grant Security: The password grant type is considered legacy and less secure than authorization_code flow. It should only be used for trusted first-party applications or when required by legacy systems like Keycloak.
Password Storage: Passwords are stored in the gateway's oauth_config. For production use, consider:
No Refresh Token UI: While the backend supports refresh tokens, there's no UI to manually trigger token refresh (it happens automatically when tokens expire)
Future Enhancements
Verification Checklist
References