A professional, secure authentication system built with Go, designed for fintech applications with enterprise-grade security features.
- JWT Access & Refresh Tokens with short expiry times
- Password Hashing using bcrypt
- Rate Limiting (100 requests/minute per IP)
- Account Lockout after 5 failed login attempts
- Token Revocation system for compromised accounts
- Audit Logging for all security events
- Input Validation and sanitization
- CORS Protection with configurable origins
- Security Headers (XSS, CSRF, etc.)
- Admin Role (01): Full system access
- User Role (02): Limited access to personal data
- Granular Permissions for different endpoints
- Account Status Management (active/inactive/suspended)
- Password Strength Validation
- Email Format Validation
- Comprehensive Error Handling
- Professional Logging System
fintech-auth/
βββ main.go # Application entry point
βββ config/
β βββ config.go # Configuration management
βββ database/
β βββ database.go # Database initialization
βββ models/
β βββ user.go # User model and repository
βββ handlers/
β βββ auth.go # Authentication handlers
β βββ admin.go # Admin-only handlers
βββ middleware/
β βββ auth.go # Authentication & security middleware
βββ utils/
β βββ validator.go # Input validation utilities
β βββ token.go # JWT token utilities
βββ go.mod # Go dependencies
βββ Postman_Collection.json # API testing collection
βββ README.md # This file
- Go 1.21 or higher
- SQLite3 (included with go-sqlite3 driver)
- Clone/Create Project
mkdir fintech-auth
cd fintech-auth
- Initialize Go Module
go mod init fintech-auth
- Install Dependencies
go get github.com/golang-jwt/jwt/v5
go get github.com/gorilla/mux
go get github.com/mattn/go-sqlite3
go get golang.org/x/crypto
- Create Directory Structure
mkdir -p config database models handlers middleware utils
-
Copy Source Files Copy all the provided source files to their respective directories.
-
Run the Application
go run main.go
The server will start on http://localhost:8080
export PORT=8080
export DATABASE_URL=./fintech_auth.db
export JWT_SECRET=your-super-secret-jwt-key-change-in-production
export REFRESH_SECRET=your-super-secret-refresh-key-change-in-production
export ENVIRONMENT=development
- Email:
admin@fintech.com
- Password:
admin123
- Role:
01
(Admin)
POST /api/register
Content-Type: application/json
{
"email": "user@example.com",
"password": "SecurePass123!",
"role": "02"
}
POST /api/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "SecurePass123!"
}
POST /api/refresh
Content-Type: application/json
{
"refresh_token": "your_refresh_token_here"
}
GET /api/protected/profile
Authorization: Bearer <access_token>
POST /api/protected/change-password
Authorization: Bearer <access_token>
Content-Type: application/json
{
"current_password": "CurrentPass123!",
"new_password": "NewSecurePass123!"
}
GET /api/admin/users
Authorization: Bearer <admin_access_token>
POST /api/admin/revoke-token
Authorization: Bearer <admin_access_token>
Content-Type: application/json
{
"user_email": "user@example.com",
"reason": "Account compromised"
}
PUT /api/admin/user/{user_id}/status
Authorization: Bearer <admin_access_token>
Content-Type: application/json
{
"status": "suspended",
"reason": "Suspicious activity"
}
GET /api/admin/audit-logs?limit=100
Authorization: Bearer <admin_access_token>
- Import Collection: Import the provided
Postman_Collection.json
- Set Base URL: Update
{{baseUrl}}
tohttp://localhost:8080/api
- Test Sequence:
- Register a new user
- Login to get tokens
- Test protected endpoints
- Login as admin
- Test admin endpoints
- β Valid registration and login
- β Invalid email format validation
- β Weak password validation
- β Invalid JSON handling
- β Unauthorized access attempts
- β Role-based access control
- β Token refresh functionality
- β Admin token revocation
- β User status management
- Minimum 8 characters
- Must contain uppercase, lowercase, numbers, and special characters
- Protection against common passwords
- Bcrypt hashing with default cost
- Short-lived access tokens (15 minutes)
- Longer-lived refresh tokens (7 days)
- Unique token IDs for tracking
- Token revocation capability
- Proper token validation
- Account lockout after failed attempts
- Audit logging for all actions
- Rate limiting per IP address
- Secure password change process
- Input validation and sanitization
- Proper error handling
- CORS protection
- Security headers
- Role-based authorization
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT '02',
status TEXT NOT NULL DEFAULT 'active',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME,
failed_login_attempts INTEGER DEFAULT 0,
locked_until DATETIME
);
CREATE TABLE revoked_tokens (
id INTEGER PRIMARY KEY AUTOINCREMENT,
token_id TEXT UNIQUE NOT NULL,
user_email TEXT NOT NULL,
revoked_at DATETIME DEFAULT CURRENT_TIMESTAMP,
reason TEXT
);
CREATE TABLE audit_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_email TEXT,
action TEXT NOT NULL,
details TEXT,
ip_address TEXT,
user_agent TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
- Change default admin credentials
- Set strong JWT secrets
- Configure HTTPS
- Set up proper CORS origins
- Configure rate limiting
- Set up log monitoring
- Regular security audits
- Database backups
# Production environment
export ENVIRONMENT=production
export JWT_SECRET=your-256-bit-secret-key
export REFRESH_SECRET=your-256-bit-refresh-secret
export DATABASE_URL=/secure/path/to/database.db
USER_REGISTERED
- New user registrationLOGIN_SUCCESS
- Successful loginLOGIN_FAILED
- Failed login attemptTOKEN_REFRESHED
- Access token refreshedPASSWORD_CHANGED
- Password changeTOKEN_REVOKED
- Token revoked by adminSTATUS_CHANGED
- User status changedADMIN_*
- Admin actions
- Monitor failed login attempts
- Review audit logs regularly
- Clean up old revoked tokens
- Monitor rate limiting effectiveness
- Update dependencies regularly
- Follow Go best practices
- Add comprehensive tests
- Update documentation
- Security-first approach
- Performance considerations
This project is designed for educational and professional use. Ensure compliance with your organization's security policies and local regulations when deploying in production.