2FAir Backend - E2E Encrypted TOTP Vault
Status: β
Phase 3 Complete - Clean Architecture + PRF Implementation (Core Complete, Not Production Ready)
A secure, end-to-end encrypted TOTP (Time-based One-Time Password) vault backend built with clean architecture principles, Go, PostgreSQL, WebAuthn PRF (Pseudo-Random Function), and zero-knowledge encryption.
ποΈ Clean Architecture Implementation β
β
Architectural Layers
2FAir follows Uncle Bob's Clean Architecture with strict dependency rules:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β External Systems β
β HTTP Clients β PostgreSQL β OAuth Providers β WebAuthn β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β interfaces/ (Interface Layer) β
β β’ HTTP Handlers β’ Middleware β’ Server Setup β
β β’ Request/Response β’ Error Handling β’ Route Management β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β application/usecases/ (Application Layer) β
β β’ Auth Service β’ OTP Service β’ Business Logic β
β β’ Use Case Orchestration β’ Application-specific Logic β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β domain/ (Domain Layer) β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββββββ β
β β entities/ β β interfaces/ β β dto/ β β
β β β’ User β β β’ Services β β β’ Data Transfer Objects β β
β β β’ OTP β β β’ Repos β β β’ Request/Response β β
β β β’ WebAuthn β β β’ Contracts β β β’ Validation β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β infrastructure/ (Infrastructure Layer) β
β βββββββββββ βββββββββββ βββββββββββ βββββββββββββββββββββββ β
β β crypto/ β β totp/ β βwebauthn/β β database/ β β
β β β’ AES β β β’ TOTP β β β’ PRF β β β’ PostgreSQL β β
β β β’ HKDF β β β’ Codes β β β’ Auth β β β’ SQLC β β
β β β’ PBKDF2β β β’ Configβ β β’ Creds β β β’ Repositories β β
β βββββββββββ βββββββββββ βββββββββββ β β’ Migrations β β
β βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π― Architectural Benefits
- β
Dependency Inversion: All dependencies point inward to domain
- β
Interface Segregation: Clean contracts between layers
- β
Single Responsibility: Each package has one clear purpose
- β
Open/Closed Principle: Easy to extend without modification
- β
Testability: All dependencies injected via interfaces
- β
Maintainability: Clear separation enables safe changes
π Project Structure (Phase 3 Clean Architecture)
server/
βββ cmd/server/ # Application entry point
β βββ main.go # Dependency injection & startup
βββ internal/
β βββ application/ # π΅ Application Layer
β β βββ usecases/ # Business logic orchestration
β β βββ auth_service.go # Authentication use cases
β β βββ otp_service.go # OTP management use cases
β β
β βββ domain/ # π‘ Domain Layer (Core Business)
β β βββ entities/ # Business entities
β β β βββ user.go # User aggregate root
β β β βββ otp.go # OTP value object
β β β βββ webauthn.go # WebAuthn entities
β β βββ interfaces/ # Domain contracts
β β β βββ auth.go # Auth service contracts
β β β βββ crypto_service.go # Crypto service interface
β β β βββ totp_service.go # TOTP service interface
β β β βββ *_repository.go # Repository interfaces
β β βββ dto/ # Data transfer objects
β β
β βββ infrastructure/ # π’ Infrastructure Layer
β β βββ crypto/ # Cryptographic implementations
β β β βββ crypto_service.go # AES-GCM, HKDF, PBKDF2
β β β βββ crypto_service_test.go
β β βββ totp/ # TOTP implementations
β β β βββ totp_service.go # TOTP generation & validation
β β β βββ totp_service_test.go
β β βββ webauthn/ # WebAuthn implementations
β β β βββ webauthn_service.go # PRF support & auth
β β β βββ webauthn_service_test.go
β β βββ database/ # Database implementations
β β β βββ migrations/ # Goose SQL migrations
β β β βββ queries/ # SQLC SQL queries
β β β βββ sqlc/ # Generated SQLC code
β β β βββ postgres.go # DB connection
β β β βββ *_repository.go # Repository implementations
β β βββ config/ # Configuration management
β β βββ jwt/ # JWT token service
β β βββ oauth/ # OAuth implementations
β β
β βββ interfaces/ # π΄ Interface Layer
β βββ http/ # HTTP delivery mechanism
β βββ handlers/ # HTTP request handlers
β β βββ auth.go # Auth endpoints
β β βββ otp.go # OTP endpoints
β β βββ webauthn.go # WebAuthn endpoints
β β βββ health.go # Health check endpoints
β βββ middleware/ # HTTP middleware
β β βββ auth.go # JWT authentication
β β βββ cors.go # CORS configuration
β β βββ security.go # Security headers
β βββ server.go # HTTP server setup
β
βββ docs/ # API documentation (Swagger)
βββ Dockerfile # Production Docker image
βββ docker-compose.dev.yaml # Development environment
βββ Makefile # Development commands
βββ go.mod & go.sum # Go dependencies
βββ sqlc.yaml # SQLC configuration
π Quick Start
Prerequisites
- Go 1.22+ with module support
- Docker & Docker Compose for databases
- Make for convenient command execution
π§ Development Setup
# 1. Navigate to server directory
cd server
# 2. Install dependencies
make deps
# 3. Start databases via Docker
make db-up
# 4. Generate SQLC code from SQL queries
make generate
# 5. Build and run application
make build
make run
# Server starts at http://localhost:8080
β
Verify Installation
# Health check
curl http://localhost:8080/health
# API status
curl http://localhost:8080/v1/public/status
π οΈ Development Commands
# Application Lifecycle
make help # Show all available commands
make deps # Install/update dependencies
make generate # Generate SQLC code from SQL
make build # Compile application binary
make run # Start development server
make test # Run all tests
make test-cover # Run tests with coverage
# Database Management
make db-up # Start PostgreSQL via Docker
make db-down # Stop database services
make db-reset # Reset database
# Code Quality
make lint # Run linter
make fmt # Format code
make check # Run format + lint + test
# Docker Operations
make docker-run # Start development environment
make docker-down # Stop Docker containers
π Security Features
- Zero-Knowledge Architecture: Server never sees plaintext TOTP seeds
- Clean Architecture: Security enforced at domain layer
- WebAuthn PRF: Enhanced key derivation when available
- Fallback Compatibility: Works with all WebAuthn devices
- End-to-End Encryption: AES-256-GCM with authenticated encryption
- Audit Logging: All security events tracked
- Security Headers: CSP, HSTS, CORS protection
π Documentation
π€ Contributing
Follow clean architecture principles:
- Domain layer has no external dependencies
- All dependencies injected via interfaces
- Each layer has single responsibility
- Write tests for new functionality
- Update documentation for changes
Phase 3 Complete β
- Clean Architecture + PRF Implementation