chore: prettier cleanup

This commit is contained in:
Savely Savianok
2025-02-26 00:33:40 +03:00
parent cebe95acf5
commit 24e097a253
9 changed files with 318 additions and 296 deletions

View File

@@ -1,162 +1,172 @@
import { getTOTPByLink } from './utils/queryParser.js'
import { getTOTPByLink } from "./utils/queryParser.js";
let _props = null;
AppSettingsPage({
build(props) {
_props = props;
build(props) {
_props = props;
const storage = JSON.parse(props.settingsStorage.getItem("TOTPs") ?? "[]")
const totpEntrys = GetTOTPList(storage)
const createButton = TextInput({
placeholder: "otpauth://",
label: "Add new OTP Link",
onChange: (changes) => {
var link = getTOTPByLink(changes)
if(link == null){
console.log("link is invalid")
return;
}
storage.push(link)
updateStorage(storage)
},
labelStyle: {
backgroundColor: "#14213D",
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
flexGrow: 1,
fontSize: "20px",
color: "#FFFFFF",
borderRadius: "5px"
}
});
var body = Section(
{
style: {
backgroundColor: "black",
minHeight: "100vh",
},
},
[
View(
{
style: {
textAlign: "center",
const storage = JSON.parse(
props.settingsStorage.getItem("TOTPs") ?? "[]"
);
const totpEntrys = GetTOTPList(storage);
const createButton = TextInput({
placeholder: "otpauth://",
label: "Add new OTP Link",
onChange: (changes) => {
var link = getTOTPByLink(changes);
if (link == null) {
console.log("link is invalid");
return;
}
storage.push(link);
updateStorage(storage);
},
},
Text(
labelStyle: {
backgroundColor: "#14213D",
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
flexGrow: 1,
fontSize: "20px",
color: "#FFFFFF",
borderRadius: "5px",
},
});
var body = Section(
{
align: "center",
paragraph: true,
style: {
marginBottom: "10px",
color: "#fff",
fontSize: 23,
verticalAlign: "middle",
},
style: {
backgroundColor: "black",
minHeight: "100vh",
},
},
"TOTPS:"
)
),
...totpEntrys,
createButton
]
);
return body;
},
[
View(
{
style: {
textAlign: "center",
},
},
Text(
{
align: "center",
paragraph: true,
style: {
marginBottom: "10px",
color: "#fff",
fontSize: 23,
verticalAlign: "middle",
},
},
"TOTPS:"
)
),
...totpEntrys,
createButton,
]
);
return body;
},
});
function GetTOTPList(storage){
let totpEntrys = [];
let counter = 0;
storage.forEach((element) => {
const elementId = counter;
const textInput = TextInput({
placeholder: "otpauth://",
label: "Change OTP link",
onChange: (changes) => {
try{
storage[elementId] = getTOTPByLink(changes)
updateStorage(storage)
}
catch(err){
console.log(err)
}
},
labelStyle: {
backgroundColor: "#14213D",
textAlign: "center",
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
flexGrow: 1,
fontSize: "20px",
color: "#E5E5E5",
borderRadius: "5px"
},
function GetTOTPList(storage) {
let totpEntrys = [];
let counter = 0;
storage.forEach((element) => {
const elementId = counter;
const textInput = TextInput({
placeholder: "otpauth://",
label: "Change OTP link",
onChange: (changes) => {
try {
storage[elementId] = getTOTPByLink(changes);
updateStorage(storage);
} catch (err) {
console.log(err);
}
},
labelStyle: {
backgroundColor: "#14213D",
textAlign: "center",
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
flexGrow: 1,
fontSize: "20px",
color: "#E5E5E5",
borderRadius: "5px",
},
});
const textBig = Text(
{
align: "center",
style: {
color: "#ffffff",
fontSize: "16px",
},
paragraph: true,
},
`${element.issuer}: ${element.client}`
);
const delButton = Button({
onClick: () => {
storage = storage.filter(
(x) => storage.indexOf(x) != elementId
);
updateStorage(storage);
},
style: {
backgroundColor: "#ba181b",
fontSize: "18px",
color: "#ffffff",
height: "fit-content",
margin: "10px",
},
label: "DEL",
});
const text = Text(
{
style: {
color: "#ffffff",
fontSize: "14px",
},
align: "center",
},
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | offset ${element.timeOffset} seconds`
);
const view = View(
{
style: {
textAlign: "center",
border: "2px solid white",
borderRadius: "5px",
margin: "10px",
},
},
[
textBig,
text,
View(
{
style: {
display: "grid",
gridTemplateColumns: "1fr 100px",
},
},
[textInput, delButton]
),
]
);
totpEntrys.push({ text: text, view: view });
counter++;
});
const textBig = Text(
{
align: "center",
style: {
color: "#ffffff",
fontSize: "16px"
},
paragraph: true,
},
`${element.issuer}: ${element.client}`
);
const delButton = Button(
{
onClick: () => {
storage = storage.filter(x => storage.indexOf(x) != elementId)
updateStorage(storage)
},
style: {
backgroundColor: "#ba181b",
fontSize: "18px",
color: "#ffffff",
height: "fit-content",
margin: "10px"
},
label: "DEL"
}
);
const text = Text(
{
style: {
color: "#ffffff",
fontSize: "14px"
},
align: "center"
},
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | offset ${element.timeOffset} seconds`
);
const view = View(
{
style: {
textAlign: "center",
border: "2px solid white",
borderRadius: "5px",
margin: "10px"
},
},
[textBig, text, View({style: {
display: "grid",
gridTemplateColumns: "1fr 100px"
}}, [textInput, delButton])]
);
totpEntrys.push({ text: text, view: view });
counter++;
});
return totpEntrys.map(x => x.view);
return totpEntrys.map((x) => x.view);
}
function updateStorage(storage){
_props.settingsStorage.setItem('TOTPs', JSON.stringify(storage))
}
function updateStorage(storage) {
_props.settingsStorage.setItem("TOTPs", JSON.stringify(storage));
}

View File

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