From 0bc487ccc938e82157b415d4fb8e7cf9a5b54233 Mon Sep 17 00:00:00 2001 From: James Patrick Date: Fri, 3 Jul 2020 02:39:14 -0400 Subject: [PATCH] Added pass extensions To tried to properly explain. Pretty easy to read. Go do that. --- pass/extensions/get.bash | 90 +++++++++++++++++++++++++++++++++++ pass/extensions/get_pass.bash | 49 +++++++++++++++++++ pass/extensions/get_user.bash | 49 +++++++++++++++++++ pass/makefile | 24 ++++++++++ 4 files changed, 212 insertions(+) create mode 100755 pass/extensions/get.bash create mode 100755 pass/extensions/get_pass.bash create mode 100755 pass/extensions/get_user.bash create mode 100644 pass/makefile diff --git a/pass/extensions/get.bash b/pass/extensions/get.bash new file mode 100755 index 0000000..e5ad460 --- /dev/null +++ b/pass/extensions/get.bash @@ -0,0 +1,90 @@ +#!/usr/bin/env bash +# pass get - Password Store Extension (https://www.passwordstore.org/) +# Copyright (C) 2020 James Patrick +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +R='\033[0;31m' +G='\033[0;32m' +NC='\033[0m' + +PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}" + +cmd_get_usage() { + cat <<-_EOF +Usage: + $PROGRAM get pattern pass_name + Parses the pass data for lines pulling out the value of browserpass's + format of "key:value". The 'pattern' arg is a case insenitive, extended + regex using grep -iE. + + https://github.com/browserpass/browserpass-extension + + Options: + Nada. +_EOF + exit 0 +} + + +cmd_get_assert(){ + test="$1" + local pattern="$2" expected="$3" + + actual="$(cmd_get "$pattern")" + if [[ "$actual" == "$expected" ]] ; then + echo -e "$G ✔ '$actual' == '$expected' $NC" + else + echo -e "$R ✘ f('$test', '$pattern') → '$actual' != '$expected' $NC" + fi +} + +cmd_get_test(){ + cmd_get_assert "username:value" "(user|username|login)" "value" + cmd_get_assert " user: whitespace" "(user|username|login)" "whitespace" + + val="multiple:lines +value:pairs" + cmd_get_assert "$val" "value" "pairs" + cmd_get_assert "Key:capitalization doesn't counts" "key" \ + "capitalization doesn't counts" + cmd_get_assert "missing:empty" "i don't exist" "" + + + exit 0 +} + +cmd_get(){ + local pattern="$1" pass_name="$2" + + # valid the regex is valid + echo | grep -qiE "$pattern" + if [[ "$?" == 2 ]] ; then + >&2 echo -e "${R}Invalid Pattern:${NC} $pattern" + return 1 + fi + + echo "${test:-$(cmd_show "$pass_name")}" \ + | grep -iE "$pattern" \ + | head -1 \ + | cut -d ':' -f2- \ + | sed -e 's/^[ \t]*//' +} + + +[[ "$1" == "test" ]] && cmd_get_test +[[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]] && cmd_get_usage +[[ "$#" != "2" ]] && cmd_get_usage +cmd_get "$@" diff --git a/pass/extensions/get_pass.bash b/pass/extensions/get_pass.bash new file mode 100755 index 0000000..fe17654 --- /dev/null +++ b/pass/extensions/get_pass.bash @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# pass get - Password Store Extension (https://www.passwordstore.org/) +# Copyright (C) 2020 James Patrick +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}" + +cmd_get_pass_usage() { + cat <<-_EOF +TODO: +Usage: + $PROGRAM get_pass pass_name + Grabs the pass based on rules for Browserpass. + 1. match "key:value" where key, either pass, password, or secret + 2. if no match is present default to the first line. + + https://github.com/browserpass/browserpass-extension + + Options: + Nada. +_EOF + exit 0 +} + + +cmd_get_user(){ + local pass_name="$1" val backup + val=$(pass get "(pass|password|secret)" "$pass_name") + backup=$(cmd_show "$pass_name" | head -1) + echo "${val:-$backup}" +} + +[[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]] && cmd_get_user_usage +[[ "$#" != "1" ]] && cmd_get_user_usage +cmd_get_user "$@" diff --git a/pass/extensions/get_user.bash b/pass/extensions/get_user.bash new file mode 100755 index 0000000..0085b71 --- /dev/null +++ b/pass/extensions/get_user.bash @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# pass get - Password Store Extension (https://www.passwordstore.org/) +# Copyright (C) 2020 James Patrick +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}" + +cmd_get_user_usage() { + cat <<-_EOF +TODO: +Usage: + $PROGRAM get_user pass_name + Grabs the username based on rules for Browserpass. + 1. match "key:value" where key, either user, username, or login + 2. if no match is present default to a trimmed filename. + + https://github.com/browserpass/browserpass-extension + + Options: + Nada. +_EOF + exit 0 +} + + +cmd_get_user(){ + local pass_name="$1" val + val=$(pass get "(user|username|login)" "$pass_name") + backup=$(echo "$pass_name" | awk -F"/" '{print $NF}' ) + echo "${val:-$backup}" +} + +[[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]] && cmd_get_user_usage +[[ "$#" != "1" ]] && cmd_get_user_usage +cmd_get_user "$@" diff --git a/pass/makefile b/pass/makefile new file mode 100644 index 0000000..cbd40f0 --- /dev/null +++ b/pass/makefile @@ -0,0 +1,24 @@ +# -*- mode: makefile-gmake; -*- +SRC := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) + +include ../lib/shared.mk +TARGET := ~/.password-store + +install: | init update + +init: + if (( $$+commands[pass] )) ; then + $(report) header "Setting up pass" + [ -e $(TARGET) ] \ + && $(mk_link) $(SRC)/extensions $(TARGET)/.extensions \ + || $(report) warn "$(TARGET) doesn't exist. Skipping." + else + $(report) debug " pass not installed. Skipping." + fi + +update: + + +remove: + $(report) header "Removing pass" + $(rm_link) $(TARGET)