Skip to content

Instantly share code, notes, and snippets.

@amiryal
Last active March 31, 2024 19:07
Show Gist options
  • Save amiryal/122b9c1a0fc4656efcf2d20eaf4913f3 to your computer and use it in GitHub Desktop.
Save amiryal/122b9c1a0fc4656efcf2d20eaf4913f3 to your computer and use it in GitHub Desktop.
Script to copy username/password/TOTP to clipboard from dmenu-selected entry in Password Store
#!/bin/bash
# Adapted from https://git.zx2c4.com/password-store/tree/contrib/dmenu/passmenu
# at 0b2f803fe61992.
shopt -s nullglob globstar
mode=password
case "$1" in
'--username')
mode=username
shift
;;
'--totp')
mode=totp
shift
;;
'--password')
shift
;;
esac
prefix=${PASSWORD_STORE_DIR:-~/.password-store}
mapfile -t password_files < <(find "$prefix" -name '*.gpg' -exec stat -c '%X {}' '{}' \; | sort -n -r | cut -d' ' -f2-)
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
password=$(printf '%s\n' "${password_files[@]}" | dmenu "$@")
[[ -n $password ]] || exit
data="$(pass show "$password" 2>/dev/null)"
[[ -n "$data" ]] || exit
clip() {
printf '%s' "$1" | xsel -i -b -t 10
}
case $mode in
username)
username=$(<<<"$data" tail -n +2 | sed -ne '/^username: /I s/^username: // p')
clip "$username"
;;
password)
pw=$(<<<"$data" head -n 1)
clip "$pw"
;;
totp)
totp_key=$(<<<"$data" tail -n +2 | grep -o 'otpauth:[^\b]*' | grep -o 'secret=[[:alnum:]]*' | cut -d= -f2)
clip "$(oathtool --totp -b "$totp_key")"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment