env/utils/scripts/create-otp-uri.sh

58 lines
1.2 KiB
Bash
Raw Normal View History

#!/bin/sh
# env
# Written in 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 - <<EOF >&2
Usage:
${0##*/} [-d digits] [-p period] [-a algorithm]
${0##*/} [-H counter] [-d digits] [-a algorithm]
EOF
exit 1
}
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
algorithm=
counter=
digits=
period=
type=totp
while getopts a:d:H:p: flag; do
case $flag in
a) algorithm=$OPTARG
;;
d) digits=$OPTARG
;;
H) counter=$OPTARG
type=hotp
;;
p) period=$OPTARG
;;
*) usage
;;
esac
done
printf "otpauth://%s/" "$type"
printf "?secret="
cat - | tr -d "\n"
[ -n "$algorithm" ] && printf "&algorithm=%s" "$algorithm"
[ -n "$counter" ] && printf "&counter=%s" "$counter"
[ -n "$digits" ] && printf "&digits=%s" "$digits"
[ -n "$period" ] && printf "&period=%s" "$period"
printf "\n"