From 015c0c051ace3955cb98aa4864d47dfff5bac536 Mon Sep 17 00:00:00 2001 From: Yigit Date: Sun, 21 Mar 2021 10:49:19 +0300 Subject: [PATCH] Exchanging speed for security, but its worth it IMO --- src/index.js | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 7ad2d0f..db06e98 100644 --- a/src/index.js +++ b/src/index.js @@ -15,17 +15,23 @@ class BitwardenDmenu { } const isLoggedIn = async () => { - try { - bwRun('login', '--check') - } catch (e) { - if (e instanceof CommandError && e.stderr === 'You are not logged in.') { - return false - } else { - throw e + + if(!existsSync('/tmp/bwdmenu_loggedin')){ + try { + bwRun('login', '--check') + writeFileSync('/tmp/bwdmenu_loggedin', "",{encoding:'utf8', flag:'w'}); + } catch (e) { + if (e instanceof CommandError && e.stderr === 'You are not logged in.') { + return false + } else { + throw e + } } + return true; } - return true + return true; } + const login = async ({ dmenuArgs, dmenuPswdArgs }) => { const email = await dmenuRun( '-p', @@ -34,6 +40,7 @@ const login = async ({ dmenuArgs, dmenuPswdArgs }) => { )('\n') const password = await dmenuRun(...dmenuPswdArgs)('\n') const session = bwRun('login', email, password, '--raw') + writeFileSync('/tmp/bwdmenu_loggedin', "",{encoding:'utf8', flag:'w'}); return session } @@ -75,12 +82,21 @@ const getSessionVar = async ({ dmenuPswdArgs, saveSession, sessionFile }) => { * if --sync-vault-after < time since the last sync */ const syncIfNecessary = ({ oldestAllowedVaultSync }, session) => { - const last = bwRun('sync', '--last', `--session=${session}`) + // const last = bwRun('sync', '--last', `--session=${session}`) + if(!existsSync('/tmp/bwdmenu_lastsync')){ + console.debug('syncing vault...') + bwRun('sync', `--session=${session}`) + writeFileSync('/tmp/bwdmenu_lastsync', new Date().toISOString(),{encoding:'utf8', flag:'w'}); + return; + } + + const last = readFileSync('/tmp/bwdmenu_lastsync', {encoding:'utf8', flag:'r'}); const timeSinceSync = (new Date().getTime() - new Date(last).getTime()) / 1000 if (timeSinceSync > oldestAllowedVaultSync) { console.debug('syncing vault...') bwRun('sync', `--session=${session}`) console.debug(`sync complete, last sync was ${last}`) + writeFileSync('/tmp/bwdmenu_lastsync',new Date().toISOString(),{encoding:'utf8', flag:'w'}); } } @@ -88,9 +104,15 @@ const syncIfNecessary = ({ oldestAllowedVaultSync }, session) => { * get the list all password accounts in the vault */ const getAccounts = ({ bwListArgs }, session) => { - const listStr = bwRun('list', 'items', bwListArgs, `--session=${session}`) + var listStr; + if(!existsSync('/tmp/bwdmenu_accounts')){ + listStr = bwRun('list', 'items', bwListArgs, `--session=${session}`) + writeFileSync('/tmp/bwdmenu_accounts', listStr,{encoding:'utf8', flag:'w'}); + }else{ + listStr = readFileSync('/tmp/bwdmenu_accounts', {encoding:'utf8', flag:'r'}); + } const list = JSON.parse(listStr) - return list + return list; }