@volcanicminds/backend includes a robust, native implementation of Multi-Factor Authentication (TOTP based), designed to secure access without requiring external identity providers.
MFA behavior is controlled globally via environment variables (MFA_POLICY) or the configuration file src/config/general.ts.
// src/config/general.ts
export default {
name: 'general',
options: {
// ...
mfa_policy: process.env.MFA_POLICY || 'OPTIONAL'
}
}
OPTIONAL (Default): Users can choose to enable or disable MFA from their profile settings.MANDATORY: MFA is enforced for all users.
ONE_WAY: Users are not forced to enable it, but once they do, they cannot disable it themselves. Only an administrator can reset it.The framework uses a Two-Stage Login process to ensure security. When MFA is required (due to policy or user preference), the login endpoint does not return a valid access token immediately.
POST /auth/login
If credentials are valid but MFA is pending:
202 Accepted{
"mfaRequired": true,
"mfaSetupRequired": false,
"tempToken": "eyJhbGci..."
}
pre-auth-mfa. It has a short lifespan (e.g., 5 minutes) and extremely limited privileges.The global onRequest hook inspects the JWT. If the role is pre-auth-mfa, it blocks access to all endpoints except the MFA whitelist:
/auth/mfa/setup/auth/mfa/enable/auth/mfa/verify/auth/logoutTrying to access /users or /orders with a temp token results in a 403 Forbidden.
POST /auth/mfa/verify
Authorization: Bearer <tempToken>{ "token": "123456" } (The TOTP code)If valid, the server returns 200 OK with the Final Access Token and Refresh Token. The user is now fully logged in.
If mfaSetupRequired is true:
/auth/mfa/setup (using tempToken).
secret, qrCode (Data URL), uri./auth/mfa/enable (using tempToken).
{ "secret": "...", "token": "..." }An administrator can disable MFA for a specific user (e.g., lost device) via API:
POST /users/:id/mfa/reset (Requires Admin privileges).
If the administrator themselves loses access to their MFA device, the framework provides a filesystem/environment-based “Backdoor” designed for emergency recovery.
Set Environment Variables:
Set these in your server environment (e.g., docker-compose.yml or .env):
MFA_ADMIN_FORCED_RESET_EMAIL=admin@example.com
MFA_ADMIN_FORCED_RESET_UNTIL=2025-12-31T15:00:00.000Z
EMAIL: The email of the admin user to reset.UNTIL: A timestamp in the near future (must be within 10 minutes of server startup for security).Restart Server:
On startup (index.ts), the framework checks these variables. If valid, it forcibly disables MFA for that user.
Login & Cleanup: The admin can now log in with just the password. Important: Remove these variables immediately after recovery to seal the security hole.