chore: prettier cleanup

This commit is contained in:
2025-07-24 14:39:02 +03:00
parent 3e037da6da
commit 69dddcebb5
20 changed files with 488 additions and 484 deletions

View File

@@ -9,30 +9,33 @@ const colors = {
text: "#fafafa",
alert: "#ad3c23",
notify: "#555555",
bigText: "#fafafa"
bigText: "#fafafa",
};
AppSettingsPage({
build(props) {
_props = props;
const storage = JSON.parse(
props.settingsStorage.getItem("TOTPs") ?? "[]"
props.settingsStorage.getItem("TOTPs") ?? "[]",
);
const totpEntrys = GetTOTPList(storage);
const addTOTPsHint = storage.length < 1 ?
Text({
paragraph: true,
align: "center",
style: {
paddingTop: "10px",
marginBottom: "10px",
color: colors.text,
fontSize: 16,
verticalAlign: "middle",
},
},
"For add a 2FA TOTP record you must have otpauth:// link or otpauth-migration:// link from Google Authenticator Migration QR-Code"
) : null;
const addTOTPsHint =
storage.length < 1
? Text(
{
paragraph: true,
align: "center",
style: {
paddingTop: "10px",
marginBottom: "10px",
color: colors.text,
fontSize: 16,
verticalAlign: "middle",
},
},
"For add a 2FA TOTP record you must have otpauth:// link or otpauth-migration:// link from Google Authenticator Migration QR-Code",
)
: null;
const createButton = TextInput({
placeholder: "otpauth(-migration)://",
label: "Add new TOTP record",
@@ -43,8 +46,7 @@ AppSettingsPage({
return;
}
if (Array.isArray(link))
storage.push(...link);
if (Array.isArray(link)) storage.push(...link);
else storage.push(link);
updateStorage(storage);
@@ -61,7 +63,7 @@ AppSettingsPage({
position: storage.length < 1 ? "absolute" : null, //TODO: Сделать что-то с этим кошмаром
bottom: storage.length < 1 ? "0px" : null,
left: storage.length < 1 ? "0px" : null,
right: storage.length < 1 ? "0px" : null
right: storage.length < 1 ? "0px" : null,
},
});
@@ -79,35 +81,40 @@ AppSettingsPage({
textAlign: "center",
},
},
storage.length < 1 ? addTOTPsHint : Text(
{
align: "center",
paragraph: true,
style: {
marginBottom: "10px",
color: colors.bigText,
fontSize: 23,
fontWeight: "500",
verticalAlign: "middle",
},
},
"TOTP records:"
)
storage.length < 1
? addTOTPsHint
: Text(
{
align: "center",
paragraph: true,
style: {
marginBottom: "10px",
color: colors.bigText,
fontSize: 23,
fontWeight: "500",
verticalAlign: "middle",
},
},
"TOTP records:",
),
),
...totpEntrys,
createButton,
View({
style: {
display: "flex",
justifyContent: "center"
}
},
Link({
source: "https://github.com/Lisoveliy/totpfit/blob/main/docs/guides/how-to-add-totps/README.md"
View(
{
style: {
display: "flex",
justifyContent: "center",
},
},
"Instruction | Report issue (GitHub)")
Link(
{
source: "https://github.com/Lisoveliy/totpfit/blob/main/docs/guides/how-to-add-totps/README.md",
},
"Instruction | Report issue (GitHub)",
),
),
]
],
);
return body;
},
@@ -124,8 +131,7 @@ function GetTOTPList(storage) {
onChange: (changes) => {
try {
let link = getTOTPByLink(changes);
if (Array.isArray(link))
return;
if (Array.isArray(link)) return;
storage[elementId] = link;
updateStorage(storage);
@@ -152,16 +158,16 @@ function GetTOTPList(storage) {
style: {
color: colors.text,
fontSize: "18px",
fontWeight: "500"
fontWeight: "500",
},
paragraph: true,
},
`${element.issuer}: ${element.client}`
`${element.issuer}: ${element.client}`,
);
const delButton = Button({
onClick: () => {
storage = storage.filter(
(x) => storage.indexOf(x) != elementId
(x) => storage.indexOf(x) != elementId,
);
updateStorage(storage);
},
@@ -182,7 +188,7 @@ function GetTOTPList(storage) {
},
align: "center",
},
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | ${element.timeOffset} sec offset`
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | ${element.timeOffset} sec offset`,
);
const view = View(
{
@@ -204,9 +210,9 @@ function GetTOTPList(storage) {
gridTemplateColumns: "1fr 100px",
},
},
[textInput, delButton]
[textInput, delButton],
),
]
],
);
totpEntrys.push({ text: text, view: view });
counter++;

View File

@@ -6,109 +6,100 @@ const otpauthScheme = "otpauth:/";
const googleMigrationScheme = "otpauth-migration:/";
export function getTOTPByLink(link) {
if (link.includes(otpauthScheme))
return getByOtpauthScheme(link)
if (link.includes(googleMigrationScheme))
return getByGoogleMigrationScheme(link)
if (link.includes(otpauthScheme)) return getByOtpauthScheme(link);
if (link.includes(googleMigrationScheme))
return getByGoogleMigrationScheme(link);
return null;
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";
}
function getByOtpauthScheme(link) {
try {
let args = link.split("/", otpauthScheme.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("digit=")[1]?.split("&")[0]; //Returns digits
let algorithm = args[3].split("algorithm=")[1]?.split("&")[0]; //Returns algorithm
let offset = args[3].split("offset=")[1]?.split("&")[0] ?? 0; //Returns offset
try {
let args = link.split("/", otpauthScheme.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("digit=")[1]?.split("&")[0]; //Returns digits
let algorithm = args[3].split("algorithm=")[1]?.split("&")[0]; //Returns algorithm
let offset = args[3].split("offset=")[1]?.split("&")[0] ?? 0; //Returns offset
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");
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);
issuer = decodeURIComponent(issuer);
client = decodeURIComponent(client);
return new TOTP(
secret,
issuer,
client,
Number(digits),
Number(period),
Number(offset),
getHashType(algorithm)
);
} catch (err) {
console.log(err)
return null;
}
return new TOTP(
secret,
issuer,
client,
Number(digits),
Number(period),
Number(offset),
getHashType(algorithm),
);
} catch (err) {
console.log(err);
return null;
}
}
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 protoTotps = [];
proto.parts.forEach((part) => {
if (part.type == TYPES.LENDELIM) {
protoTotps.push(decodeProto(part.value));
}
});
proto.parts.forEach(part => {
if (part.type == TYPES.LENDELIM) {
protoTotps.push(decodeProto(part.value));
}
});
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);
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);
let name = bytesToString(x.parts.filter((x) => x.index == 2)[0].value);
let issuer = bytesToString(
x.parts.filter((x) => x.index == 3)[0].value,
);
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"
));
});
return totps;
totps.push(new TOTP(secret, issuer, name, 6, 30, 0, "SHA-1"));
});
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;
}