This user authentication system in Go includes role-based access control (RBAC) and refresh tokens. It supports user registration, login, two-factor authentication (2FA) using TOTP, and secure access to protected routes with role-based permissions.
- User registration with hashed passwords
- User login with username and password
- Two-factor authentication (2FA) using TOTP
- QR code generation for easy 2FA setup
- JWT-based access tokens for authorization
- Refresh tokens for session management
- Role-based access control (RBAC)
- PostgreSQL integration for data storage
- Simple JSON-based API
- Go
- Fiber - Web framework for Go
- PostgreSQL - Database for user data
- TOTP - Time-based One-Time Password (for 2FA)
- bcrypt - Password hashing
- JWT - JSON Web Tokens for authentication
- Role-Based Access Control (RBAC) - Authorization based on roles
- Go (version 1.16 or higher)
- Git
- PostgreSQL
- Set up environment variables:
JWT_SECRET- Secret key for JWT generationPRIVATE_KEY- Private key for signing access tokensPUBLIC_KEY- Public key for validating access tokens
-
Clone the repository:
git clone https://github.com/EmBachlitzanakis/2fa-authentication-system.git
-
Navigate to your project directory:
cd 2fa-authentication-system -
Install the dependencies:
go mod tidy
-
Run the application:
go run main.go
The application will start on http://localhost:8080.
- Endpoint:
POST /auth/signup - Request Body:
{ "username": "your_username", "password": "your_password", "role": "user" // or "admin" } - Response:
201 Createdif successful409 Conflictif the username already exists400 Bad Requestfor validation errors
- Endpoint:
POST /auth/login - Request Body:
{ "username": "your_username", "password": "your_password" } - Response:
200 OKif successful, with access and refresh tokens:{ "accessToken": "your_jwt_access_token", "refreshToken": "your_jwt_refresh_token" }401 Unauthorizedfor invalid credentials400 Bad Requestfor validation errors
- Endpoint:
POST /auth/enable-2fa - Request Body:
{ "username": "your_username" } - Response:
200 OKwith the 2FA secret and QR code URL if successful404 Not Foundif the user does not exist400 Bad Requestfor validation errors
- Endpoint:
POST /auth/verify - Request Body:
{ "username": "your_username", "code": "your_2fa_code" } - Response:
200 OKif the code is valid401 Unauthorizedif the code is invalid404 Not Foundif the user does not exist400 Bad Requestfor validation errors
- Endpoint:
POST /auth/refresh - Request Body:
{ "refreshToken": "your_refresh_token" } - Response:
200 OKwith a new access token:{ "accessToken": "new_jwt_access_token" }401 Unauthorizedif the refresh token is invalid or expired400 Bad Requestfor validation errors
- Endpoint:
GET /protected/dashboard - Authorization: Bearer token required
- Response:
200 OKwith a message if the JWT token is valid403 Forbiddenif the user lacks the necessary role401 Unauthorizedif the JWT token is invalid or missing
- Endpoint:
GET /protected/admin - Authorization: Bearer token required
- Response:
200 OKwith a message if the user has theadminrole403 Forbiddenif the user lacks theadminrole401 Unauthorizedif the JWT token is invalid or missing
- Refresh Token Rotation: The system rotates refresh tokens on every usage, enhancing security.
- Role-Based Access Control: Different routes are restricted based on user roles (e.g., "admin" or "user").
- Enhanced JWT Structure:
- Includes
iss(issuer),aud(audience),exp(expiration),iat(issued at), andsub(subject) claims. - Signed with RS256 using a private/public key pair for access tokens.
- Refresh tokens use HS256 for simpler validation.
- Includes
This project is licensed under the MIT License - see the LICENSE file for details.
- pquerna/otp - TOTP library for Go.
- golang.org/x/crypto/bcrypt - Password hashing library.