From d599e9cd5e67a8006b2fa0c270de9f0f7d7089c5 Mon Sep 17 00:00:00 2001 From: James Blair Date: Fri, 10 Feb 2023 19:23:42 +1300 Subject: [PATCH] Start working on a custom bw git credential cache. --- .bashrc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ readme.org | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/.bashrc b/.bashrc index 6ac4d9a..35ecf87 100644 --- a/.bashrc +++ b/.bashrc @@ -39,6 +39,55 @@ function bwli () { local test=$(export BW_SESSION=~/.bw_session) && bw list item function bwol () { local test=$(export BW_SESSION=~/.bw_session) && bw get item --pretty "$1" | grep https | awk '{print $2}' | $cpcmd; } function bwgu () { local test=$(export BW_SESSION=~/.bw_session) && bw get username "$1" | $cpcmd; } +# custom git credential cache implementation for bitwarden +# https://github.com/bitwarden/cli/blob/master/examples/git-credential-bw.sh +function bw_gitea () { + declare -A params + + if [[ "$1" == "get" ]]; then + read -r line + while [ -n "$line" ]; do + key=${line%%=*} + value=${line#*=} + params[$key]=$value + read -r line + done + + if [[ "${params['protocol']}" != "https" ]]; then + exit + fi + + if [[ -z "${params["host"]}" ]]; then + exit + fi + + if ! bw list items --search "asdf" > /dev/null 2>&1; then + echo "Please login to Bitwarden to use git credential helper" > /dev/stderr + exit + fi + + id=$(bw list items --search "${params["host"]}"|jq ".[] | select(.name == \"${params["host"]}\").id" -r) + + if [[ -z "$id" ]]; then + echo "Couldn't find item id in Bitwarden DB." > /dev/stderr + echo "${params}" + exit + fi + + user=$(bw get username "${id}") + pass=$(bw get password "${id}") + + if [[ -z "$user" ]] || [[ -z "$pass" ]]; then + echo "Couldn't find host in Bitwarden DB." > /dev/stderr + exit + fi + + echo username="$user" + echo password="$pass" + fi +} + + # automate multimonitor command alias hdmioff='xrandr --output HDMI-0 --off' alias hdmion='xrandr --output HDMI-0 --auto --rate 144.00 --left-of DVI-I-0 --primary' @@ -100,6 +149,7 @@ elif [ -f ~/.bw_session ]; then export BW_SESSION=$(cat ~/.bw_session); else bwu; fi # Helper function for tmate pane renaming +# This isn't working properly yet! function renamepane { printf '\033]2;%s\033\\' "${1}" } diff --git a/readme.org b/readme.org index f54cc4c..5eb2c5a 100644 --- a/readme.org +++ b/readme.org @@ -47,4 +47,4 @@ The thing I like most about mutt is the caching which means I can traverse or sc One of the things I really like and use daily is leveraging bitwarden for my one time passwords. I can run something like ~bw get totp | xclip~ in a terminal and have a two factor auth code copied straight onto my clipboard ready to paste where it is required. -The [[./.bashrc][.bashrc]] file in this repository includes helper functions to make bitwarden workflows even faster. +The [[./.bashrc][.bashrc]] file in this repository includes helper functions to make bitwarden workflows even faster, including an implementation of a custom git credential cache to dynamically retrieve git https credentials from bitwarden.