env/bin/credentials.sh
2020-08-23 14:48:24 +00:00

102 lines
1.9 KiB
Bash

#!/bin/sh
# credentials
# Written in 2019-2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
cat - <<. >&2
Usage:
${0##*/} [-mpu] account
.
exit 1
}
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
clip()
{
xclip -q -r -l 1 -sel clip 2>/dev/null
}
get_user()
{
printf user
sekrit cp "$1/user" && printf "\n"
}
get_pass()
{
printf pass
sekrit cp "$1/pass" && printf "\n"
}
get_mfa()
{
printf mfa
sekrit get "$1/mfa" | otpcli | clip && printf "\n"
}
get_account()
{
account=$1
user=$2
pass=$3
mfa=$4
if [ $user = no ] && [ $pass = no ] && [ $mfa = no ]; then
sekrit has "$account/pass" || err "unknown account $account"
pass=yes
sekrit has "$account/user" && user=yes
sekrit has "$account/mfa" && mfa=yes
fi
if [ $user = check ]; then
sekrit has "$account/user" || err "account $account has no user"
user=yes
fi
if [ $pass = check ]; then
sekrit has "$account/pass" || err "account $account has no pass"
pass=yes
fi
if [ $mfa = check ]; then
sekrit has "$account/mfa" || err "account $account has no MFA"
mfa=yes
fi
[ $user = yes ] && get_user "$account"
[ $pass = yes ] && get_pass "$account"
[ $mfa = yes ] && get_mfa "$account"
}
mfa=no
pass=no
user=no
while getopts mpu flag; do
case $flag in
m) mfa=check ;;
p) pass=check ;;
u) user=check ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] && [ -n "$1" ] || usage
key=$1
: ${SEKRIT_DIR:=~/keep/sekrit/accounts}
export SEKRIT_DIR
get_account "$key" $user $pass $mfa