Created
February 2, 2018 20:03
-
-
Save calebcase/66d91af4dd610a83290ed3d9a48c32b5 to your computer and use it in GitHub Desktop.
Script for fetching yubikey oauth tokens.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
query=${1:-amazon} | |
# Shut down other users of the smart card (otherwise we can't use it). | |
pkill scdaemon || true | |
if [[ -f ~/.token ]]; then | |
last=$(<~/.token) | |
else | |
last='000000' | |
fi | |
# Fetch the token... | |
while true; do | |
token=$(ykman oath code "$query" | grep -E '[0-9]{6}$' | sed -r 's/.* ([0-9]{6})$/\1/') | |
if [[ $token != $last ]]; then | |
break | |
fi | |
printf 'Same token detected. Try again in 30...\n' 1>&2 | |
sleep 30 | |
printf 'Touch token again!\n' 1>&2 | |
done | |
# Save this token as the one last generated. | |
printf '%s' "$token" > ~/.token | |
printf '%s' "$token" | xclip -selection primary | |
printf '%s' "$token" | xclip -selection clipboard | |
printf '%s\n' "$token" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment