OAuth2 Flow & Token Management
Automate OAuth2 token management with KnoxCall. Never manually refresh tokens again - KnoxCall handles OAuth2 authentication, token refresh, and rotation automatically.The OAuth2 Token Problem
Without KnoxCall, managing OAuth2 tokens is painful:- 100+ lines of token management code
- Must securely store tokens
- Handle refresh failures
- Deal with token rotation
- Race conditions in concurrent requests
With KnoxCall
- ✅ Refreshes tokens before expiry
- ✅ Handles token rotation
- ✅ Stores tokens encrypted (AES-256)
- ✅ Manages concurrent requests
- ✅ Retries on auth failures
- ✅ Zero downtime, zero code
Supported OAuth2 Providers
KnoxCall supports all OAuth2-compliant providers:Pre-Configured Providers
- Google Drive
- Gmail API
- Google Calendar
- Google Sheets
Microsoft
- Office 365
- OneDrive
- Outlook API
- Microsoft Graph
Salesforce
- Salesforce API
- Force.com
Slack
- Slack API
- Slack Webhooks
Custom OAuth2 Providers
Any OAuth2-compliant API:- Stripe
- GitHub
- Shopify
- QuickBooks
- Custom APIs
Setting Up OAuth2
Step 1: Register Your Application
First, register your application with the OAuth2 provider:- Google
- Microsoft
- Custom
- Go to Google Cloud Console
- Create a new project or select existing
- Enable the API you want to use (Drive, Gmail, etc.)
- Go to Credentials → Create Credentials → OAuth 2.0 Client ID
- Application type: Web application
- Authorized redirect URIs:
https://admin.knoxcall.com/oauth/callback - Save Client ID and Client Secret
Step 2: Create OAuth2 Secret in KnoxCall
- Navigate to Secrets → Add Secret
- Configure:
- Click Save
Step 3: Complete OAuth2 Authorization
After creating the secret, you need to authorize it:- Click Authorize button next to your OAuth2 secret
- You’ll be redirected to the provider’s login page
- Log in and grant permissions
- You’ll be redirected back to KnoxCall
- Tokens are automatically saved and encrypted
Step 4: Use in Routes
- Create or edit a route
- Target Base URL:
https://www.googleapis.com(for Google APIs) - In Header Injection, add:
- Save route
How Token Refresh Works
KnoxCall automatically refreshes tokens 5 minutes before expiry:- Zero downtime during refresh
- No race conditions
- Automatic retry on failure
- Token rotation handled
Token Storage & Security
OAuth2 tokens are stored with military-grade security:Encryption
- Algorithm: AES-256-GCM
- Key derivation: PBKDF2 with 100,000 iterations
- Master key: Stored in AWS KMS (or your key management)
Access Control
Tokens are:- ❌ Never exposed in logs
- ❌ Never returned via API
- ❌ Never visible in dashboard
- ✅ Only decrypted at request time
- ✅ Tenant-isolated
- ✅ RBAC-protected
Token Versioning
Every token refresh creates a new version:- View token refresh history
- Roll back to previous version (if needed)
- See when tokens were last refreshed
Handling Token Revocation
If a user revokes access or tokens expire:Automatic Detection
KnoxCall detects revoked tokens:Reauthorization
To reauthorize:- Go to Secrets → Select OAuth2 secret
- Status shows: REVOKED
- Click Reauthorize
- Complete OAuth2 flow again
- New tokens saved
Multi-User OAuth2
For multi-user applications, each user needs their own tokens:Approach 1: Per-User Secrets
Create a separate secret for each user:Approach 2: Dynamic Secret Selection
Use KnoxCall’s dynamic secret selection:{user_id} with the value from request context.
OAuth2 with Refresh Token Rotation
Some providers (like Google) rotate refresh tokens:- Saves new refresh token
- Invalidates old refresh token
- No manual intervention needed
Scopes & Permissions
Different scopes for different use cases:Google Drive (Read-Only)
Google Drive (Full Access)
Gmail (Send Email)
Microsoft Graph (Calendar)
Testing OAuth2 Routes
After setting up OAuth2:Monitoring OAuth2 Tokens
Token Health Dashboard
Monitor your OAuth2 tokens:- Last refreshed: When was the token last refreshed
- Expires at: When will the token expire
- Refresh attempts: How many times has refresh been attempted
- Status: Active, Expiring Soon, Revoked, Failed
Alerts
Set up alerts for OAuth2 issues:- Navigate to Alerts → Add Alert
- Type: OAuth2 Token Issues
- Configure:
Best Practices
1. Use Separate Secrets Per Environment
2. Request Minimal Scopes
Only request permissions you actually need: ❌ Bad:3. Monitor Token Health
Set up monitoring:- Alert on refresh failures
- Alert on token expiration
- Monitor refresh success rate
4. Handle Revocation Gracefully
When tokens are revoked:- Show clear error message to users
- Provide reauthorization link
- Don’t expose technical details
5. Test Token Refresh
Manually trigger refresh to test:- Go to secret details
- Click Force Refresh
- Check logs for success
Troubleshooting
”Invalid refresh token” errors
Causes:- User revoked access
- Refresh token expired (rare)
- OAuth app was deleted
- Reauthorize the secret
- Check OAuth app is still active
- Verify redirect URI matches
Token refresh fails silently
Check:- Alert configuration
- Error logs in KnoxCall
- OAuth provider’s API status
Concurrent request issues
Solution: KnoxCall handles this automatically with request queuing. If you see issues, contact support.Wrong scope errors
- Edit OAuth2 secret
- Add required scopes
- Reauthorize
Next Steps
Secret Management
Learn about managing secrets
Request Signing
Add signature verification
Advanced Configuration
Advanced route configuration
Monitoring
Monitor API usage and errors
📊 Statistics
- Level: intermediate
- Time: 20 minutes
🏷️ Tags
oauth2, authentication, tokens, google, microsoft