-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbw
executable file
·49 lines (43 loc) · 1.75 KB
/
bw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
# This script will proxy commands for the bitwarden CLI, intercepting them to
# get or set a # session token for seamless use between shells and other programs.
# Make sure this script is in your $PATH, so you can simply run 'bw' like you
# normally would.
# If you have a $HOME/bin dir that is at the front of your $PATH, you should be
# able to use this as an install method:
# curl https://raw.githubusercontent.com/mjslabs/bitagent/master/examples/bw > $HOME/bin/bw && chmod 0755 $HOME/bin/bw
# You will want to make sure that $bwcmd below is the full path to the real
# bitwarden cli. The default is 'bwcli' stored in ~/bin
# Installation of the real bitwarden CLI
bwcmd="${HOME}/bin/bwcli"
# Default socket location
basock="${HOME}/.bitagent.sock"
# This assumes you've run a `go install` to install bitagent
bitagent="${GOBIN:-${HOME}/go/bin}/bitagent $basock"
# If our socket doesn't exist then run bitagent
if [[ ! -e $basock ]]; then
$bitagent 1>/dev/null & disown
fi
# If runninig 'bw unlock' or 'bw login', expect the session key to be output on success
if [[ $1 == "unlock" || $1 == "login" ]]; then
unlockOut=$($bwcmd $1)
<<< $unlockOut grep -q BW_SESSION
if [[ $? == "0" ]]; then
echo "$1 successful"
# Store the key in bitagent
echo "P"$(<<< $unlockOut tr -d '\n' | sed -e 's/^.*BW_SESSION="\(.*\)".*$/\1/') | nc -U $basock -N
else
echo "$1 failed"
echo $unlockOut
fi
exit
else
# We're running some bitwarden command that isn't login or unlock,
# so set BW_SESSION using the secret in bitagent, then run the bw cli
export BW_SESSION="$(echo G | nc -U $basock -N)"
if [[ $BW_SESSION == "" ]]; then
# Avoids "Session key is invalid." being sent to the terminal
unset BW_SESSION
fi
$bwcmd $*
fi