From 8c92239aa404487e37e6faa053b35b0d60755807 Mon Sep 17 00:00:00 2001 From: Lisoveliy Date: Wed, 23 Jul 2025 21:41:50 +0300 Subject: [PATCH] chore: experiments with proto --- setting/utils/queryParser.js | 101 +++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/setting/utils/queryParser.js b/setting/utils/queryParser.js index aca7871..a7b0b9c 100644 --- a/setting/utils/queryParser.js +++ b/setting/utils/queryParser.js @@ -6,9 +6,9 @@ const otpauthScheme = "otpauth:/"; const googleMigrationScheme = "otpauth-migration:/"; export function getTOTPByLink(link) { - if(link.includes(otpauthScheme)) + if (link.includes(otpauthScheme)) return getByOtpauthScheme(link) - if(link.includes(googleMigrationScheme)) + if (link.includes(googleMigrationScheme)) return getByGoogleMigrationScheme(link) return null; @@ -21,7 +21,7 @@ function getHashType(algorithm) { else return "SHA-1"; } -function getByOtpauthScheme(link){ +function getByOtpauthScheme(link) { try { let args = link.split("/", otpauthScheme.length); let type = args[2]; //Returns 'hotp' or 'totp' @@ -40,9 +40,9 @@ function getByOtpauthScheme(link){ if (secret === undefined) throw new Error("Secret not defined"); - if(issuer == client){ - issuer = args[3].split("issuer=")[1]?.split("&")[0]; - } + if (issuer == client) { + issuer = args[3].split("issuer=")[1]?.split("&")[0]; + } issuer = decodeURIComponent(issuer); client = decodeURIComponent(client); @@ -57,58 +57,79 @@ function getByOtpauthScheme(link){ getHashType(algorithm) ); } catch (err) { - console.log(err) + console.log(err) return null; } } -function getByGoogleMigrationScheme(link){ +function getByGoogleMigrationScheme(link) { - let data = link.split("data=")[1]; //Returns base64 encoded data - data = decodeURIComponent(data); - let decode = base64decode(data); - let proto = decodeProto(decode); + let data = link.split("data=")[1]; //Returns base64 encoded data + data = decodeURIComponent(data); + let decode = base64decode(data); + let proto = decodeProto(decode); - let protoTotps = []; + let totps = []; - proto.parts.forEach(part => { - if(part.type == TYPES.LENDELIM){ - protoTotps.push(decodeProto(part.value)); - } - }); + totps.push(new TOTP( + "", + data, + decode, + 6, + 30, + 0, + "SHA-1" + )); - let totps = []; - protoTotps.forEach(x => { - let type = x.parts.filter(x => x.index == 6)[0]; //find type of OTP - if(type.value !== '2'){ - console.log("ERR: it's a not TOTP record") - return; - } - let secret = x.parts.filter(x => x.index == 1)[0].value; - secret = encode(secret); + totps.push(new TOTP( + "", + proto.leftOver, + proto.parts.length, + 6, + 30, + 0, + "SHA-1" + )); - let name = bytesToString(x.parts.filter(x => x.index == 2)[0].value); - let issuer = bytesToString(x.parts.filter(x => x.index == 3)[0].value); - - totps.push(new TOTP( + let protoTotps = []; + + proto.parts.forEach(part => { + if (part.type == TYPES.LENDELIM) { + protoTotps.push(decodeProto(part.value)); + } + }); + + protoTotps.forEach(x => { + let type = x.parts.filter(x => x.index == 6)[0]; //find type of OTP + if (type.value !== '2') { + console.log("ERR: it's a not TOTP record") + return; + } + let secret = x.parts.filter(x => x.index == 1)[0].value; + secret = encode(secret); + + let name = bytesToString(x.parts.filter(x => x.index == 2)[0].value); + let issuer = bytesToString(x.parts.filter(x => x.index == 3)[0].value); + + totps.push(new TOTP( secret, issuer, name, 6, 30, 0, - "SHA-1" + "SHA-1" )); - }); + }); + + return totps; - return totps; - } function bytesToString(bytes) { - let str = ''; - for (let i = 0; i < bytes.length; i++) { - str += String.fromCharCode(bytes[i]); - } - return str; + let str = ''; + for (let i = 0; i < bytes.length; i++) { + str += String.fromCharCode(bytes[i]); + } + return str; } \ No newline at end of file