Skip to content

Instantly share code, notes, and snippets.

@mikeboiko
Last active March 31, 2024 13:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeboiko/58ab730afd65bca0a125bc12b6f4670d to your computer and use it in GitHub Desktop.
Save mikeboiko/58ab730afd65bca0a125bc12b6f4670d to your computer and use it in GitHub Desktop.
Use bitwarden rbw as git-credential helper
#!/usr/bin/env bash
# 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
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
rbw ls > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Please login to rbw to use git credential helper" > /dev/stderr
exit
fi
user=`rbw get --full ${params["host"]} | grep "Username:" | cut -d' ' -f2-`
pass=`rbw get ${params["host"]}`
if [ "x$user" == "x" ] || [ "x$pass" == "x" ]; then
echo "Couldn't find host in rbw DB." > /dev/stderr
exit
fi
echo username=$user
echo password=$pass
fi
@ivankovnatsky
Copy link

i was going to use in nixos, but we have no #!/bin/bash path, can you please change it to: #!/usr/bin/env bash

@mikeboiko
Copy link
Author

i was going to use in nixos, but we have no #!/bin/bash path, can you please change it to: #!/usr/bin/env bash

Sure no problem. Glad you found the script useful.

@ivankovnatsky
Copy link

i can now reference your raw file, thanks a lot! i also moved from bw to rbw meanwhile 👍

@mikeboiko
Copy link
Author

Yea rbw is much better!

@modem7
Copy link

modem7 commented Aug 8, 2022

Heya,

Probably an ID10T issue, but I'm getting:

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.

Not quite sure where I've gone wrong.

git config --global --edit

[credential]
        helper = rbw

Script location:
/usr/bin/git-credential-rbw

@ivankovnatsky
Copy link

ivankovnatsky commented Aug 9, 2022

Hey, rbw upstream got itself a helper by the way: https://github.com/doy/rbw/blob/master/bin/git-credential-rbw, scripts are almost the same, though.

You probably need to generate an API token to use in your vault item: github.com / [token]

❯ rbw ls|rg 'https://github.com'
https://github.com

https://github.com/settings/tokens/new.

@mikeboiko
Copy link
Author

@modem7, yea I agree with what @ivankovnatsky stated.
I didn't realize that rbw had a helper in the repo. You should probably use that.
I recently switched to gh for my GitHub authentication. I still use rbw for my BitBucket repos.
I tested changing my GitHub password in bitwarden to my GitHub Personal Access Token, and my helper worked properly.

@ivankovnatsky
Copy link

I didn't realize that rbw had a helper in the repo. You should probably use that.

they added it some time after, and I did not like their implementation, too weird for my taste 😄

@modem7
Copy link

modem7 commented Aug 9, 2022

@modem7, yea I agree with what @ivankovnatsky stated. I didn't realize that rbw had a helper in the repo. You should probably use that. I recently switched to gh for my GitHub authentication. I still use rbw for my BitBucket repos. I tested changing my GitHub password in bitwarden to my GitHub Personal Access Token, and my helper worked properly.

Aye, the GH client seems to work quite nicely tbf! I was mostly seeing if RBW had a better use case, but I think for me, probably not!

@quul
Copy link

quul commented Jul 25, 2023

For macOS users, due to the bash version in macOS is still 3.2(at least before Ventura 13.2.1) and not supported declare -A command.
A workaround for this is follow the steps above:
Install a newer bash via homebrew or anything else and then change shebang to #!/usr/bin/env bash

@mikeboiko
Copy link
Author

For macOS users, due to the bash version in macOS is still 3.2(at least before Ventura 13.2.1) and not supported declare -A command. A workaround for this is follow the steps above: Install a newer bash via homebrew or anything else and then change shebang to #!/usr/bin/env bash

Ahh yes, good call. When I was using a mac, I got around this issue by installing the latest bash version from homebrew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment