Overview
Conductor provides a unified authentication system that supports multiple authentication methods out of the box. Each method integrates seamlessly with the UnifiedRouter and can be configured globally, per-type, per-path, or per-member. Supported Authentication Methods:- Bearer Token (JWT) - For API authentication with JSON Web Tokens
- API Key - For service-to-service authentication with KV storage
- Cookie Session - For browser-based authentication with KV sessions
- Unkey - Integration with Unkey.dev for managed API keys
- Custom Validators - For webhook signatures (Stripe, GitHub, Twilio, etc.)
Bearer Token Authentication
Bearer token authentication uses JSON Web Tokens (JWT) for stateless API authentication.Configuration
Environment Variables
Token Generation
Using Node.js (jose library)
Using OpenSSL (for testing)
Usage
Clients send the token in the Authorization header:Token Validation
The Bearer validator automatically checks:- Token signature validity
- Expiration time (
expclaim) - Not-before time (
nbfclaim) - Issuer (
issclaim) - Audience (
audclaim)
Accessing User Context
In your member handlers:Security Best Practices
- Use Strong Secrets: Minimum 32 characters, use
openssl rand -base64 32 - Set Appropriate Expiration: 1 hour for sensitive operations, 24 hours for regular APIs
- Validate Issuer/Audience: Always set and validate to prevent token reuse
- Use HTTPS Only: Never send tokens over unencrypted connections
- Rotate Secrets Regularly: Implement secret rotation strategy
- Store Securely: Never log or expose tokens in error messages
API Key Authentication
API key authentication stores keys in Cloudflare KV for stateful service-to-service authentication.Configuration
Environment Variables
KV Namespace Setup
Inwrangler.toml:
Creating API Keys
API keys are stored in KV with metadata:Usage
Clients can send API keys in multiple ways: Header (recommended):Key Rotation
Security Best Practices
- Use UUIDs or Cryptographically Random Keys: Never use predictable keys
- Set Expiration: Always set TTL for keys (30-90 days recommended)
- Implement Key Rotation: Provide users ability to rotate keys
- Audit Key Usage: Log API key usage for security monitoring
- Rate Limit Per Key: Prevent abuse with per-key rate limits
- Revocation Strategy: Implement immediate key revocation when compromised
Cookie Session Authentication
Cookie-based authentication for browser applications with server-side sessions stored in KV.Configuration
Environment Variables
KV Namespace Setup
Inwrangler.toml:
Creating Sessions
Logging Out
Session Refresh
Security Best Practices
- Always Use Secure Cookies: Set
Secureflag in production - HttpOnly Flag: Prevent XSS attacks by making cookies inaccessible to JavaScript
- SameSite Protection: Use
SameSite=LaxorStrictto prevent CSRF - Short Session Lifetime: 24 hours maximum, refresh on activity
- Session Invalidation: Delete sessions on logout and password change
- IP Binding (Optional): Store and validate IP address for additional security
Unkey Integration
Unkey.dev provides managed API key infrastructure with built-in analytics and rate limiting.Configuration
Environment Variables
Setup Unkey
- Sign up at unkey.dev
-
Create an API:
- Go to APIs section
- Click “Create API”
- Note your API ID
-
Generate Root Key:
- Go to Settings > Root Keys
- Create new root key with permissions:
api.*.read_key - Save the key securely
Creating Keys via Unkey Dashboard
- Go to your API in Unkey dashboard
- Click “Create Key”
- Set permissions, rate limits, and expiration
- Distribute key to users
Creating Keys Programmatically
Usage
Key Verification
Unkey automatically tracks:- Key validity
- Rate limit status
- Usage analytics
- Geographic distribution
Security Best Practices
- Use Namespaces: Separate keys by environment (production, staging)
- Set Expiration: Always set key expiration dates
- Implement Rate Limits: Protect APIs with per-key rate limits
- Monitor Usage: Use Unkey analytics to detect abuse
- Revoke Compromised Keys: Immediately revoke in dashboard
Custom Validators
Custom validators enable webhook signature verification for third-party services.Supported Services
- Stripe - Webhook signature verification
- GitHub - Webhook signature verification
- Twilio - Request signature verification
Stripe Webhooks
Configuration
Environment Variables
Setup
-
Create webhook endpoint in Stripe Dashboard:
- Go to Developers > Webhooks
- Add endpoint:
https://your-domain.com/webhooks/stripe - Select events to listen for
- Copy webhook signing secret
- Configure in conductor.config.ts:
GitHub Webhooks
Configuration
Environment Variables
Setup
-
Create webhook in GitHub repository:
- Go to Settings > Webhooks
- Add webhook URL:
https://your-domain.com/webhooks/github - Set Content type:
application/json - Set Secret: generate random string
- Select events
- Configure in conductor.config.ts:
Twilio Webhooks
Configuration
Environment Variables
Setup
-
Configure webhook in Twilio Console:
- Go to Phone Numbers
- Select number
- Set webhook URL for SMS/Voice:
https://your-domain.com/webhooks/twilio
- Configure in conductor.config.ts:
Security Best Practices
- Always Verify Signatures: Never process webhooks without verification
- Use HTTPS: Webhooks must be delivered over secure connections
- Implement Replay Protection: Check timestamps and reject old requests
- Rate Limit Webhooks: Protect against webhook flooding
- Log All Webhooks: Audit trail for security and debugging
- Validate Payload Structure: Verify payload matches expected schema
Multi-Method Authentication
Routes can accept multiple authentication methods:Common Patterns
API with multiple client types:Role-Based Access Control (RBAC)
Configuring Roles
Setting Roles in Tokens
Bearer Token:Permission-Based Access Control
Configuring Permissions
Setting Permissions
Rate Limiting
Per-User Rate Limiting
Per-IP Rate Limiting
Per-API-Key Rate Limiting
Failure Handling
Redirect to Login
Show Custom Error Page
Return JSON Error
Stealth Mode (404 Instead of 401)
Environment Variables Reference
Complete List
| Variable | Auth Method | Required | Description |
|---|---|---|---|
JWT_SECRET | Bearer | Yes | Secret key for JWT signing (min 32 chars) |
JWT_ISSUER | Bearer | No | Expected issuer claim in JWT |
JWT_AUDIENCE | Bearer | No | Expected audience claim in JWT |
API_KEY_KV_NAMESPACE | API Key | Yes | KV binding name for API keys |
API_KEY_HEADER | API Key | No | Header name (default: X-API-Key) |
SESSION_KV_NAMESPACE | Cookie | Yes | KV binding name for sessions |
SESSION_COOKIE_NAME | Cookie | No | Cookie name (default: __session) |
SESSION_SECRET | Cookie | Yes | Secret for session token signing |
SESSION_DOMAIN | Cookie | No | Cookie domain |
SESSION_MAX_AGE | Cookie | No | Max session age in seconds |
UNKEY_ROOT_KEY | Unkey | Yes | Unkey root key for API operations |
UNKEY_API_ID | Unkey | Yes | Unkey API ID |
UNKEY_NAMESPACE | Unkey | No | Namespace for key isolation |
STRIPE_WEBHOOK_SECRET | Custom (Stripe) | Yes | Stripe webhook signing secret |
GITHUB_WEBHOOK_SECRET | Custom (GitHub) | Yes | GitHub webhook secret |
TWILIO_AUTH_TOKEN | Custom (Twilio) | Yes | Twilio auth token |
Testing Authentication
Testing Bearer Tokens
Testing API Keys
Testing Sessions
Testing Webhooks
Troubleshooting
”Invalid token” Error
Bearer Token:- Check
JWT_SECRETmatches signing secret - Verify token hasn’t expired
- Check
issandaudclaims if configured
- Verify key exists in KV namespace
- Check key hasn’t expired
- Ensure correct KV binding name
”Unauthorized” for Valid Credentials
- Check auth method is listed in
methodsarray - Verify environment variables are set correctly
- Check KV namespace bindings in wrangler.toml
- Enable audit logging to see validation details
Session Not Persisting
- Verify
SESSION_DOMAINmatches your domain - Check cookies are set with
HttpOnly,Secure,SameSite - Ensure session hasn’t expired in KV
- Verify cookie is being sent by browser
Webhook Signature Verification Failing
- Check webhook secret matches exactly
- Verify timestamp tolerance (default 5 minutes)
- Ensure payload hasn’t been modified
- Test with webhook testing tools provided by service
Related Documentation
- Routing Overview - Complete routing guide
- Configuration Reference - Full config options
- Docs Member - API documentation with auth
- Page Member - Web pages with auth

