fix: small fixes of totp addition, fix of protocol missing crash

This commit is contained in:
Pavel-Savely Savianok 2025-01-13 02:35:18 +03:00
parent 304a253e86
commit 589a9cb527

View File

@ -2,39 +2,44 @@ import { TOTP } from "../../lib/totp-quickjs";
const otpScheme = "otpauth:/"; const otpScheme = "otpauth:/";
export function getTOTPByLink(link){ export function getTOTPByLink(link) {
try{ try {
let args = link.split("/", otpScheme.length) let args = link.split("/", otpScheme.length);
let type = args[2] //Returns 'hotp' or 'totp' let type = args[2]; //Returns 'hotp' or 'totp'
let issuer = args[3].split(':')[0]?.split('?')[0] //Returns issuer 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 client =
let secret = args[3].split('secret=')[1]?.split('&')[0] //Returns secret args[3].split(":")[1]?.split("?")[0] ??
let period = args[3].split('period=')[1]?.split('&')[0] //Returns period args[3].split(":")[0]?.split("?")[0]; //Returns client
let digits = args[3].split('digits=')[1]?.split('&')[0] //Returns digits let secret = args[3].split("secret=")[1]?.split("&")[0]; //Returns secret
let algorithm = args[3].split('algorithm=')[1]?.split('&')[0] //Returns algorithm let period = args[3].split("period=")[1]?.split("&")[0]; //Returns period
} let digits = args[3].split("digits=")[1]?.split("&")[0]; //Returns digits
catch(err){ let algorithm = args[3].split("algorithm=")[1]?.split("&")[0]; //Returns algorithm
throw new Error("Link is not valid")
}
if(type.toLowerCase() != 'totp')
throw new Error("Type is not valid, requires 'TOTP'")
if(secret === undefined) if (type.toLowerCase() != "totp")
throw new Error("Secret not defined") throw new Error("Type is not valid, requires 'TOTP'");
issuer = issuer.replace("%20", " ") if (secret === undefined) throw new Error("Secret not defined");
client = client.replace("%20", " ")
return new TOTP(secret, issuer, client, digits, period, 0, getHashType(algorithm)) issuer = issuer.replace("%20", " ");
client = client.replace("%20", " ");
return new TOTP(
secret,
issuer,
client,
digits,
period,
0,
getHashType(algorithm)
);
} catch (err) {
throw new Error("Link is not valid");
}
} }
function getHashType(algorithm){ function getHashType(algorithm) {
if(algorithm == "SHA1") if (algorithm == "SHA1") return "SHA-1";
return "SHA-1" if (algorithm == "SHA256") return "SHA-256";
if(algorithm == "SHA256") if (algorithm == "SHA512") return "SHA-512";
return "SHA-256" else return "SHA-1";
if(algorithm == "SHA512")
return "SHA-512"
else
return null
} }