This document describes the three-tier API protection system implemented in the Bixor Trading Engine.
The API uses three levels of protection:
- Public API - No authentication required
- Backend Secret Protected - Requires backend secret (for frontend requests)
- Personal API - Requires user token (for future personal API access)
These routes can be accessed without any authentication:
GET /- Landing pageGET /api/v1/health- Health checkGET /api/v1/status- Service statusGET /api/v1/info- API information
Usage: Direct access, no headers required.
These routes require the X-Backend-Secret header (or X-API-Secret as alternative):
POST /api/v1/auth/register- User registrationPOST /api/v1/auth/login- User loginPOST /api/v1/auth/refresh- Refresh JWT tokensGET /api/v1/auth/me- Get current user (also requires JWT)POST /api/v1/auth/otp/request- Request OTP (also requires JWT)POST /api/v1/auth/otp/verify- Verify OTP (also requires JWT)
Usage from Frontend:
headers: {
'X-Backend-Secret': process.env.NEXT_PUBLIC_BACKEND_SECRET,
'Authorization': 'Bearer <jwt_token>', // For authenticated endpoints
}Usage from External:
curl -H "X-Backend-Secret: your-secret-here" \
-H "Authorization: Bearer <jwt_token>" \
https://api.example.com/api/v1/auth/meThese routes will require user-specific API tokens (not JWT):
GET /api/v1/personal/*- Personal API endpoints (to be implemented)
Usage: Will use custom user API tokens generated in user settings.
BACKEND_SECRET=your-super-secret-key-here-change-in-productionImportant:
- Generate a strong random secret for production
- Never commit the actual secret to version control
- If
BACKEND_SECRETis not set, the middleware allows all requests (development mode)
NEXT_PUBLIC_BACKEND_SECRET=your-super-secret-key-here-change-in-productionImportant:
- Must match the backend
BACKEND_SECRET - The
NEXT_PUBLIC_prefix makes it available in the browser - This is safe because it's only used to authenticate frontend requests
Located in internal/middleware/auth.go:
- Checks for
X-Backend-SecretorX-API-Secretheader - Compares against
BACKEND_SECRETenvironment variable - Returns 401 if secret is missing or invalid
- Allows requests if
BACKEND_SECRETis not set (development mode)
- Passthrough middleware for public routes
- No validation performed
- Placeholder for future personal API token validation
- Currently a passthrough
-
Backend Secret:
- Use a strong, randomly generated secret (minimum 32 characters)
- Rotate secrets periodically
- Use different secrets for development and production
-
CORS:
- Backend secret headers are allowed in CORS configuration
- Adjust CORS settings based on your deployment
-
Development Mode:
- If
BACKEND_SECRETis not set, all requests are allowed - This is convenient for development but should never be used in production
- If
Routes are organized in internal/routes/routes.go:
// Public routes
public := v1.Group("")
public.Use(middleware.PublicMiddleware())
// Backend secret protected routes
protected := v1.Group("")
protected.Use(middleware.BackendSecretMiddleware())
// Personal API routes (future)
personal := v1.Group("/personal")
personal.Use(middleware.UserTokenMiddleware())The AuthService class automatically includes the backend secret in all requests:
// Automatically adds X-Backend-Secret header
const response = await AuthService.login(email, password);The secret is read from process.env.NEXT_PUBLIC_BACKEND_SECRET and included in all API requests.
curl http://localhost:8080/api/v1/healthcurl http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"test"}'curl http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-H "X-Backend-Secret: your-secret-here" \
-d '{"email":"test@example.com","password":"test"}'-
Personal API Tokens:
- Users will be able to generate personal API tokens
- These tokens will be stored in the database
- Tokens will have scopes/permissions
- Tokens can be revoked by users
-
Rate Limiting:
- Implement rate limiting per secret/token
- Different limits for different protection levels
-
IP Whitelisting:
- Optional IP whitelisting for backend secret
- Additional security layer