feat(go-api): API key middleware and per-key rate limiting#31
Open
Depo-dev wants to merge 4 commits into
Open
Conversation
APIKey middleware reads X-API-Key, HMAC-SHA256 hashes it with API_KEY_SALT, and checks against comma-separated API_KEY_HASHES env var. GET /v1/health is exempt from auth. Returns 401 on missing or invalid key. RateLimit middleware uses golang.org/x/time/rate token bucket per API key (100 req/s sustained, burst 200; configurable via RATE_LIMIT_RPS and RATE_LIMIT_BURST). Returns 429 with Retry-After: 1 on limit exceeded. 5 tests: valid key passes, missing key 401, invalid key 401, health bypass, burst exceeded 429.
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.
Summary
Adds authentication and rate limiting middleware to the Go API.
API Key middleware (
middleware/auth.go)X-API-Keyheader; HMAC-SHA256 hashes it withAPI_KEY_SALTenv varAPI_KEY_HASHES(comma-separated pre-hashed keys)GET /v1/healthis exempt from authRate limiting (
middleware/ratelimit.go)golang.org/x/time/rateRATE_LIMIT_RPS+RATE_LIMIT_BURST)Retry-After: 1on limit exceededTests (all pass,
go vetclean)TestAPIKey_validKeyPassesTestAPIKey_missingHeader_returns401TestAPIKey_invalidKey_returns401TestAPIKey_healthSkipsAuth/v1/healthwith no keyTestRateLimit_exceedBurst_returns429Retry-Afterafter burst=2 exceededCloses #16