UNPKG

@kiryano/etherealotp

Version:

A library for two-factor authentication using Twilio and Speakeasy.

83 lines (82 loc) 3.59 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TwoFactorAuth = void 0; const twilio_1 = __importDefault(require("twilio")); const TwilioError_1 = require("./errors/TwilioError"); const TokenUtils_1 = require("./utils/TokenUtils"); class TwoFactorAuth { constructor(config) { // Validate the config to ensure necessary details are provided if (!config.accountSid || !config.authToken || !config.fromNumber) { throw new Error('Invalid Twilio configuration'); } this.client = (0, twilio_1.default)(config.accountSid, config.authToken); this.fromNumber = config.fromNumber; } /** * Generates a new TOTP secret. * @returns The generated secret. */ generateSecret() { return (0, TokenUtils_1.generateSecret)(); } /** * Generates a TOTP token based on a user's secret. * @param secret The user's TOTP secret. * @returns The generated token. */ generateToken(secret) { return (0, TokenUtils_1.generateToken)(secret); } /** * Verifies a user-provided TOTP token against their secret. * @param token The TOTP token to verify. * @param secret The user's TOTP secret. * @returns True if the token is valid, otherwise false. */ verifyToken(token, secret) { return (0, TokenUtils_1.verifyToken)(token, secret); } /** * Sends an SMS message containing the TOTP token to a user's phone number. * @param phoneNumber The destination phone number, including country code. * @param message The message to send, typically containing the TOTP token. * @returns A promise resolved with the message sid if sending succeeds. */ sendSmsOTP(phoneNumber, message) { return __awaiter(this, void 0, void 0, function* () { try { const messageInstance = yield this.client.messages.create({ body: message, to: phoneNumber, from: this.fromNumber }); console.log(`Message sent successfully to ${phoneNumber}. SID: ${messageInstance.sid}`); return messageInstance.sid; } catch (error) { if (error instanceof Error) { console.error(`Failed to send SMS: ${error.message}`); throw new TwilioError_1.TwilioError(error.message); } else { console.error('An unexpected error occurred:', error); throw new TwilioError_1.TwilioError('An unexpected error occurred in sending SMS'); } } }); } } exports.TwoFactorAuth = TwoFactorAuth;