mfa-authy
v1.0.6
Published
A TypeScript library for implementing Multi-Factor Authentication (MFA). This library provides essential features like generating MFA authentication keys, tokens, verifying tokens, and creating Time-based One-Time Password (TOTP) URIs for use with popular
Downloads
164
Maintainers
Readme
MFA-AUTHY
A TypeScript library for implementing Multi-Factor Authentication (MFA). This library provides essential features like generating MFA authentication keys, tokens, verifying tokens, and creating Time-based One-Time Password (TOTP) URIs for use with popular authenticator apps.
Key Benefits
- Enhanced Security: Strengthens authentication by reducing the risk of unauthorized access even if passwords are compromised.
- Ease of Integration: Includes prebuilt, reliable functions for generating keys, tokens, and TOTP URIs, reducing development effort.
- Wide Compatibility: Works seamlessly with popular apps like Google Authenticator, Authy, and Microsoft Authenticator.
- Customizable Options: Adjust settings like token validity duration, allowed clock drift, and encryption methods.
- Compliance Support: Helps meet security standards like PCI-DSS, HIPAA, and GDPR.
Supported Authenticator Apps
This library is compatible with popular authentication apps, including:
- Google Authenticator
- Authy
- Microsoft Authenticator among others.
Installation
Install the package using npm:
npm install mfa-authy
Usage
Importing the library
import {
generateMFAAuthKey,
verifyMFAAuthToken,
generateTotpUri,
} from 'mfa-authy';
Generate Secret Key
const generatedKey = generateMFAAuthKey();
Verify Token
const isTokenValid = verifyMFAAuthToken(generatedKey, generatedToken);
Generating URI (barcode/QR code) for Authenticator Apps
This function generates a base64-encoded URI that can be scanned by authenticator apps like Google Authenticator or Authy. It now supports asynchronous functionality for better integration:
const totpUri = await generateTotpUri(generatedKey, '[email protected]', 'MyAuthenticatorApp', 'SHA256', 8, 60);
Example base64 response:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAADkCAYAAACIV4iNAAAAAklEQVR4AewaftIAAAxKSURBVO3BQW4ER5IAQfcC//9lXx3jlEChm1RqNszsH6y1rvCw1rrGw1rrGg9rrWs8rLWu8bDWusbDWusaD2utazysta7xsNa6xsNa6xoPa61rPKy1rvGw1rrGw1rrGg9rrWv88CGVv1QxqfymihOVqeINlaniDZWp4kTlpGJS+U0Vk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6qYVKaKSWWq+MTDWusaD2utazysta7xw4cqflPFiconKj6h8kbFJyreUDlROamYVKaKSWVS+UTFGxVvqLyh8pse1lrXeFhrXeNhrXWNHz6k8pcqTip+k8pJxYnKVDGpvKEyVZyonFRMKm9UTConFZPKpDJVvKEyVZyoTBWTym96WGtd42GtdY2HtdY1fviyim9SeUNlqphUTireqPiEylQxqZxUvFExqUwq31RxojJVfFPFN1X8poe11jUe1lrXeFhrXeOHX6byRsUbFZPKGxUnKlPFpDJVTConFZPKiconVE4qJpWp4g2Vk4pJ5RMq36RyUvFND2utazysta7xsNa6xg//cSpTxaQyVbxRcVLxCZWp4g2VqWJSmSo+oTJVTConFZPKVDGpfKJiUjmpOFH5TQ9rrWs8rLWu8bDWusYP/8+onFRMKlPFpHJS8ZsqJpWp4kRlqnhD5RMVk8pU8QmV/5KHtdY1HtZa13hYa13jh19W8ZsqJpWpYlJ5o2JSmSpOVKaKSeWk4kTlDZWp4o2KT6h8k8pUMVV8QmWq+E0Pa61rPKy1rvGw1rrGD1+m8pdUTlROKk5UTlTeULmZyhsqU8VJxaRyojJVTBWTyknFJ1ROKj7xsNa6xsNa6xoPa61r2D9Ya13hYa11jYe11jUe1lrXeFhrXeNhrXWNh7XWNR7WWtd4WGtd42GtdY2HtdY1HtZa13hYa13jYa11jYe11jUe1lrX+D9NKIN6tFQSVAAAAABJRU5ErkJggg==
Display the TOTP URI as a QR Code in HTML
To render the QR code in a web page:
<img alt="QR-Code" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOQAAADkCAYAAACIV4iNAAAAAklEQVR4AewaftIAAAxKSURBVO3BQW4ER5IAQfcC//9lXx3jlEChm1RqNszsH6y1rvCw1rrGw1rrGg9rrWs8rLWu8bDWusbDWusaD2utazysta7xsNa6xsNa6xoPa61rPKy1rvGw1rrGw1rrGg9rrWv88CGVv1QxqfymihOVqeINlaniDZWp4kTlpGJS+U0Vk8pUcaIyVUwqf6niEw9rrWs8rLWu8bDWusYPX1bxTSp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kbFJyreUDlROamYVKaKSWVS+UTFGxVvqLyh8pse1lrXeFhrXeNhrXWNHz6k8pcqTip+k8pJxYnKVDGpvKEyVZyonFRMKm9UTConFZPKpDJVvKEyVZyoTBWTym96WGtd42GtdY2HtdY1fviyim9SeUNlqphUTireqPiEylQxqZxUvFExqUwq31RxojJVfFPFN1X8poe11jUe1lrXeFhrXeOHX6byRsUbFZPKGxUnKlPFpDJVTConFZPKiconVE4qJpWp4g2Vk4pJ5RMq36RyUvFND2utazysta7xsNa6xg//cSpTxaQyVbxRcVLxCZWp4g2VqWJSmSo+oTJVTConFZPKVDGpfKJiUjmpOFH5TQ9rrWs8rLWu8bDWusYP/8+onFRMKlPFpHJS8ZsqJpWp4kRlqnhD5RMVk8pU8QmV/5KHtdY1HtZa13hYa13jh19W8ZsqJpWpYlJ5o2JSmSpOVKaKSeWk4kTlDZWp4o2KT6h8k8pUMVV8QmWq+E0Pa61rPKy1rvGw1rrGD1+m8pdUTlROKk5UTlTeULmZyhsqU8VJxaRyojJVTBWTyknFJ1ROKj7xsNa6xsNa6xoPa61r2D9Ya13hYa11jYe11jUe1lrXeFhrXeNhrXWNh7XWNR7WWtd4WGtd42GtdY2HtdY1HtZa13hYa13jYa11jYe11jUe1lrX+D9NKIN6tFQSVAAAAABJRU5ErkJggg=="/>
Features
- Key Generation: Securely generate unique authentication keys.
- Token Verification: Validate tokens generated by authenticator apps.
- TOTP URI Creation: Generate QR codes for easy integration with apps.
- Customizable Parameters: Specify token length, hashing algorithms, and expiration duration.
Example App
Check the textExample.txt file
Running Tests
To run the test suite, ensure that you have a testing framework installed (e.g., Jest), and then execute:
npm run test
License
This project is licensed under the MIT License - see the LICENSE file for details.