Open-source edge authentication service architecture documentation
EdgeAuth is an open-source, serverless authentication service built on Cloudflare Workers, providing globally distributed, low-latency authentication with a clean, layered architecture.
- License: Apache 2.0
- Tech Stack: TypeScript + Cloudflare Workers + D1 + Hono
- Build Tools: pnpm monorepo + Turbo
- Current Version: v0.0.1 (Early Development)
EdgeAuth/
├── src/ # Core Layer (Technical-Oriented)
│ ├── core/ # Authentication logic (crypto, JWT, persistence)
│ └── domain/ # Domain models (User, OAuth, SSO)
├── services/ # Edge Services Layer (Cloudflare Workers)
│ ├── admin-api/ # Admin service + Schema management center
│ ├── oauth-api/ # OAuth 2.0 Provider
│ └── sso-api/ # SSO authentication service
├── apps/ # Application Layer (Future)
└── packages/ # Shared infrastructure (Future)
- Layered Architecture: Domain → Core → Services, clear dependency direction
- OOP Programming: One file per type, interface-first naming
- Workspace Dependencies: Internal packages via
workspace:*
EdgeAuth uses a single database architecture with Cloudflare's native migration system:
One D1 Database:
edgeauth-db- All tables in a single databaseusers- User accountssso_sessions- SSO sessionsoauth_clients,authorization_codes,access_tokens,refresh_tokens- OAuth
Core Principle - Cloudflare Native:
- ✅ Migrations in
/migrations/(root directory, Cloudflare standard) - ✅ Automatic tracking via
d1_migrationstable - ✅ Idempotent deployments (safe to run multiple times)
- Benefits:
- Simplified migration management
- No cross-database complexity
- Native Cloudflare tooling support
- Automatic migration history
All services bind to the same database:
| Service | DB Binding | Purpose |
|---|---|---|
| Admin API | ✅ (R/W) | User management, full access |
| Account API | ✅ (R/W) | User registration & login |
| SSO API | ✅ (R/W) | SSO sessions |
| OAuth API | ✅ (R/W) | OAuth flows |
Pure business logic, no technical dependencies:
// User domain models
import { UserService } from "edge-auth-domain/user";
// OAuth domain models
import { OAuthClient } from "edge-auth-domain/oauth";
// SSO domain models
import { SSOSession } from "edge-auth-domain/sso";Responsibilities:
- Domain models and entities
- Business rules and validations
- Domain services
Technical implementation layer:
// Password hashing (PBKDF2)
import { hashPassword, verifyPassword } from "edge-auth-core/crypto";
// JWT generation and validation (HS256)
import { generateToken, verifyToken } from "edge-auth-core/jwt";
// D1 Repository implementations
import { D1UserRepository } from "edge-auth-core/persistence";
// OAuth logic
import { OAuthService } from "edge-auth-core/oauth";Module Structure:
/crypto- Password hashing (Web Crypto API - PBKDF2)/jwt- JWT token operations (HS256 algorithm)/oauth- OAuth 2.0 logic/persistence- D1 database repositories
Responsibilities:
- User management APIs
- Admin operations
- System configuration
Database Bindings:
DB→ edgeauth-usersSSO_DB→ edgeauth-ssoOAUTH_DB→ edgeauth-oauth
Testing: BDD with Vitest + Cucumber
Responsibilities:
- OAuth 2.0 authorization flows
- Client management
- Token management
Database Bindings:
OAUTH_DB→ edgeauth-oauth
Status: Complete BDD test coverage
Responsibilities:
- User authentication
- SSO session management
- Token generation and verification
Database Bindings:
DB→ edgeauth-users (read)SSO_DB→ edgeauth-sso (read/write)
Status: In development
- Runtime: Cloudflare Workers
- Database: Cloudflare D1 (SQLite)
- Web Framework: Hono
- Language: TypeScript
- Password Hashing: Web Crypto API (PBKDF2)
- JWT Algorithm: HS256
- Build: tsup
- Test: Vitest
- Monorepo: Turbo
- Code Quality: Prettier + ESLint
- Git Hooks: Lefthook
- BDD:
@deepracticex/vitest-cucumber - Features:
.featurefiles in each service - Step Definitions: Aligned with business specs
@deepracticex/config-preset- Configuration presets@deepracticex/error-handling- Error handling utilities@deepracticex/logger- Logging system
# Build all packages (managed by Turbo)
pnpm build
# Type checking
pnpm typecheck
# Run tests
pnpm test
# Development mode
pnpm test:devAutomated via GitHub Actions:
-
Create Release (via
release.yml):- Merge
release/*branch PR to main - Automatically creates Git tag and GitHub Release
- Merge
-
Deploy (via
deploy.yml, triggered by Release):# Create/get database wrangler d1 create edgeauth-db # Apply migrations (Cloudflare native) wrangler d1 migrations apply edgeauth-db --remote # Deploy all services cd services/admin-api && wrangler deploy --env production cd services/account-api && wrangler deploy --env production cd services/sso-api && wrangler deploy --env production cd services/oauth-api && wrangler deploy --env production
Key Features:
- Fully automated deployment
- Idempotent (safe to rerun)
- Automatic migration tracking
- No manual steps required
Previous Approach: Multiple databases with manual migration execution
- 3 separate databases (users, sso, oauth)
- Manual SQL file execution via
wrangler d1 execute - No automatic migration tracking
- Risk of duplicate execution
Current Solution: Single database with Cloudflare native migrations
- 1 unified database (
edgeauth-db) - Cloudflare's migration system with automatic tracking
- Idempotent deployments
- Simplified management
Benefits:
- Automatic migration history in
d1_migrationstable - Safe to rerun deployments
- Standard Cloudflare tooling
- No custom migration tracking needed
Simplicity Through:
- Single database (no cross-database complexity)
- Native migration system (no custom tracking)
- Automated CI/CD (no manual deployment)
- Code sharing via workspace packages (no duplication)
Dependency Flow:
Services → Core → Domain
↓
Persistence (D1)
- Domain: Pure business logic, no technical dependencies
- Core: Technical implementation (crypto, JWT, repositories)
- Services: Application layer (Cloudflare Workers)
- Clean layered architecture
- Domain + Core layer implementation
- OAuth Worker with full BDD coverage
- Admin Worker basic framework
- Centralized schema management solution
- Complete development toolchain
- SSO Worker implementation
- Admin API routes
- OAuth migrations (0003)
- Email verification
- Password reset flow
- Admin dashboard (UI)
- SDK development (JavaScript/TypeScript)
- Rate limiting
- Refresh token support
// Domain-agnostic interface
interface UserRepository {
findByEmail(email: string): Promise<User | null>;
save(user: User): Promise<void>;
}
// D1-specific implementation in core
class D1UserRepository implements UserRepository {
constructor(private db: D1Database) {}
// Implementation details...
}// Business logic in domain
class UserService {
constructor(private userRepo: UserRepository) {}
async registerUser(email: string, password: string) {
// Business validation
// Password hashing
// User creation
}
}// Cloudflare Worker entry point
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Initialize services with D1 bindings
const userRepo = new D1UserRepository(env.DB);
const userService = new UserService(userRepo);
// Route handling with Hono
return app.fetch(request, env);
},
};-
Schema Management
- All migrations in
/migrations/(Cloudflare standard location) - Use
IF NOT EXISTSfor idempotency - Descriptive migration names (e.g.,
0001_create_users_table.sql) - Never modify applied migrations, create new ones
- All migrations in
-
Code Organization
- One file per type (OOP style)
- Interface-first naming (no Hungarian notation)
- Use
~for internal imports,@for external
-
Testing
- BDD for business logic (feature files)
- Unit tests for technical logic
- E2E tests for critical paths
- CI runs on every PR
-
Deployment
- Use GitHub Actions for all deployments
- No manual production deployments
- Test locally with
pnpm setup:local - Follow release branch workflow
- Database Management - Detailed database architecture
- API Documentation - API specifications
- Contributing Guide - Development guidelines
- Multiple database replicas (Cloudflare D1 feature)
- Caching layer (Cloudflare KV/R2)
- Rate limiting per user/IP
- MFA support
- Session management improvements
- Audit logging
- SDK libraries (JS/TS, Python, Go)
- CLI tools for management
- Local development improvements
Last Updated: 2025-10-12 Version: 0.0.1 Maintained By: Deepractice Team