server

command module
v0.0.0-...-c3d4352 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2025 License: GPL-3.0 Imports: 5 Imported by: 0

README ΒΆ

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:

  1. Domain layer has no external dependencies
  2. All dependencies injected via interfaces
  3. Each layer has single responsibility
  4. Write tests for new functionality
  5. Update documentation for changes

Phase 3 Complete βœ… - Clean Architecture + PRF Implementation

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL