-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-credential-bw
executable file
·76 lines (66 loc) · 2.05 KB
/
git-credential-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# A credential helper for git to retrieve usernames and passwords from bitwarden.
# For general usage, see https://git-scm.com/docs/gitcredentials.
# Here's a quick version:
# 1. Put this somewhere in your path (i.e. ~/.local/bin/git-credential-bw)
# 2. git config --global credential.helper bw
#
# Author: Marco Salathe
# Website: https://github.com/cosama/git-credential-bw
## Configurable Parameters:
# Timeout after which the password needs to be entered again (seconds)
timeout=900
declare -A params
declare -A credentials
if [ "x$1" == "x-l" ];
then
shift
bwuser=$1
shift
fi
if [ "x$1" == "xget" ];
then
while read line;
do
key=${line%%=*}
value=${line#*=}
params[$key]=$value
done
if [ "x${params['protocol']}" != "xhttps" ];
then
exit
fi
if [ -z "${params['host']}" ];
then
exit
fi
cache_buf=$(echo "host=${params['host']}" | git credential-cache get)
if [ -z "${cache_buf}" ];
then
session=$(bw unlock --raw < /dev/tty)
if [ $? -ne 0 ];
then
session=$(bw login --raw < /dev/tty)
if [ $? -ne 0 ];
then
echo "Failed to login to bitwarden" > /dev/stderr
exit
fi
fi
credentials['username']=$(bw get --session ${session} --raw username ${params['host']})
credentials['password']=$(bw get --session ${session} --raw password ${params['host']})
if [ "x${credentials['username']}" == "x" ] || [ "x${credentials['password']}" == "x" ];
then
echo "Couldn't find host in bitwarden database." > /dev/stderr
exit
fi
(echo "protocol=${params['protocol']}";
echo "host=${params['host']}";
echo "username=${credentials['username']}";
echo "password=${credentials['password']}") | git credential-cache store --timeout $timeout
echo "username=${credentials['username']}"
echo "password=${credentials['password']}"
else
printf "${cache_buf}"
fi
fi