aboutsummaryrefslogtreecommitdiff
path: root/bin/git-credential-read-only
blob: e47259bc1f382f7623f1c03daabdb3d4de7db048 (about) (plain)
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
#!/bin/sh
#
# Reads STDIN ensuring protocol=https (if present) and then until the
# line host=<HOST> is found. Finally, uses <HOST> to look through
# login info to then output credentials for git.

LOGINS_DB=$HOME/.config/logins.db.asc

# We only support get as the first argument
if [ $1 != "get" ]; then
    exit 1
fi

# Read input to figure out which host/domain we want to read
# credentials from
export LESSPASS_MASTER_PASSWORD=`pass lesspass`
SELECTION=
while read input; do
    key=`echo $input | cut -f 1 -d '='`
    value=`echo $input | cut -f 2 -d '='`
    if [ $key == 'protocol' ] && [ $value != 'https' ]; then
        exit 1
    fi
    if [ $key == 'host' ]; then
        SELECTION=`gpg --decrypt $LOGINS_DB 2>/dev/null | grep $value`
        break
    fi
done

if [ -z "${SELECTION}" ]; then
    exit 1
fi

DOMAIN=`echo $SELECTION | cut -f 1 -d ' '`
USERNAME=`echo $SELECTION | cut -f 2 -d ' '`
OPTIONS=`echo $SELECTION | cut -f 1,2 -d ' ' --complement`

# Output credentials in the format git expects
echo protocol=https
echo host=$DOMAIN
echo username=$USERNAME
echo password=`lpass $DOMAIN $USERNAME $OPTIONS`