A modular, high-performance authentication service built with Go and PostgreSQL.
Moved away from a "God-file" approach to a Layered Design to ensure maintainability:
- Handler: Manages HTTP logic, JSON decoding/encoding, and routing using
chi. - Service: The "Brain" of the app. Handles business logic (bcrypt hashing, secure verification code generation).
- Repository: Data persistence layer using
pgxpoolfor high-concurrency connection management. - Migrations: Database versioning via
gooseand Go'sembedpackage to keep the schema "baked" into the binary.
- UUID v4: Used non-sequential unique identifiers (via
google/uuidand Postgresuuid-ossp) instead of typical integers for security and distributed safety. - Connection Pooling: Implemented
pgxpoolto allow hundreds of concurrent users to connect simultaneously without bottlenecks. - Security: One-way password hashing using
bcryptand cryptographically secure 6-digit verification codes. - Environment Isolation: Used a
.envfile and Docker Compose to manage secrets and infrastructure independently.
Ensure your .env file matches the following structure:
# Networking & Server
PORT=8080
DOMAIN=localhost
API_PREFIX=api
API_VERSION=v1
# Database Credentials
DB_USER=your_user
DB_PASSWORD=your_password
DB_NAME=auth_db
DB_PORT=5432
# Docker Specific
DB_HOST=db
APP_CONTAINER_NAME=auth_app
DB_CONTAINER_NAME=auth_db
COMPOSE_PROJECT_NAME=auth_serviceRebuild the Go binary and start the infrastructure containers:
docker-compose up --buildThis project implements a "Testing Pyramid" including Unit, HTTP, and Integration tests. Integration tests require an isolated database.
A. Start the Test Database Run the dedicated test container (mapped to port 5436 to avoid conflicts with development):
docker-compose up -d test_dbgo test -v ./...- Unit Tests: Validate business logic (Service layer) using mocks.
- HTTP Tests: Validate API endpoints and JSON encoding (Handler layer).
- Integration Tests: Validate real SQL queries against the test_db (Repository layer).
To retrieve the email_code for manual verification testing, use the following connection details:
- Host:
localhost - Port:
5433(as mapped indocker-compose.yml) - User/Pass: From your
.envfile
| Method | Endpoint | Description | Request Body / Params |
|---|---|---|---|
| POST | /api/v1/users/register |
Register a new user | JSONemail (string, required)password (string, required)name (string, required)allowed_apps (array of strings) |
| GET | /api/v1/users |
List all registered users | None |
| GET | /api/v1/users/{id} |
Retrieve a specific user | id (UUID) in URL path |
| PATCH | /api/v1/users/verify |
Confirm email | JSONemail (string, required)code (string, required) |
Note
Strict Paths: We use the versioned path /api/v1/users to allow for future API iterations without breaking existing clients.
Tip
Migrations: If you need to change the table structure, create a new .sql file in the /migrations folder instead of editing existing ones.
- IP-Based Rate Limiting: Implement a 5-attempt-per-minute limit per IP on
/loginand/register. - Automated IP Ban: Temporary blacklisting of IPs that exceed brute-force thresholds.
- Account Lockout: Secure user accounts after multiple failed password attempts.
- Fingerprinting: Track User OS, Browser, and Language for every login attempt.
- GDPR Compliance: Implementation of IP anonymization (xxx masking) for stored logs.
- Audit Logs: Record of sensitive actions (password changes, email updates) with masked IP data.
- Automated Verification: Immediate 6-digit code delivery via SMTP (SendGrid/Mailtrap).
- Welcome Sequence: Trigger a "Welcome" email automatically after a user confirms their email.
- Password Recovery: Secure "Forgot Password" flow with time-limited tokens.
- JWT & Refresh Tokens: Full session management with short-lived access tokens and secure rotation.
- RBAC (Role-Based Access Control): Permission layers for Owner, Admin, and SuperAdmin.
- Profile Management: Allow data owners to update their own info while restricting admin-only fields.
- Structured Logging: Integration of
slogfor JSON-based observability. - Health Monitoring: Real-time tracking of DB connection pool health and latency.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request