Skip to content

Instantly share code, notes, and snippets.

@ivankovnatsky
Last active October 9, 2021 09:02
Show Gist options
  • Save ivankovnatsky/dde972d3e6f84b7591758c6d1dc9b20d to your computer and use it in GitHub Desktop.
Save ivankovnatsky/dde972d3e6f84b7591758c6d1dc9b20d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# bw 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 bw.
# 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 bw
declare -A params
if [ "x$1" == "xget" ]; then
read line
while [ -n "$line" ]; do
key=${line%%=*}
value=${line#*=}
params[$key]=$value
read line
done
if [ "x${params['protocol']}" != "xhttps" ]; then
exit
fi
if [ -z "${params["host"]}" ]; then
exit
fi
bw list items > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Please login to bw to use git credential helper" > /dev/stderr
exit
fi
id=$(bw list items --search ${params["host"]}|jq ".[] | select(.name == \"${params[\"host\"]}\").id" -r)
if [ "x$id" == "x" ]; then
echo "Couldn't find id in bw DB." > /dev/stderr
echo ${params}
exit
fi
user=$(bw get username ${id})
pass=$(bw get password ${id})
if [ "x$user" == "x" ] || [ "x$pass" == "x" ]; then
echo "Couldn't find host in bw DB." > /dev/stderr
exit
fi
echo username=$user
echo password=$pass
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment