Add Bearer token authentication support for all API endpoints #4
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.
Summary
This PR implements Bearer token authentication via
Authorizationheader for all master token endpoints while maintaining full backward compatibility with query parameter authentication.Fixes link-assistant/master-plan#14
Changes
Service Layer
getMasterTokenFromRequest(req)method toTokensServicethat:Authorization: Bearer <token>header (recommended)?masterToken=<token>query parameter (deprecated)Controllers Updated
Updated 10 endpoints across 5 controllers to use new authentication method:
tokensController.js (4 endpoints)
GET /token- Get user tokenGET /token/has- Check if user has tokenPUT /token- Update token balancePOST /token- Regenerate tokencompletionsController.js (1 endpoint)
POST /completions- Create completion with dialog historydialogsController.js (1 endpoint)
GET /dialog-history- Get dialog historysystemMessagesController.js (2 endpoints)
POST /system-message- Create system messageGET /system-message- Get system messagereferralController.js (2 endpoints)
POST /referral- Create referralGET /referral- Get referralSecurity Improvements
Why Bearer Token in Headers is More Secure
Based on OAuth 2.0 RFC 6750 and 2025 security best practices:
Query Parameter Risks:
Header Benefits:
OAuth 2.0 RFC 6750 Quote:
Backward Compatibility
✅ No breaking changes - all existing code continues to work
Query Parameter Support (Deprecated)
New Header Support (Recommended)
Testing
Manual Testing Checklist
Example Test Commands
Test Bearer header (recommended):
Test query parameter (deprecated, should log warning):
curl -X GET "http://localhost:3000/token?userId=123&masterToken=${MASTER_TOKEN}"Test missing authentication:
Migration Guide for API Consumers
For Telegram Bot
Before:
After:
Timeline
Benefits
Related Issues & PRs
Additional Documentation
See detailed documentation in master-plan repository:
🤖 Generated with Claude Code