-
Notifications
You must be signed in to change notification settings - Fork 2
Authentication Setup
Complete guide to configuring Cloudflare Access (Zero Trust) authentication for D1 Database Manager.
D1 Manager uses Cloudflare Access (part of Cloudflare Zero Trust) for enterprise-grade authentication. This provides:
- π Enterprise SSO - GitHub, Google, Azure AD, Okta, and more
- π‘οΈ Zero Trust Security - Network-level protection
- π Centralized Management - One place to manage all users
- π Audit Logs - Track all authentication events
- π Free Tier - Up to 50 users at no cost
- Cloudflare account
- Domain managed by Cloudflare (or use workers.dev subdomain)
- D1 Manager deployed to Cloudflare Workers
User β Cloudflare Access Login β Identity Provider (GitHub/Google/etc)
β JWT Token β D1 Manager Worker β Validates JWT β Access Granted
- Go to Cloudflare Zero Trust Dashboard
- If prompted, create a new Zero Trust organization
- Choose a Team Name (e.g.,
mycompany)- This becomes:
https://mycompany.cloudflareaccess.com
- This becomes:
- Navigate to Settings β Authentication
- Click Add under Login Methods
- Select GitHub
- Follow the GitHub OAuth setup:
- Create a GitHub OAuth App at https://github.com/settings/developers
- Application name: D1 Manager
-
Homepage URL:
https://d1.yourdomain.com -
Authorization callback URL:
https://yourteam.cloudflareaccess.com/cdn-cgi/access/callback
- Copy Client ID and Client Secret to Cloudflare
- Click Save
- Navigate to Settings β Authentication
- Click Add under Login Methods
- Select Google
- Follow the Google OAuth setup:
- Create credentials at https://console.cloud.google.com/apis/credentials
- Application type: Web application
-
Authorized redirect URIs:
https://yourteam.cloudflareaccess.com/cdn-cgi/access/callback
- Copy Client ID and Client Secret to Cloudflare
- Click Save
Cloudflare Access supports:
- Azure AD
- Okta
- Yubico
- One-time PIN
- And more...
Follow similar steps for your chosen provider.
- Navigate to Access β Applications
- Click Add an application
- Select Self-hosted
- Configure the application:
Application name:
D1 Database Manager
Session Duration:
24 hours (or your preference)
Application domain:
For custom domain:
Type: Custom
Value: d1.yourdomain.com
For workers.dev subdomain:
Type: Custom
Value: d1-manager.your-account.workers.dev
App Launcher visibility: Visible (shows in Cloudflare Access app launcher)
Logo: Upload a logo (optional)
Custom login page: Use default or create custom
Create policies to control who can access D1 Manager.
Policy name: Admin Access
Action: Allow
Configure rules:
- Rule type: Emails
-
Value:
admin@yourdomain.com, developer@yourdomain.com
Policy name: GitHub Org Members
Action: Allow
Configure rules:
- Rule type: GitHub organization
-
Value:
your-github-org
Policy name: Company Email Domain
Action: Allow
Configure rules:
- Rule type: Emails ending in
-
Value:
@yourcompany.com
βββββββββββββββββββββββββββββββββββββββ
β Policy: Developer Access β
βββββββββββββββββββββββββββββββββββββββ€
β Action: Allow β
β Session duration: 24 hours β
βββββββββββββββββββββββββββββββββββββββ€
β Include: β
β β’ Emails ending in @company.com β
β β’ GitHub org: company-dev β
βββββββββββββββββββββββββββββββββββββββ€
β Exclude: β
β β’ Country: (none) β
β β’ IP ranges: (none) β
βββββββββββββββββββββββββββββββββββββββ
After creating the application:
- Go to Access β Applications
- Click on your D1 Manager application
- Scroll to Application Details
- Copy the Application Audience (AUD) tag
Example:
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
- Go to Settings β General
- Copy your Team Domain
Example:
https://mycompany.cloudflareaccess.com
Set the authentication secrets in your Worker:
# Set Team Domain (include https://)
npx wrangler secret put TEAM_DOMAIN
# Enter: https://yourteam.cloudflareaccess.com
# Set Policy AUD (Application Audience tag)
npx wrangler secret put POLICY_AUD
# Enter: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6# Deploy the Worker
npx wrangler deploy
# Visit your application
https://d1.yourdomain.comYou should be redirected to Cloudflare Access login.
- Navigate to your D1 Manager URL
- You should see Cloudflare Access login page
- Choose your identity provider (GitHub, Google, etc.)
- Authenticate with your credentials
- You should be redirected back to D1 Manager
The Worker automatically validates JWTs on every request:
// worker/utils/auth.ts
export async function validateAccessJWT(request: Request, env: Env) {
// Validates JWT from Cloudflare Access
// Returns user email if valid
// Returns null if invalid
}View authentication logs:
# Worker logs
npx wrangler tail
# Cloudflare Access logs
# Go to: Zero Trust β Logs β AccessFor local development, authentication is automatically bypassed:
// worker/index.ts
const isLocalhost = isLocalDevelopment(request);
if (isLocalhost) {
console.log('[Auth] Localhost detected, skipping JWT validation');
userEmail = 'dev@localhost';
}This allows testing without setting up authentication.
Create separate Access applications for different environments:
-
Production:
d1.yourdomain.com -
Staging:
d1-staging.yourdomain.com -
Development:
d1-dev.yourdomain.com
Each can have different policies and AUD tags.
- Go to Access β Applications β Your Application
- Scroll to App Launcher
- Enable Custom login page
- Enter your custom HTML/CSS
For API access without browser authentication:
- Go to Access β Service Auth β Service Tokens
- Click Create Service Token
- Name it:
D1 Manager API - Copy Client ID and Client Secret
- Use in API requests:
curl -H "CF-Access-Client-Id: <client-id>" \
-H "CF-Access-Client-Secret: <client-secret>" \
https://d1.yourdomain.com/api/databasesIf accessing from different domains, update CORS settings in worker/utils/cors.ts:
const allowedOrigins = [
'https://yourdomain.com',
'https://app.yourdomain.com',
];Configure how long users stay logged in:
- Go to Access β Applications β Your Application
- Edit Session Duration
- Options: 15min, 30min, 1hr, 12hr, 24hr, 1 week, 1 month
Only grant access to users who need it:
β
Allow: Developers and DBAs
β Deny: Everyone else
Require multi-factor authentication:
- Go to Settings β Authentication
- Enable Require MFA for your identity provider
Regularly review who's accessing D1 Manager:
- Go to Logs β Access
- Filter by application name
- Review authentication events
For sensitive databases, use shorter sessions:
Production: 1 hour
Staging: 12 hours
Development: 24 hours
Restrict access to specific IP ranges:
- Edit your Access Policy
- Add Include β IP ranges
- Enter your office/VPN IP ranges
Block access from specific countries:
- Edit your Access Policy
- Add Exclude β Country
- Select countries to block
Cause: JWT validation failed
Solutions:
- Check
TEAM_DOMAINis correct (includehttps://) - Verify
POLICY_AUDmatches your Access application - Clear browser cookies and try again
- Check Worker logs:
npx wrangler tail
Cause: JWT cookie not being set or validated
Solutions:
- Verify application domain in Access matches your deployed URL
- Check browser allows cookies
- Try incognito/private browsing mode
- Verify no browser extensions blocking cookies
Cause: User not in allowed policies
Solutions:
- Check Access policies include the user's email or domain
- Verify user's GitHub org (if using GitHub OAuth)
- Review Access logs to see why user was denied
Cause: Secrets misconfigured
Solutions:
# Verify secrets are set
npx wrangler secret list
# Re-set secrets
npx wrangler secret put TEAM_DOMAIN
npx wrangler secret put POLICY_AUD
# Redeploy
npx wrangler deployCause: Access application not configured for domain
Solutions:
- Verify Access application domain matches deployment
- Check application is enabled
- Verify policies are active
To temporarily disable authentication (not recommended for production):
// worker/utils/auth.ts
export async function validateAccessJWT(request: Request, env: Env) {
// Comment out JWT validation
// return null;
// Return fake user
return 'admin@localhost';
}- Go to https://github.com/settings/developers
- Click OAuth Apps β New OAuth App
- Fill in:
- Application name: D1 Database Manager
-
Homepage URL:
https://d1.yourdomain.com - Application description: Database management tool
-
Authorization callback URL:
https://yourteam.cloudflareaccess.com/cdn-cgi/access/callback
- Click Register application
- Generate a Client Secret
- Copy Client ID and Client Secret to Cloudflare Zero Trust
- Go to https://console.cloud.google.com/
- Create a new project or select existing
- Enable Google+ API
- Go to Credentials β Create Credentials β OAuth client ID
- Configure consent screen if prompted
- Choose Web application
- Add Authorized redirect URIs:
https://yourteam.cloudflareaccess.com/cdn-cgi/access/callback
- Copy Client ID and Client Secret to Cloudflare Zero Trust
- Production Deployment - Deploy D1 Manager
- Security Best Practices - Additional security measures
- Configuration - Configure Worker secrets
- Troubleshooting - Fix common issues
Need Help? See Troubleshooting or open an issue.
- Database Management
- R2 Backup Restore
- Scheduled Backups
- Table Operations
- Query Console
- Schema Designer
- Column Management
- Bulk Operations
- Job History
- Time Travel
- Read Replication
- Undo Rollback
- Foreign Key Visualizer
- ER Diagram
- Foreign Key Dependencies
- Foreign Key Navigation
- Circular Dependency Detector
- Cascade Impact Simulator
- AI Search
- FTS5 Full Text Search
- Cross Database Search
- Index Analyzer
- Database Comparison
- Database Optimization