From e9bbc58fe1b54567738bc2720cd0332feb9ff3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Mon, 30 Sep 2019 11:20:52 +0200 Subject: Add surf-pass to access user names and passwords from surf --- surf-pass | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 surf-pass (limited to 'surf-pass') diff --git a/surf-pass b/surf-pass new file mode 100755 index 0000000..ee20f52 --- /dev/null +++ b/surf-pass @@ -0,0 +1,98 @@ +#!/bin/bash +set -e + +usage() { + echo "Usage: $0 XID {NAME|PASS}" >&2 + exit 1 +} + +set_active_value() { + xprop -id "$xid" -f "_SURF_ACTIVE_VALUE" 8s -set "_SURF_ACTIVE_VALUE" "$1" +} + +get_active_value() { + xprop -id "$xid" -notype "_SURF_ACTIVE_VALUE" | sed -nE 's/^_SURF_ACTIVE_VALUE = "(.*)"$/\1/p' +} + +get_uri() { + xprop -id "$xid" -notype "_SURF_URI" | sed -nE 's/^_SURF_URI = "(.*)"$/\1/p' +} + +domain() { + get_uri | awk -F/ '{print $3}' +} + +primary_domain() { + domain | awk -F. '{ print $(NF-1) "." $NF}' +} + +find_pass() { + local postfix="$1" + shift + + array=() + while read -r field; do + local nogpg="${field%.gpg}" + local noprefix="${nogpg#*/.password-store/}" + if [ -n "$noprefix" ]; then + array+=("$noprefix") + fi + done <<<"$(find ~/.password-store/"$postfix" -type f "$@" 2>/dev/null)" +} + +choose_entry() { + if [ "${#array[@]}" -eq 0 ]; then + find_pass + fi + if [ "${#array[@]}" -eq 1 ]; then + echo "${array[0]}" + return + fi + printf '%s\n' "${array[@]}" | dmenu -p 'Pass:' -w "$xid" | head -1 +} + + +pass_name() { + local array + find_pass "$(primary_domain)" + + local entry + entry="$(choose_entry)" + set_active_value "${entry##*/}" +} + +pass_pass() { + local array curname + array=() + curname="$(get_active_value)" + if [ -n "$curname" ]; then + find_pass "$(primary_domain)" -name "$curname" + fi + if [ "${#array[@]}" -eq 0 ]; then + find_pass "$(primary_domain)" + fi + + local entry + entry="$(choose_entry)" + set_active_value "$(pass "$entry")" + set_active_value # Reset value to empty to hide password +} + +####################### + +[ $# -eq 2 ] || usage + + +xid="$1" + +case "$2" in + NAME) + pass_name + ;; + PASS) + pass_pass + ;; + *) + usage + ;; +esac -- cgit v1.2.3