Skip to content

Commit

Permalink
bw_login now cats session_file when already logged in, with failure i…
Browse files Browse the repository at this point in the history
…f it does not exist; added FORCE flag to force logout and login as well as creating a new session
  • Loading branch information
cjvirtucio87 committed Mar 25, 2022
1 parent 7fa2d8f commit 4d63012
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions bwsh/bin/bw_session.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
###
### Options:
### BW_CREDS: path to the credentials script (default: "${HOME}/.secrets/bitwarden/creds.sh)
### FORCE: force login and creating a new session
### GET_PATH: if set to anything, print the path to the script so that it can be
### sourced, then exit
###
Expand Down Expand Up @@ -46,9 +47,21 @@ function bw_creds {
}

function bw_login {
local session_file="${HOME}/.secrets/bitwarden/session"
if [[ -v FORCE ]]; then
rm -f "${session_file}"
bw logout
fi

if bw login --check "$(get_username)" &>/dev/null; then
log "already logged in"
return
if [[ -f "${session_file}" ]]; then
cat "${session_file}"
return
fi

log "missing session file; logout and log back in with this script"
return 1
fi

local login_output
Expand All @@ -61,7 +74,8 @@ function bw_login {
echo -n "${login_output}" \
| grep --extended-regexp '^\$ export' \
| sed --regexp-extended 's/^\$ export BW_SESSION="(.+)"$/\1/g' \
| tee "${HOME}/.secrets/bitwarden/session"
| tee "${session_file}"
chmod 0600 "${session_file}" &>/dev/null
}

function get_password {
Expand Down Expand Up @@ -95,9 +109,7 @@ if (return 0 &>/dev/null); then
export BW_SESSION
elif bw_session="$(bw_login)"; then
log "logged in"
if [[ -n "${bw_session}" ]]; then
export BW_SESSION="${bw_session}"
fi
export BW_SESSION="${bw_session}"
else
log "login failed"
return 1
Expand Down

0 comments on commit 4d63012

Please sign in to comment.