Prompt
I'm building a developer-facing SaaS and need secure API auth, what are my options?
Latest observation
For a developer-facing SaaS, your auth options usually fall into a few buckets. The right choice depends on whether you’re authenticating humans, machines, or both.
Common options
1) API keys
Best for: simple developer APIs, server-to-server usage, quick adoption
How it works:
- You issue each customer a secret key.
- They send it with requests, usually in a header.
Pros:
- Very simple to implement and use
- Easy onboarding for developers
- Good for identifying the calling app/account
Cons:
- Usually acts like a long-lived bearer token
- Hard to scope finely unless you build that
- If leaked, it can be abused until revoked
- Not ideal for user-granular permissions
Security tips:
- Use only over HTTPS
- Make keys revocable and rotatable
- Store hashed keys if possible
- Support per-environment keys
- Allow scopes/permissions if you can
2) OAuth 2.0
Best for: third-party integrations, delegated access, user-authorized apps
How it works:
- Users authorize your app to access their data.
- Your API issues access tokens, often with refresh tokens.
Pros:
- Industry standard
- Good for delegated access and consent
- Fine-grained scopes
- Strong ecosystem support
Cons:
- More complex to implement
- More moving parts: clients, authorization server, token lifecycle
- Can be overkill for simple machine-to-machine use
Security tips:
- Use Authorization Code + PKCE for user-facing apps
- Use short-lived access tokens
- Rotate refresh tokens
- Validate redirect URIs strictly
- Follow OAuth 2.1 guidance where possible
3) JWT access tokens
Best for: stateless auth, internal services, high-scale APIs
How it works:
- The token is self-contained and signed.
- Your API verifies signature and claims without calling an auth DB every time.
Pros:
- Fast validation
- Good for distributed systems
- Useful for embedding scopes/tenant/user info
Cons:
- Harder to revoke immediately unless you add extra infrastructure
- Token leakage risk is still real
- Poorly designed JWT usage can create security issues
Security tips:
- Sign, don’t encrypt unless needed
- Keep expirations short
- Validate issuer, audience, expiry, and algorithm
- Avoid putting sensitive data in the token
- Use key rotation via JWKS
4) Session cookies
Best for: web apps, dashboards, browser-based admin interfaces
How it works:
- Browser stores a cookie tied to a server-side session.
Pros:
- Very good security model for browser apps
- Easier CSRF protection patterns than raw bearer tokens in some cases
- Server-side revocation is straightforward
Cons:
- Not ideal for public API consumers
- Less convenient for non-browser clients
- CORS/CSRF considerations for browser APIs
Security tips:
- Use HttpOnly, Secure, SameSite cookies
- Protect against CSRF
- Use short session lifetimes and idle timeouts
5) HMAC-signed requests
Best for: high-security server-to-server APIs, payment-like workflows
How it works:
- Client signs each request with a secret.
- Server verifies the signature using the shared secret.
Pros:
- Prevents simple request tampering
- Can include timestamps/nonces to reduce replay attacks
- Good for strict integrity requirements
Cons:
- More complex for clients
- Harder to debug
- You need a robust canonicalization scheme
Security tips:
- Include timestamp, nonce, method, path, body hash
- Reject old timestamps
- Track nonces to prevent replay
- Be careful with canonicalization edge cases
6) mTLS / client certificates
Best for: enterprise, internal APIs, very high-security integrations
How it works:
- Client authenticates with a certificate during TLS handshake.
Pros:
- Strong cryptographic client authentication
- Great for service-to-service trust
- Reduces reliance on shared secrets in headers
Cons:
- Operationally heavy
- Certificate issuance/rotation is more complex
- Usually not the easiest choice for external developers
Security tips:
- Automate certificate issuance and rotation
- Use short-lived certs if possible
- Pair with authorization checks; authn alone is not enough
What most SaaS products do
A common pattern is:
- Human users / dashboard: session cookies or OAuth login
- Developer API access: API keys or OAuth access tokens
- Enterprise / service integrations: OAuth, mTLS, or HMAC depending on need
Many successful APIs offer:
- API keys for quick start
- OAuth for user-consented integrations
- Scoped permissions
- Token rotation and revocation
- Audit logs
Practical recommendation
If you’re building a developer-facing SaaS, a strong default is:
Start with:
- API keys for simple machine-to-machine access
- OAuth 2.0 if customers need user-consented access or third-party integrations
Internally:
- Use short-lived JWTs or opaque access tokens behind the scenes
- Support scopes, revocation, and rotation
For browser-based admin UI:
- Use session cookies, not API keys
Security checklist regardless of method
- Always use HTTPS
- Store secrets securely and hash them where possible
- Support revocation
- Use scopes/least privilege
- Log auth events and anomalies
- Rate limit by token/customer
- Rotate secrets/keys regularly
- Use short-lived credentials where possible
- Validate audience/issuer/expiry for tokens
If you want, I can also help you choose based on your exact product shape, like:
- public API vs private integrations
- browser app vs machine-to-machine
- B2B SaaS vs consumer SaaS
- whether you need user-level permissions or just account-level access