-
Notifications
You must be signed in to change notification settings - Fork 2.6k
OAuth TokenHandler Enhancement: Authorization Header Fallback Support #1316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88f6ccb
feat(auth): add Authorization header fallback for OAuth client creden…
ChenyangLi4288 6956c44
Fix markdownlint issues
ChenyangLi4288 73b9b62
fix(tests): resolve type checking and linting issues in TokenHandler …
ChenyangLi4288 55d8b42
feat: Add Authorization header fallback for OAuth TokenHandler
ChenyangLi4288 592ceaa
Delete OAUTH_ENHANCEMENT_SUMMARY.md
ChenyangLi4288 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# OAuth TokenHandler Enhancement - Issue #1315 | ||
|
||
## Overview | ||
|
||
This enhancement addresses GitHub issue #1315, which requested that the `TokenHandler` should check the `Authorization` header for client credentials when they are missing from the request body. | ||
|
||
## Problem | ||
|
||
Previously, the `TokenHandler` only looked for client credentials (`client_id` and `client_secret`) in the request form data. However, according to OAuth 2.0 specifications, client credentials can also be provided in the `Authorization` header using Basic authentication. When credentials were only provided in the header, the handler would throw a `ValidationError` even though valid credentials were present. | ||
|
||
## Solution | ||
|
||
The `TokenHandler.handle()` method has been enhanced to: | ||
|
||
1. **Primary**: Continue using client credentials from form data when available | ||
2. **Fallback**: Check the `Authorization` header for Basic authentication when `client_id` is missing from form data | ||
3. **Graceful degradation**: Handle malformed or invalid Authorization headers without breaking the existing flow | ||
|
||
## Implementation Details | ||
|
||
### Code Changes | ||
|
||
The enhancement was implemented in `src/mcp/server/auth/handlers/token.py`: | ||
|
||
```python | ||
async def handle(self, request: Request): | ||
try: | ||
form_data = dict(await request.form()) | ||
|
||
# Try to get client credentials from header if missing in body | ||
if "client_id" not in form_data: | ||
auth_header = request.headers.get("Authorization") | ||
if auth_header and auth_header.startswith("Basic "): | ||
encoded = auth_header.split(" ")[1] | ||
decoded = base64.b64decode(encoded).decode("utf-8") | ||
client_id, _, client_secret = decoded.partition(":") | ||
client_secret = urllib.parse.unquote(client_secret) | ||
form_data.setdefault("client_id", client_id) | ||
form_data.setdefault("client_secret", client_secret) | ||
|
||
token_request = TokenRequest.model_validate(form_data).root | ||
# ... rest of the method | ||
``` | ||
|
||
### Key Features | ||
|
||
- **Base64 Decoding**: Properly decodes Basic authentication credentials | ||
- **URL Decoding**: Handles URL-encoded client secrets (e.g., `test%2Bsecret` → `test+secret`) | ||
- **Non-intrusive**: Only activates when credentials are missing from form data | ||
- **Backward Compatible**: Existing functionality remains unchanged | ||
|
||
## Testing | ||
|
||
Comprehensive tests have been added in `tests/server/auth/test_token_handler.py` covering: | ||
|
||
1. **Form Data Credentials**: Existing functionality continues to work | ||
2. **Authorization Header Fallback**: New functionality works correctly | ||
3. **URL-encoded Secrets**: Handles special characters in client secrets | ||
4. **Invalid Headers**: Gracefully handles malformed Authorization headers | ||
5. **Refresh Token Grants**: Works with both grant types | ||
6. **Error Cases**: Proper validation when no credentials are provided | ||
|
||
### Test Coverage | ||
|
||
- ✅ `test_handle_with_form_data_credentials` | ||
- ✅ `test_handle_with_authorization_header_credentials` | ||
- ✅ `test_handle_with_authorization_header_url_encoded_secret` | ||
- ✅ `test_handle_with_invalid_authorization_header` | ||
- ✅ `test_handle_with_malformed_basic_auth` | ||
- ✅ `test_handle_with_refresh_token_grant` | ||
- ✅ `test_handle_without_credentials_fails` | ||
|
||
## OAuth 2.0 Compliance | ||
|
||
This enhancement improves compliance with OAuth 2.0 specifications by supporting both authentication methods: | ||
|
||
- **client_secret_post** (form data) - RFC 6749 Section 2.3.1 | ||
- **client_secret_basic** (Authorization header) - RFC 6749 Section 2.3.1 | ||
|
||
## Impact | ||
|
||
- **Positive**: Improves OAuth 2.0 compliance and client compatibility | ||
- **Neutral**: No breaking changes to existing functionality | ||
- **Performance**: Minimal overhead (only processes header when needed) | ||
|
||
## Files Modified | ||
|
||
1. **`src/mcp/server/auth/handlers/token.py`** - Main implementation | ||
2. **`tests/server/auth/test_token_handler.py`** - New test suite | ||
|
||
## Verification | ||
|
||
- ✅ All new tests pass | ||
- ✅ All existing tests continue to pass | ||
- ✅ Code passes linting (ruff) | ||
- ✅ Code passes type checking (pyright) | ||
- ✅ No breaking changes to existing functionality | ||
|
||
## Usage Example | ||
|
||
Clients can now use either method: | ||
|
||
## Method 1: Form Data (existing) | ||
|
||
```http | ||
POST /token | ||
Content-Type: application/x-www-form-urlencoded | ||
|
||
grant_type=authorization_code&code=abc123&client_id=myapp&client_secret=secret | ||
``` | ||
|
||
## Method 2: Authorization Header (new) | ||
|
||
```http | ||
POST /token | ||
Authorization: Basic bXlhcHA6c2VjcmV0 | ||
Content-Type: application/x-www-form-urlencoded | ||
|
||
grant_type=authorization_code&code=abc123 | ||
``` | ||
|
||
Both methods will work seamlessly with the enhanced `TokenHandler`. |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this intentionally included? We shouldn't be adding large
.md
files describing a specific implementation fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted! That makes sense. It's my first time contributing to this project didn't realize it's not necessary.