allow cookie on stdin

This commit is contained in:
Lizzy Hunt 2023-10-11 11:06:35 -06:00
parent 5a7f1ac4c9
commit 7331da69aa
No known key found for this signature in database
GPG Key ID: E835BD4B08CCAF96
5 changed files with 37 additions and 19 deletions

2
cli.js
View File

@ -1,4 +1,4 @@
#!/usr/bin/env node #!/usr/bin/env node
import main from './src/main.js'; import main from "./src/main.js";
main(); main();

View File

@ -43,11 +43,11 @@ export const get_user_info = async () => {
.toJSON() .toJSON()
.cookies.find( .cookies.find(
({ domain, key }) => ({ domain, key }) =>
domain === AGGIETIME_DOMAIN && key === "XSRF-TOKEN" domain === AGGIETIME_DOMAIN && key === "XSRF-TOKEN",
).value; ).value;
expireCache.set("aggietime-csrf", csrf_token); expireCache.set("aggietime-csrf", csrf_token);
return data; return data;
}) }),
); );
expireCache.set("user", user, USER_CACHE_EXP_SEC); expireCache.set("user", user, USER_CACHE_EXP_SEC);
@ -68,7 +68,7 @@ const do_clock_mutation = async (path, { position_id } = {}) => {
headers: { headers: {
"X-XSRF-TOKEN": expireCache.get("aggietime-csrf"), "X-XSRF-TOKEN": expireCache.get("aggietime-csrf"),
}, },
} },
) )
.then(({ data }) => { .then(({ data }) => {
expireCache.remove("status_line"); expireCache.remove("status_line");
@ -109,14 +109,14 @@ export const get_status_line = async () => {
const start = new Date(shift?.start); const start = new Date(shift?.start);
status_line = status_line =
((new Date().getTime() - start.getTime()) / (1000 * 60 * 60)).toFixed( ((new Date().getTime() - start.getTime()) / (1000 * 60 * 60)).toFixed(
2 2,
) + " hours"; ) + " hours";
} }
expireCache.set( expireCache.set(
"status_line", "status_line",
`${anumber} - ${status_line}`, `${anumber} - ${status_line}`,
OPEN_SHIFT_EXP_SEC OPEN_SHIFT_EXP_SEC,
); );
} }
return { status: expireCache.get("status_line") }; return { status: expireCache.get("status_line") };
@ -143,7 +143,7 @@ export const last_week = async ({ position_id }) => {
expireCache.set( expireCache.set(
"past_week", "past_week",
`${anumber} - ${hours} hours`, `${anumber} - ${hours} hours`,
PAST_WEEK_EXP_SEC PAST_WEEK_EXP_SEC,
); );
} }
return { status: expireCache.get("past_week") }; return { status: expireCache.get("past_week") };

View File

@ -11,12 +11,12 @@ export const with_exponential_retry = async (
promise_fn, promise_fn,
validation_fn = (x) => Promise.resolve(!!x), validation_fn = (x) => Promise.resolve(!!x),
max_retries = MAX_DEFAULT_RETRY_AMOUNT, max_retries = MAX_DEFAULT_RETRY_AMOUNT,
retries = 0 retries = 0,
) => { ) => {
try { try {
if (retries) if (retries)
await wait_for( await wait_for(
WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries) WAIT_MS * Math.pow(RETRY_EXPONENT, RETRY_EXPONENTIAL_FACTOR * retries),
); );
const res = await promise_fn(); const res = await promise_fn();
@ -29,7 +29,7 @@ export const with_exponential_retry = async (
promise_fn, promise_fn,
validation_fn, validation_fn,
max_retries, max_retries,
retries + 1 retries + 1,
); );
} }
}; };

View File

