Skip to content

Add API key authentication alongside JWT#5169

Closed
soongyintong wants to merge 18 commits into
ClankerNation:mainfrom
soongyintong:feature/api-key-auth
Closed

Add API key authentication alongside JWT#5169
soongyintong wants to merge 18 commits into
ClankerNation:mainfrom
soongyintong:feature/api-key-auth

Conversation

@soongyintong
Copy link
Copy Markdown

Summary

Adds static API key authentication support alongside the existing JWT-based auth middleware.

Changes

New Features

  • API Key Auth: Support X-API-Key header as alternative auth method alongside JWT
  • SHA-256 Storage: API keys stored as hashes in the database (never plaintext)
  • POST /auth/api-keys: Generate new API keys (returns plaintext key exactly once)
  • DELETE /auth/api-keys/{id}: Revoke API keys
  • GET /auth/api-keys: List all API keys for authenticated user
  • Differential Rate Limiting: API key users get 1000 req/min vs 100 req/min for JWT users

Fixes

  • Fixed JWT algorithm pinning vulnerability (algorithms=["HS256"])
  • Fixed user_id type consistency in auth
  • Added JWT_SECRET dev fallback to prevent startup crashes

Tests

21 comprehensive tests covering:

  • JWT auth: valid, missing, invalid, expired tokens
  • API key auth: valid, invalid, revoked, SHA-256 storage
  • API key CRUD endpoints
  • Rate limit header differences
  • Key generation uniqueness

Closes #177

Copy link
Copy Markdown

@wndzph9jkb-create wndzph9jkb-create left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review:

1. Out of scope changes — PR modifies which is not in scope for issue #177. The acceptance criteria only requires changes to auth middleware.

2. Non-existent model imports — References and from but these models do not exist in the project. This will cause import errors.

3. RateLimitConfig API mismatch — Uses and params that do not exist in the RateLimitConfig constructor. Will cause runtime errors.

4. JWT algorithm vulnerability not fixed — Original code uses allowing token forgery. This PR does not address this documented security bug.

5. Python syntax issues — Missing newline at end of main.py, inconsistent import paths between relative and absolute imports.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

Unfortunately the changes in this PR didn't fully resolve the issue. Please rework your solution and submit a new pull request within 2 hours.

Make sure to review the acceptance criteria in the linked issue and verify all conditions are met before resubmitting.

@github-actions github-actions Bot closed this Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ Bounty $5k ] [ API ] Fix auth.py doesn't support API key authentication alongside JWT — mainnet prep

2 participants