TOTPFit/setting/utils/queryParser.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

import { TOTP } from "../../lib/totp-quickjs";
const otpScheme = "otpauth:/";
export function getTOTPByLink(link) {
2025-02-26 00:33:40 +03:00
try {
let args = link.split("/", otpScheme.length);
let type = args[2]; //Returns 'hotp' or 'totp'
let issuer = args[3].split(":")[0]?.split("?")[0]; //Returns issuer
let client =
args[3].split(":")[1]?.split("?")[0] ??
args[3].split(":")[0]?.split("?")[0]; //Returns client
let secret = args[3].split("secret=")[1]?.split("&")[0]; //Returns secret
let period = args[3].split("period=")[1]?.split("&")[0]; //Returns period
let digits = args[3].split("digits=")[1]?.split("&")[0]; //Returns digits
let algorithm = args[3].split("algorithm=")[1]?.split("&")[0]; //Returns algorithm
2025-02-26 00:33:40 +03:00
if (type.toLowerCase() != "totp")
throw new Error("Type is not valid, requires 'TOTP'");
2025-02-26 00:33:40 +03:00
if (secret === undefined) throw new Error("Secret not defined");
2025-02-26 02:28:30 +03:00
if(issuer == client){
issuer = args[3].split("issuer=")[1]?.split("&")[0]
}
issuer = decodeURIComponent(issuer);
client = decodeURIComponent(client);
2025-02-26 02:36:43 +03:00
2025-02-26 00:33:40 +03:00
return new TOTP(
secret,
issuer,
client,
digits,
period,
0,
getHashType(algorithm)
);
} catch (err) {
console.log(err)
2025-02-26 00:33:40 +03:00
return null;
}
}
function getHashType(algorithm) {
2025-02-26 00:33:40 +03:00
if (algorithm == "SHA1") return "SHA-1";
if (algorithm == "SHA256") return "SHA-256";
if (algorithm == "SHA512") return "SHA-512";
else return "SHA-1";
}