2024-02-07 06:52:48 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2023-12-20 13:53:36 +00:00
|
|
|
# rbw git-credential helper
|
|
|
|
# Based on https://github.com/lastpass/lastpass-cli/blob/master/contrib/examples/git-credential-lastpass
|
|
|
|
|
|
|
|
# A credential helper for git to retrieve usernames and passwords from rbw.
|
|
|
|
# For general usage, see https://git-scm.com/docs/gitcredentials.
|
|
|
|
# Here's a quick version:
|
|
|
|
# 1. Put this somewhere in your path.
|
|
|
|
# 2. git config --global credential.helper rbw
|
|
|
|
|
2024-02-07 06:52:48 +00:00
|
|
|
set -f
|
|
|
|
|
|
|
|
[ "$1" = get ] || exit
|
|
|
|
|
|
|
|
while read -r line; do
|
|
|
|
case $line in
|
|
|
|
protocol=*)
|
|
|
|
protocol=${line#*=} ;;
|
|
|
|
host=*)
|
|
|
|
host=${line#*=} ;;
|
|
|
|
username=*)
|
|
|
|
user=${line#*=} ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
output=
|
|
|
|
#shellcheck disable=2154
|
|
|
|
for arg in \
|
|
|
|
"${protocol:+$protocol://}$host" \
|
|
|
|
"$host" \
|
|
|
|
"${host2=${host%.*}}" \
|
|
|
|
"${host2#*.}"
|
|
|
|
do
|
|
|
|
# exit on first good result
|
|
|
|
[ -n "$user" ] && output=$(rbw get --full "$arg" "$user") && break
|
|
|
|
output=$(rbw get --full "$arg") && break
|
|
|
|
done || exit
|
|
|
|
|
|
|
|
printf '%s\n' "$output" | sed -n '
|
|
|
|
1{ s/^/password=/p }
|
|
|
|
s/^Username: /username=/p
|
|
|
|
s/^URI: /host=/p
|
|
|
|
'
|