Skip to main content

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:
Problems:
  • 100+ lines of token management code
  • Must securely store tokens
  • Handle refresh failures
  • Deal with token rotation
  • Race conditions in concurrent requests

With KnoxCall

KnoxCall automatically:
  • ✅ Refreshes tokens before expiry
  • ✅ Handles token rotation
  • ✅ Stores tokens encrypted (AES-256)
  • ✅ Manages concurrent requests
  • ✅ Retries on auth failures
  • ✅ Zero downtime, zero code

OAuth Clients page

The OAuth Clients page (Settings → Security → OAuth Clients) is the unified surface for managing OAuth 2.1 in KnoxCall. It has three tabs:
There are two distinct OAuth flows in KnoxCall:
  1. OAuth2 Secrets (Secrets page) — store provider tokens (Google, Stripe, etc.) that KnoxCall auto-refreshes and injects into route headers. Use these for outbound API calls through your routes.
  2. OAuth Client Registrations (OAuth Clients page) — register OAuth clients that grant access to the KnoxCall API itself. Use these for Workload Identity Federation bindings and programmatic API access with DPoP-bound tokens.
Use the Active Tokens tab as your primary monitoring surface for live token state — you can see which tokens are DPoP-bound, when they expire, and when they were last used, without needing to set up an alert rule.

Supported OAuth2 Providers

KnoxCall supports all OAuth2-compliant providers:

Pre-Configured Providers

Google

  • 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:
  1. Go to Google Cloud Console
  2. Create a new project or select existing
  3. Enable the API you want to use (Drive, Gmail, etc.)
  4. Go to CredentialsCreate CredentialsOAuth 2.0 Client ID
  5. Application type: Web application
  6. Authorized redirect URIs: https://oauth.knoxcall.com/callback
  7. Save Client ID and Client Secret

Step 2: Create OAuth2 Secret in KnoxCall

  1. Navigate to SecretsAdd Secret
  2. Configure:
Name:
Type:
Provider: Select from dropdown or choose “Custom” Client ID:
Client Secret:
Scopes:
Redirect URI:
  1. Click Save

Step 3: Complete OAuth2 Authorization

After creating the secret, you need to authorize it:
  1. Click Authorize button next to your OAuth2 secret
  2. You’ll be redirected to the provider’s login page
  3. Log in and grant permissions
  4. You’ll be redirected back to KnoxCall
  5. Tokens are automatically saved and encrypted
Done! KnoxCall now has your OAuth2 tokens.

Step 4: Use in Routes

  1. Create or edit a route
  2. Target Base URL: https://www.googleapis.com (for Google APIs)
  3. In Header Injection, add:
  1. Save route
Now all requests to this route will include the OAuth2 token automatically!

How Token Refresh Works

KnoxCall uses intelligent token refresh that adapts to each provider’s token lifetime:

Dynamic Refresh Threshold

Tokens are automatically refreshed when 25% of their lifetime remains (or 5 minutes before expiry, whichever is greater):
Example: 1-hour token lifecycle
Why 75% threshold?
  • Short tokens (< 20 min): Prevents last-second refresh failures
  • Long tokens (> 1 hour): Avoids unnecessary early refreshes
  • Variable tokens: Adapts to each provider’s expiry time
  • Network delays: Buffer time for slow refresh responses
Benefits:
  • Adapts to any token lifetime (5 min to 90 days)
  • Zero downtime during refresh
  • No race conditions
  • Automatic retry on failure
  • Graceful fallback if expires_in missing
  • Token rotation handled transparently

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:
You can:
  • 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:
  1. Go to Secrets → Select OAuth2 secret
  2. Status shows: REVOKED
  3. Click Reauthorize
  4. Complete OAuth2 flow again
  5. 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:
Use client IDs to route to the right secret:

Approach 2: Dynamic Secret Selection

Use KnoxCall’s dynamic secret selection:
KnoxCall will replace {user_id} with the value from request context.

OAuth2 with Refresh Token Rotation

Some providers (like Google) rotate refresh tokens:
KnoxCall handles this automatically:
  • 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)

Request minimum scopes needed. Users are more likely to grant limited permissions.

Testing OAuth2 Routes

After setting up OAuth2:
You should receive a successful response with your Google Drive files.

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:
  1. Navigate to AlertsAdd Alert
  2. Type: OAuth2 Token Issues
  3. Configure:

Best Practices

1. Use Separate Secrets Per Environment

2. Request Minimal Scopes

Only request permissions you actually need: Bad:
Good:

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:
  1. Go to secret details
  2. Click Force Refresh
  3. Check logs for success

Troubleshooting

”Invalid refresh token” errors

Causes:
  • User revoked access
  • Refresh token expired (rare)
  • OAuth app was deleted
Fix:
  • 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

Fix:
  1. Edit OAuth2 secret
  2. Add required scopes
  3. 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