@alexstrnik/multifactor-cli
Version:
CLI tool for bypassing multifactor authentication
56 lines (55 loc) • 1.75 kB
JavaScript
import { AsyncStorage, tcp } from "@aracna/core";
import { lstat, readFile, writeFile } from "fs/promises";
import { deserialize, serialize } from "v8";
import { join } from "path";
import { homedir } from "os";
const PATH = join(homedir(), ".multifactor-cli");
export const DiskStorage = new AsyncStorage("DiskStorage", async () => writeFile(PATH, serialize({})), async (key) => {
let path, stat, file, json, item;
path = PATH;
stat = await tcp(() => lstat(path), false);
if (stat instanceof Error) {
await writeFile(path, serialize({}));
}
file = await readFile(path);
json = deserialize(file);
item = json[key];
if (item) {
return item;
}
return new Error(`The item does not exist.`);
}, async (key) => {
let path, stat, file, json;
path = PATH;
stat = await tcp(() => lstat(path), false);
if (stat instanceof Error) {
await writeFile(path, serialize({}));
}
file = await readFile(path);
json = deserialize(file);
return Boolean(json[key]);
}, async (key) => {
let path, stat, file, json;
path = PATH;
stat = await tcp(() => lstat(path), false);
if (stat instanceof Error) {
await writeFile(path, serialize({}));
}
file = await readFile(path);
json = deserialize(file);
if (json[key]) {
delete json[key];
}
return writeFile(path, serialize(json));
}, async (key, item) => {
let path, stat, file, json;
path = PATH;
stat = await tcp(() => lstat(path), false);
if (stat instanceof Error) {
await writeFile(path, serialize({}));
}
file = await readFile(path);
json = deserialize(file);
json[key] = item;
return writeFile(path, serialize(json));
});