feat(auth): unified auth middleware (closes #12)#13
Merged
Conversation
…port - Add AuthContext dataclass for consistent return type - Add require_auth() - accepts JWT or API key, raises 401 if invalid - Add public_auth() - for demo routes, works with or without auth - Add _validate_jwt() and _validate_api_key() core functions - Keep legacy get_current_user/get_optional_user for backwards compat - Clean, readable code structure with section separators Part of #12
- GET /api/repos now uses require_auth + AuthContext - POST /api/repos now uses require_auth + AuthContext - Supports both JWT and API key authentication Part of #12
- POST /api/repos/{id}/index
- POST /api/search
- POST /api/explain
- GET /api/repos/{id}/dependencies
- POST /api/repos/{id}/impact
- GET /api/repos/{id}/insights
- GET /api/repos/{id}/style-analysis
All now use require_auth + AuthContext
Part of #12
- Migrate remaining routes: /api/metrics, /api/keys/generate, /api/keys/usage - Remove legacy verify_api_key() function - Clean up unused Header import - All routes now use unified auth middleware Part of #12
- Mock auth.get_user to return response.user=None by default - This ensures invalid JWT tokens are rejected (401) - Fixes test_invalid_key_rejected test Part of #12
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.
Unified the two separate auth systems (verify_api_key + get_current_user) into a single AuthContext-based middleware.
What changed:
require_auth()dependency that accepts both JWT tokens and API keysverify_api_key()functionAll 25 tests passing, tested manually in Docker.