Add API key authentication middleware for mobile client access#5
Add API key authentication middleware for mobile client access#5lukesmmr wants to merge 1 commit intofeat/api-expansion-mobile-appfrom
Conversation
Protect /api and /graphql routes with Bearer token auth using timing-safe comparison. Health, metrics, and docs endpoints remain open. Auth is skipped in test environment. API_KEY is required in production via Joi env validation. Co-authored-by: Cursor <cursoragent@cursor.com>
|
@coderabbitai review |
📝 WalkthroughWalkthroughThis change introduces API key-based Bearer token authentication across the application. A new middleware validates incoming requests against an API_KEY environment variable using constant-time comparison, applied conditionally to REST and GraphQL routes when not in test mode. Supporting configuration, comprehensive test coverage, and OpenAPI documentation updates have been added. Changes
Sequence DiagramsequenceDiagram
participant Client
participant Middleware as apiKeyAuth<br/>(Middleware)
participant Config as Environment<br/>(API_KEY)
participant Handler as Route<br/>Handler
Client->>Middleware: Request + Authorization:<br/>Bearer [token]
Middleware->>Config: Read API_KEY
alt API_KEY missing
Middleware->>Client: 500 Server Error
else Authorization header missing
Middleware->>Client: 401 Unauthorized
else Token invalid
Middleware->>Client: 401 Invalid API key
else Token valid (timing-safe compare)
Middleware->>Handler: next()
Handler->>Client: 200 Response
end
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
✅ Actions performedReview triggered.
|
js-0s
left a comment
There was a problem hiding this comment.
Great Work
small suggestion about key-check
| } | ||
|
|
||
| const token = authHeader.slice(7); // Strip 'Bearer ' prefix | ||
| const tokenBuffer = Buffer.from(token); |
There was a problem hiding this comment.
user input should only load tokens cut down to their maximum length.
(authHeader.length!==apiKey.length+7) should already res 401, not allocating a buffer for it
Protect /api and /graphql routes with Bearer token auth using timing-safe comparison. Health, metrics, and docs endpoints remain open. Auth is skipped in test environment. API_KEY is required in production via Joi env validation.
Summary by CodeRabbit
New Features
Tests
Chores