Skip to content

Instantly share code, notes, and snippets.

@toonetown
Last active January 20, 2017 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toonetown/b7f1a241d45812402d3d5757bd0eb968 to your computer and use it in GitHub Desktop.
Save toonetown/b7f1a241d45812402d3d5757bd0eb968 to your computer and use it in GitHub Desktop.
Helper script for managing stoken with macOS keychain storage
#!/bin/bash
case "${1}" in
add)
set -e
shift; SERVICE="${1}"
[ -n "${SERVICE}" ] || { echo "Usage $0 add <service>" >&2; exit 1; }
echo "Adding keychain stoken service '${SERVICE}' token password data"
/usr/bin/security add-generic-password -s "${SERVICE}" -a "${SERVICE}.token" -w || exit $?
echo "Adding keychain stoken service ${SERVICE} token PIN data..."
/usr/bin/security add-generic-password -s "${SERVICE}" -a "${SERVICE}.pin" -w || exit $?
;;
remove)
shift; SERVICE="${1}"
[ -n "${SERVICE}" ] || { echo "Usage $0 remove <service>" >&2; exit 1; }
echo "Removing keychain stoken service '${SERVICE}'"
/usr/bin/security delete-generic-password -s "${SERVICE}" -a "${SERVICE}.token" &>/dev/null
/usr/bin/security delete-generic-password -s "${SERVICE}" -a "${SERVICE}.pin" &>/dev/null
exit 0
;;
generate)
set -e
shift; SERVICE="${1}"
[ -n "${SERVICE}" ] || { echo "Usage $0 generate <service> [generate [options [...]]]"; exit 1; }
TOKEN="$(/usr/bin/security find-generic-password -s "${SERVICE}" -a "${SERVICE}.token" -w)"
if [ $# -eq 1 ]; then
/usr/bin/security find-generic-password -s "${SERVICE}" -a "${SERVICE}.pin" -w \
| /usr/local/bin/stoken --token="${TOKEN}" -s
else
shift
echo "Adding generated stoken service '${SERVICE}'"
/usr/bin/security add-generic-password "$@" -w "$(${0} generate "${SERVICE}")"
fi
;;
*) echo "Usage: $0 <add|remove|generate> <service> [generate [options [...]]]" >&2; exit 1 ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment