Skip to content

feat: opt-in API-key auth for saas_web via CODEC_CARVER_API_KEYS#193

Open
seonghobae wants to merge 1 commit into
mainfrom
feat/api-key-auth
Open

feat: opt-in API-key auth for saas_web via CODEC_CARVER_API_KEYS#193
seonghobae wants to merge 1 commit into
mainfrom
feat/api-key-auth

Conversation

@seonghobae

Copy link
Copy Markdown
Contributor

Summary

Adds opt-in API-key authentication to the SaaS web app (saas_web.py) — the minimal step toward controlled deployment beyond localhost.

  • If env var CODEC_CARVER_API_KEYS is set (comma-separated keys), all requests except GET / (the upload UI page) require an X-API-Key header matching one of the configured keys; failures get 401 {"error": "Invalid or missing API key"}.
  • If unset (or only empty entries), behavior is exactly as today: fully open.
  • Keys are read from os.environ at request time, so key rotation needs no restart.
  • New middleware follows the existing @app.middleware("http") pattern and is registered between the size-limit and security-headers middlewares, so 401 responses still carry security headers and unauthenticated requests are rejected before body streaming.

Security

  • Constant-time comparison via hmac.compare_digest against each configured key.
  • Keys are never logged and never echoed in responses.
  • Whitespace around keys is stripped; empty entries are ignored.

Tests

New TestApiKeyAuth class (10 tests) using TestClient + patch.dict(os.environ):

  • no env var → /shrink open as before
  • env set + no header → 401; wrong key → 401
  • correct key → passes through to handler
  • GET / always 200 without a key
  • multiple comma-separated keys all valid
  • whitespace stripped, empty entries ignored, only-empty env stays open
  • direct unit test of get_configured_api_keys() parsing

Gates

  • coverage report --include=saas_web.py100% (114 stmts, 0 miss)
  • interrogate -c pyproject.toml saas_web.py100%
  • unittest discover -s tests → 123 tests, no new failures (only the baseline 5 macOS os.listxattr errors)

🤖 Generated with Claude Code

Add an optional authentication middleware to the SaaS web app so it can
be deployed beyond localhost with access control:

- If env var CODEC_CARVER_API_KEYS is set (comma-separated keys), all
  requests except GET / (the upload UI page) require an X-API-Key header
  matching one of the configured keys.
- If the variable is unset (or contains only empty entries), behavior is
  exactly as before: fully open.
- Keys are read from the environment at request time, so rotation needs
  no restart and tests can patch the env easily.
- Comparison uses hmac.compare_digest (constant-time); keys are never
  logged or echoed in responses. Failures return 401 JSON.
- Whitespace around keys is stripped; empty entries are ignored.

Gates: saas_web.py coverage 100%, interrogate 100%, no new test
failures (baseline 5 macOS os.listxattr errors unchanged).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CJRVbDrp1vGYkJgNHMGPpG
@seonghobae seonghobae enabled auto-merge (squash) July 6, 2026 14:26
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.

1 participant