From 672cb5dea4fa60c965fc0d294b19e0e5c8586a4b Mon Sep 17 00:00:00 2001 From: exactlyaron Date: Thu, 23 Jan 2025 11:28:04 +0000 Subject: [PATCH] fix: refactor GenerateTOTP to use optauth --- src/core/operations/GenerateTOTP.mjs | 43 +++++++++++----------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/core/operations/GenerateTOTP.mjs b/src/core/operations/GenerateTOTP.mjs index 48886e05..187f8418 100644 --- a/src/core/operations/GenerateTOTP.mjs +++ b/src/core/operations/GenerateTOTP.mjs @@ -5,15 +5,14 @@ */ import Operation from "../Operation.mjs"; -import otp from "otp"; +import * as OTPAuth from "otpauth"; /** * Generate TOTP operation */ class GenerateTOTP extends Operation { - /** - * GenerateTOTP constructor + * */ constructor() { super(); @@ -29,11 +28,6 @@ class GenerateTOTP extends Operation { "type": "string", "value": "" }, - { - "name": "Key size", - "type": "number", - "value": 32 - }, { "name": "Code length", "type": "number", @@ -53,32 +47,27 @@ class GenerateTOTP extends Operation { } /** - * @param {ArrayBuffer} input - * @param {Object[]} args - * @returns {string} + * */ run(input, args) { const secretStr = new TextDecoder("utf-8").decode(input).trim(); + const secret = secretStr ? secretStr.toUpperCase().replace(/\s+/g, "") : ""; - // uppercase it and remove spaces. If input is empty, let otp generate a random key. - const secret = secretStr ? - secretStr.toUpperCase().replace(/\s+/g, "") : // e.g. "ok53 fvlf" -> "OK53FVLF" - ""; - - // Construct the OTP object. If secret is "", otp will auto-generate one. - const otpObj = new otp({ - name: args[0], - keySize: args[1], - codeLength: args[2], - secret: secret, - epoch: args[3], - timeSlice: args[4] + const totp = new OTPAuth.TOTP({ + issuer: "", + label: args[0], + algorithm: "SHA1", + digits: args[1], + period: args[3], + epoch: args[2] * 1000, // Convert seconds to milliseconds + secret: OTPAuth.Secret.fromBase32(secret) }); - // Return TOTP URI and code - return `URI: ${otpObj.totpURL}\n\nPassword: ${otpObj.totp()}`; - } + const uri = totp.toString(); + const code = totp.generate(); + return `URI: ${uri}\n\nPassword: ${code}`; + } } export default GenerateTOTP;