@ -55,6 +55,12 @@ const run_action = (args) => {
const build_args = () => { const build_args = () => {
const parser = new argparse.ArgumentParser({ description: "AggieTime CLI" }); const parser = new argparse.ArgumentParser({ description: "AggieTime CLI" });
parser.add_argument("-cos", "--cookie-on-stdin", {
help: "Set a cookie from whatever is on stdin",
action: argparse.BooleanOptionalAction,
default: false,
});
parser.add_argument("-d", "--daemon", { parser.add_argument("-d", "--daemon", {
help: "Start server as a process blocking daemon", help: "Start server as a process blocking daemon",
action: argparse.BooleanOptionalAction, action: argparse.BooleanOptionalAction,
@ -92,19 +98,28 @@ const kill_server = (server, socket_path) => {
} }
}; };
const start_server = async ({ socket_path, pass_cmd }, on_exit = () => {}) => { const start_server = async (
{ cookie_on_stdin, socket_path, pass_cmd },
on_exit = () => {},
) => {
if (fs.existsSync(socket_path)) { if (fs.existsSync(socket_path)) {
console.error( console.error(
`ERR: Socket '${socket_path}' already exists. `ERR: Socket '${socket_path}' already exists.
If no server process is running, remove it (this should've been done automatically, except in the event of a catastrophic failure) If no server process is running, remove it (this should've been done automatically, except in the event of a catastrophic failure)
OR OR
specify another socket path with --socket_path` specify another socket path with --socket_path`,
); );
process.exit(1); process.exit(1);
} }
if (!cookie_on_stdin) {
const { anumber, password } = await retrieve_creds(pass_cmd); const { anumber, password } = await retrieve_creds(pass_cmd);
await session.login(anumber, password); await session.login(anumber, password);
} else {
let cookie = "";
for await (const chunk of process.stdin) cookie += chunk;
await session.setCookie(cookie);
}
session.refresh_jwt(); session.refresh_jwt();
setInterval(session.refresh_jwt, REFRESH_JWT_MS); setInterval(session.refresh_jwt, REFRESH_JWT_MS);
@ -140,6 +155,6 @@ specify another socket path with --socket_path`
// Attempt to clean up socket before process gets killed // Attempt to clean up socket before process gets killed
KILL_SIGNALS.forEach((signal) => KILL_SIGNALS.forEach((signal) =>
process.on(signal, () => kill_server(unix_server, socket_path)) process.on(signal, () => kill_server(unix_server, socket_path)),
); );
}; };

View File

@ -21,6 +21,9 @@ export const refresh_jwt = () => {
return aggietime.get_user_info(); return aggietime.get_user_info();
}; };
export const setCookie = (jwt) =>
jar.setCookie(`${AGGIETIME_AUTH_COOKIE_NAME}=${jwt}`, AGGIETIME_URI);
export const logout = () => client.get(`${AGGIETIME_URI}/${LOGOUT_PATH}`); export const logout = () => client.get(`${AGGIETIME_URI}/${LOGOUT_PATH}`);
export const login = async (a_number, password) => { export const login = async (a_number, password) => {
@ -47,8 +50,8 @@ export const login = async (a_number, password) => {
console.log("Waiting until password field is located..."); console.log("Waiting until password field is located...");
await Promise.all( await Promise.all(
[SAML_PASSWORD_SELECTOR, SAML_SUBMIT_SELECTOR].map((selector) => [SAML_PASSWORD_SELECTOR, SAML_SUBMIT_SELECTOR].map((selector) =>
driver.wait(until.elementLocated(By.css(selector))) driver.wait(until.elementLocated(By.css(selector))),
) ),
); );
console.log("Filling password..."); console.log("Filling password...");
@ -66,7 +69,7 @@ export const login = async (a_number, password) => {
} }
await driver.wait( await driver.wait(
until.urlContains(AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE) until.urlContains(AGGIETIME_URL_CONTAINS_SIGNIFIES_AUTH_COMPLETE),
); );
console.log("Retrieving cookie..."); console.log("Retrieving cookie...");
@ -77,7 +80,7 @@ export const login = async (a_number, password) => {
...cookie, ...cookie,
key: cookie.name, key: cookie.name,
}), }),
AGGIETIME_URI AGGIETIME_URI,
); );
console.log("Got it!"); console.log("Got it!");
} finally { } finally {