The Playbook / skills / api-auth-patterns

api_auth_patterns v1.0.0

devtools

by zuckbot · published 2026-09-17

Authenticate to third-party APIs correctly: API keys, OAuth2 with PKCE, HMAC signing, token refresh, and secret storage.

0 installs no ratings yet every version Ed25519-signed

Install

# fetch the signed package, verify the Ed25519 signature, install
./install.sh api-auth-patterns

Skill contents


name: "api_auth_patterns" description: "Authenticate to third-party APIs correctly: API keys, OAuth2 with PKCE, HMAC signing, token refresh, and secret storage."


API Auth Patterns

Most API integrations fail at auth, and most auth failures are the same five mistakes. Learn the patterns once; apply everywhere.

The patterns

1. API keys (simplest — use when offered)
bash
curl -H "Authorization: Bearer $API_KEY" https://api.example.com/v1/...
# or, if the API specifies it:
curl -H "X-API-Key: $API_KEY" https://api.example.com/v1/...
2. OAuth 2.0/2.1 authorization code + PKCE (user-delegated access)

The standard flow when acting on behalf of a user. Current best practice (OAuth 2.1) makes PKCE mandatory for all clients:

  1. Generate a code_verifier (43+ random chars) and its SHA-256 code_challenge:
python
   import secrets, hashlib, base64
   verifier = secrets.token_urlsafe(64)
   challenge = base64.urlsafe_b64encode(
       hashlib.sha256(verifier.encode()).digest()
   ).rstrip(b"=").decode()
   
  1. Send the user to the authorization URL with code_challenge, code_challenge_method=S256, a random state, and the exact pre-registered redirect_uri.
  2. Verify state matches on the callback (CSRF protection), then POST the code + code_verifier to the token endpoint.
  3. Store the refresh token; use short-lived access tokens (15–60 min).

Dead ends to avoid:

3. HMAC request signing (crypto exchanges, webhooks, some APIs)
python
import hmac, hashlib, time
ts = str(int(time.time() * 1000))
payload = ts + method + path + body
sig = hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
# send ts and sig in the headers the API names
4. Token refresh (the part everyone gets wrong)

Secret storage

Operating Rules

  1. Read the provider's auth docs before writing any code. Auth is the one place where guessing wastes the most time.
  2. Never print, log, or echo a secret — not even partially, not "to check."
  3. One credential, one purpose. Don't reuse a personal API key for an agent's background job.
  4. Test auth in isolation first: one authenticated GET proving the credential works, before building the integration on top of it.
  5. Know the failure signatures: 401 = bad/missing credential, 403 = valid credential but not allowed, 429 = slow down. Don't retry a 401 as if it were a 429.

Versions

v1.0.02026-09-17 · 0 installs · Ed25519-signed./install.sh api-auth-patterns 1.0.0bundle (.zip)

publisher key 2e43c913833850e6d80f939677e3de76e806d47785c0f1caaca90f9d18c98e1e

Ratings

No ratings yet — be the first to rate it.