This TypeScript library provides functions for Multi-Factor Authentication (MFA), including generating MFA authentication keys, tokens, verifying tokens, and generating Time-based One-Time Password (TOTP) URIs.
It Can be with various authenticator devices, including
- Google Authenticator,
- Authy,
- Microsoft Authenticator and many more.
Install the package using npm:
npm install mfa-authy
import {
generateMFAAuthKey,
verifyMFAAuthToken,
generateTotpUri,
} from 'mfa-authy';
const generatedKey = generateMFAAuthKey();
console.log('Generated Authenticator Key:', generatedKey);
const isTokenValid = verifyMFAAuthToken(generatedKey, generatedToken);
console.log('Is Authenticator Token Valid?', isTokenValid);
This functions returns a base64 encoded URI that can be scanned by authenticator apps, such as Google Authenticator, Authy, and Microsoft Authenticator. Updated to an Async function to allow for the use of await.
const totpUri = await generateTotpUri(generatedKey, 'user@example.com', 'MyAuthenticatorApp', 'SHA256', 8, 60);
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAADkCAYAAACIV4iNAAAAAklEQVR4AewaftIAAAxKSURBVO3BQW4ER5IAQfcC//9lXx3jlEChm1RqNszsH6y1rvCw1rrGw1rrGg9rrWs8rLWu8bDWusbDWusaD2utazysta7xsNa6xsNa6xoPa61rPKy1rvGw1rrGw1rrGg9rrWv88CGVv1QxqfymihOVqeINlaniDZWp4kTlpGJS+U0Vk8pUcaIyVUwqf6niEw9rrWs8rLWu8bDWusYPX1bxTSpvVLyhMlV8k8obKm9UTConFZ+omFR+k8o3VXyTyjc9rLWu8bDWusbDWusaP/wylTcq3qh4Q+VEZaqYVE5UpopJZao4UZkqTipOVKaKSWWq+ETFicpJxaTyTSpvVPymh7XWNR7WWtd4WGtd44f/OJU3Kk5UJpW/pDJVnKhMFW+onKhMFVPFJyomlTcq/pc8rLWu8bDWusbDWusaP/zHVUwqn6iYVKaKT1RMKlPFpPKGyknFN6mcVEwqn6j4X/aw1rrGw1rrGg9rrWv88Msq/k0Vk8onVKaKSeWkYqqYVE4q3lA5UZkqJpWpYqqYVD5RMalMFd9UcZOHtdY1HtZa13hYa13jhy9T+UsqU8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Wu8bDWusYP/8+onFRMKlPFpHJS8ZsqJpWp4kRlqnhD5RMVk8pU8QmV/5KHtdY1HtZa13hYa13jh19W8ZsqJpWpYlJ5o2JSmSpOVKaKSeWk4kTlDZWp4o2KT6h8k8pUMVV8QmWq+E0Pa61rPKy1rvGw1rrGD1+m8pdUTlROKk5UTlTeULmZyhsqU8VJxaRyojJVTBWTyknFJ1ROKj7xsNa6xsNa6xoPa61r2D9Ya13hYa11jYe11jUe1lrXeFhrXeNhrXWNh7XWNR7WWtd4WGtd42GtdY2HtdY1HtZa13hYa13jYa11jYe11jUe1lrX+D9NKIN6tFQSVAAAAABJRU5ErkJggg==
This can be easily displayed in an HTML page using the following code:
<img alt="QR-Code" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAADkCAYAAACIV4iNAAAAAklEQVR4AewaftIAAAxKSURBVO3BQW4ER5IAQfcC//9lXx3jlEChm1RqNszsH6y1rvCw1rrGw1rrGg9rrWs8rLWu8bDWusbDWusaD2utazysta7xsNa6xsNa6xoPa61rPKy1rvGw1rrGw1rrGg9rrWv88CGVv1QxqfymihOVqeINlaniDZWp4kTlpGJS+U0Vk8pUcaIyVUwqf6niEw9rrWs8rLWu8bDWusYPX1bxTSpvVLyhMlV8k8obKm9UTConFZ+omFR+k8o3VXyTyjc9rLWu8bDWusbDWusaP/wylTcq3qh4Q+VEZaqYVE5UpopJZao4UZkqTipOVKaKSWWq+ETFicpJxaTyTSpvVPymh7XWNR7WWtd4WGtd44f/OJU3Kk5UJpW/pDJVnKhMFW+onKhMFVPFJyomlTcq/pc8rLWu8bDWusbDWusaP/zHVUwqn6iYVKaKT1RMKlPFpPKGyknFN6mcVEwqn6j4X/aw1rrGw1rrGg9rrWv88Msq/k0Vk8onVKaKSeWkYqqYVE4q3lA5UZkqJpWpYqqYVD5RMalMFd9UcZOHtdY1HtZa13hYa13jhy9T+UsqU8WkMlVMKlPFpDJVTCpTxaRyojJVTConKlPFX1KZKiaVqWJS+YTKVHGicr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V/5KHtdY1HtZa13hYa13jh19W8ZsqJpWpYlJ5o2JSmSpOVKaKSeWk4kTlDZWp4o2KT6h8k8pUMVV8QmWq+E0Pa61rPKy1rvGw1rrGD1+m8pdUTlROKk5UTlTeULmZyhsqU8VJxaRyojJVTBWTyknFJ1ROKj7xsNa6xsNa6xoPa61r2D9Ya13hYa11jYe11jUe1lrXeFhrXeNhrXWNh7XWNR7WWtd4WGtd42GtdY2HtdY1HtZa13hYa13jYa11jYe11jUe1lrX+D9NKIN6tFQSVAAAAABJRU5ErkJggg=="/>
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const mfaAuthy = require('mfa-authy');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(session({ secret: 'your-secret-key', resave: false, saveUninitialized: true }));
// Example user data (replace this with your user authentication system)
const users = {
'user@example.com': {
password: 'password123',
mfaSecret: null, // To be set during MFA setup
mfaEnabled: false //boolean
},
};
// Middleware to check if the user is authenticated
function isAuthenticated(req, res, next) {
if (req.session && req.session.user) {
next();
} else {
res.status(401).json({ error: 'Unauthorized' });
}
}
// Endpoint to initiate login with 2FA
app.post('/login', (req, res) => {
const { email, password } = req.body;
// Check if user exists and password is correct
if (users[email] && users[email].password === password) {
req.session.user = { email };
// Check if MFA is already set up for the user
if (users[email].mfaSecret && users[email].mfaEnabled) {
res.json({ requireMFA: true });
} else {
res.json({ requireMFA: false });
}
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
});
// Endpoint to set up MFA
app.post('/setup-mfa', isAuthenticated, async (req, res) => {
const { email } = req.session.user;
// Generate MFA secret and TOTP URI for the user
users[email].mfaSecret = mfaAuthy.generateMFAAuthKey();
const totpUri = await mfaAuthy.generateTotpUri(users[email].mfaSecret, email, 'MyApp', 'SHA1', 6, 30);
const mfaSecret = users[email].mfaSecret.toUpperCase().replace(/\s/g, '');
res.json({ totpUri, mfaSecret });
});
// Endpoint to enable MFA
app.post('/enable-mfa', isAuthenticated, (req, res) => {
const { email } = req.session.user;
const { token } = req.body;
const isTokenValid = mfaAuthy.verifyMFAAuthToken(users[email].mfaSecret, token);
if (isTokenValid) {
users[email].mfaEnabled = true;
res.json({ success: true, message: 'MFA enabled successfully' });
} else {
res.status(401).json({ error: 'Invalid token, MFA not set up for the user' });
}
});
// Endpoint to verify MFA during login
app.post('/verify-mfa', isAuthenticated, (req, res) => {
const { email, mfaToken } = req.body;
if(email != req.session.user.email) return res.status(401).json({ error: 'Unauthorized' } );
// Verify the MFA token
const isTokenValid = mfaAuthy.verifyMFAAuthToken(users[email].mfaSecret, mfaToken);
if (isTokenValid) {
res.json({ success: true, message: 'Logged in successfully with 2FA' });
} else {
res.status(401).json({ error: 'Invalid 2FA token' });
}
});
// Endpoint to disable MFA
app.post('/disable-mfa', isAuthenticated, (req, res) => {
const { email } = req.session.user;
const { password } = req.body;
// Check if the password is correct
if (users[email].password !== password) {
res.status(401).json({ error: 'Invalid password' });
}
// Disable MFA for the user
users[email].mfaEnabled = false;
users[email].mfaSecret = null;
res.json({ success: true, message: 'MFA disabled successfully' });
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
To run the test suite, ensure that you have a testing framework installed (e.g., Jest), and then execute:
npm run test
This project is licensed under the MIT License - see the LICENSE file for details.