diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/constants.js | 2 | ||||
-rw-r--r-- | src/main.js | 22 | ||||
-rw-r--r-- | src/retrieve_creds.js | 13 |
3 files changed, 31 insertions, 6 deletions
diff --git a/src/constants.js b/src/constants.js index b2e3f9a..9b6c037 100644 --- a/src/constants.js +++ b/src/constants.js @@ -25,3 +25,5 @@ export const MAX_DEFAULT_RETRY_AMOUNT = 3; export const WAIT_MS = 2000; export const RETRY_EXPONENT = 1.2; export const RETRY_EXPONENTIAL_FACTOR = 1.1; + +export const DEFAULT_PASS_CMD = "pass usu.edu"; diff --git a/src/main.js b/src/main.js index 4b8a1bf..690b55f 100644 --- a/src/main.js +++ b/src/main.js @@ -2,11 +2,13 @@ import { DEFAULT_SOCKET_PATH, + DEFAULT_PASS_CMD, KILL_SIGNALS, REFRESH_JWT_MS, } from "./constants.js"; import * as actions from "./actions.js"; import * as session from "./session.js"; +import retrieve_creds from "./retrieve_creds.js"; import * as argparse from "argparse"; import * as net from "net"; import * as dotenv from "dotenv"; @@ -18,9 +20,12 @@ export default async () => { if (args.daemon) { try { - start_server(args.socket_path, session.logout); - } catch { - fs.unlinkSync(args.socket_path); + start_server(args.socket_path, args.pass_cmd, session.logout); + } catch (e) { + console.error(e); + if (fs.existsSync(args.socket_path)) { + fs.unlinkSync(args.socket_path); + } } } else if (args.action) { if (fs.existsSync(args.socket_path)) { @@ -58,6 +63,11 @@ const build_args = () => { help: `Set server socket path, defaults to ${DEFAULT_SOCKET_PATH}`, }); + parser.add_argument("-p", "--pass_cmd", { + default: DEFAULT_PASS_CMD, + help: `Set GNU pass retrieval command, defaults to ${DEFAULT_PASS_CMD}`, + }); + parser.add_argument("-a", "--action", { help: `Ignored when daemon flag is set. Returns the value of action (see actions.js) when sent over the socket.`, }); @@ -67,7 +77,6 @@ const build_args = () => { const kill_server = (server, socket_path) => { server.close(); - try { fs.unlinkSync(socket_path); } finally { @@ -75,7 +84,7 @@ const kill_server = (server, socket_path) => { } }; -const start_server = async (socket_path, on_exit = () => {}) => { +const start_server = async (socket_path, login_cmd, on_exit = () => {}) => { if (fs.existsSync(socket_path)) { console.error( `ERR: Socket '${socket_path}' already exists. @@ -86,7 +95,8 @@ specify another socket path with --socket_path` process.exit(1); } - await session.login(process.env.A_NUMBER, process.env.PASSWORD); + const { anumber, password } = await retrieve_creds(login_cmd); + await session.login(anumber, password); session.refresh_jwt(); setInterval(session.refresh_jwt, REFRESH_JWT_MS); diff --git a/src/retrieve_creds.js b/src/retrieve_creds.js new file mode 100644 index 0000000..cd596a0 --- /dev/null +++ b/src/retrieve_creds.js @@ -0,0 +1,13 @@ +import { exec } from "node:child_process"; + +export default async (cmd) => + new Promise((res, rej) => { + exec(cmd, (_err, stdout, _stderr) => { + const [password, user_line] = stdout.split("\n"); + const [_anumber_specifier, anumber] = user_line.split("anumber: "); + res({ + password, + anumber, + }); + }); + }); |