This project provides an open-source Node.js implementation of multi-factor authentication (MFA) in both GraphQL and REST APIs using TypeScript. It includes a variety of common MFA methods such as SMS-based OTP, Authenticator Apps, FIDO2/U2F, push notifications, biometric authentication, backup codes, and email-based authentication.
- SMS-Based Authentication (OTP): Implements one-time password generation and verification via SMS.
- Authenticator Apps: Supports time-based one-time passwords (TOTP) through apps like Google Authenticator and Authy.
- Hardware Security Keys (FIDO2/U2F): Provides hardware-based authentication using security keys like YubiKey.
- Biometric Authentication: Integrates platform-level biometric authentication using Face ID or Touch ID.
- Push Notification-Based MFA: Utilizes push notifications for approval-based authentication.
- Backup Codes: Allows users to generate backup codes in case they lose access to primary MFA methods.
- Email-Based MFA: Sends a one-time password via email for secondary authentication.
- Platform-Specific MFA: GitHub-like MFA via mobile or desktop notifications.
- Installation
- Configuration
- Endpoints
- SMS-Based OTP
- Authenticator Apps
- Hardware Security Keys
- Biometric Authentication
- Push Notification-Based MFA
- Backup Codes
- Email-Based MFA
- GraphQL API
- REST API
- Error Handling
- License
- Node.js v14+ (LTS)
- TypeScript v4+
- PostgreSQL (or any supported database)
- Redis (optional, for caching or sessions)
- Twilio (or another SMS gateway) for SMS-based OTP
- Authy or similar service for authenticator app - functionality
- WebAuthn/FIDO2 libraries for hardware key support
-
Clone the Repository:
git clone https://github.com/your-repo/mfa-implementation-nodejs.git cd mfa-implementation-nodejs -
Install Dependencies:
npm install
-
Setup Environment Variables: Create a .env file at the root of your project with the following:
DATABASE_URL=your_postgresql_database_url TWILIO_ACCOUNT_SID=your_twilio_account_sid TWILIO_AUTH_TOKEN=your_twilio_auth_token TWILIO_PHONE_NUMBER=your_twilio_phone_number AUTHY_API_KEY=your_authy_api_key FIDO2_RP_ID=your_fido2_rp_id FIDO2_RP_NAME=your_fido2_rp_name PUSH_NOTIFICATION_API_KEY=your_push_notification_key EMAIL_SERVICE_API_KEY=your_email_service_key
-
Database Migration:
npx prisma migrate dev
-
Start the Server:
npm run dev
The application relies on the following environment variables:
DATABASE_URL: Connection string for PostgreSQL or any other relational database.TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN,TWILIO_PHONE_NUMBER: Required for sending SMS-based OTPs.AUTHY_API_KEY: For integrating authenticator apps like Authy.FIDO2_RP_IDandFIDO2_RP_NAME: Relying party information for WebAuthn-based hardware security keys.PUSH_NOTIFICATION_API_KEY: Used for sending push notifications.EMAIL_SERVICE_API_KEY: Email service credentials to send OTPs for email-based MFA.
- Endpoint:
/api/v1/mfa/sms/send - Method: POST
- Payload:
{
"phone": "+1234567890"
}- Response:
{
"message": "OTP sent successfully"
}- Endpoint:
/api/v1/mfa/sms/verify - Method: POST
- Payload:
{
"phone": "+1234567890",
"otp": "123456"
}- Response:
{
"message": "OTP verified successfully"
}- Endpoint:
/api/v1/mfa/authenticator/generate - Method: GET
- Response:
{
"secret": "YOUR_SECRET",
"qrCode": "BASE64_ENCODED_QR_CODE"
}- Endpoint:
/api/v1/mfa/authenticator/verify - Method: POST
- Payload:
{
"token": "123456"
}- Response:
{
"message": "Authenticator token verified successfully"
}-
Endpoint:
/api/v1/mfa/webauthn/register -
Method: POST
-
Payload:
{
"userId": "your_user_id"
}- Response:
{
"publicKeyCredentialOptions": { /* FIDO2 options for registration */ }
}- Endpoint:
/api/v1/mfa/webauthn/verify - __Method: POST
- Payload:
{
"userId": "your_user_id",
"authenticatorResponse": {/* WebAuthn response */}
}- Mutation:
mutation BiometricLogin($input: BiometricLoginInput!) {
biometricLogin(input: $input) {
token
user {
id
email
}
}
}- Payload:
{
"input": {
"fingerprint": "base64_fingerprint_data"
}
}- Endpoint:
/api/v1/mfa/push - Method: POST
- Payload:
{
"userId": "your_user_id"
}- Response:
{
"message": "Push notification sent"
}-
Endpoint:
/api/v1/mfa/backup/generate -
Method: GET
-
Response:
{
"backupCodes": ["code1", "code2", "code3"]
}-
Endpoint:
/api/v1/mfa/backup/verify -
Method: POST
-
Payload:
{
"code": "backup_code"
}- Response:
{
"message": "Backup code verified successfully"
}-
Endpoint:
/api/v1/mfa/email/send -
Method: POST
-
Payload:
{
"email": "user@example.com"
}- Response:
{
"message": "Email OTP sent successfully"
}- Endpoint:
/api/v1/mfa/email/verify - Method: POST
- Payload:
{
"email": "user@example.com",
"otp": "123456"
}- Response:
{
"message": "Email OTP verified successfully"
}This project supports GraphQL for seamless integration.
mutation SendOTP($input: SendOTPInput!) {
sendOTP(input: $input) {
success
message
}
}mutation VerifyOTP($input: VerifyOTPInput!) {
verifyOTP(input: $input) {
success
message
}
}This project also supports a fully functional RESTful API for easier integration into various services.
All requests return standardized error codes and messages to ensure consistent handling on the client side.
This project is licensed under the MIT License.