Skip to content

Instantly share code, notes, and snippets.

@calebcase
Created February 2, 2018 20:03
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 calebcase/66d91af4dd610a83290ed3d9a48c32b5 to your computer and use it in GitHub Desktop.
Save calebcase/66d91af4dd610a83290ed3d9a48c32b5 to your computer and use it in GitHub Desktop.
Script for fetching yubikey oauth tokens.
#!/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