Created
August 20, 2013 16:00
-
-
Save thefinn93/6283436 to your computer and use it in GitHub Desktop.
A simple script to check the validity of a Yubikey OTP
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
#!/usr/bin/env python | |
import sys | |
import os | |
import ConfigParser | |
try: | |
import yubico_client | |
except: | |
print "Whoops! Missing yubikey library." | |
print "Try installing it like this: " | |
print "pip install yubico-client" | |
sys.exit(1) | |
def checkKey(otp, yubico): | |
try: | |
yubico.verify(otp) | |
print otp + " is valid" | |
except yubico_client.yubico_exceptions.StatusCodeError: | |
print otp + " has already been used. Invalid" | |
except yubico_client.yubico_exceptions.SignatureVerificationError: | |
print otp + " server response message signature verification failed" | |
except yubico_client.yubico_exceptions.InvalidClientIdError: | |
print "client with the specified id does not exist" | |
sys.exit(1) | |
configs = [ | |
"yubico.ini", | |
".yubico.ini", | |
os.getenv("HOME") + "/.yubico.ini", | |
os.getenv("HOME") + "/.config/yubico.ini", | |
"/etc/yubico.ini" | |
] | |
config = ConfigParser.ConfigParser() | |
config.read(configs) | |
if config.has_option("yubico", "clientid") and config.has_option("yubico", "key"): | |
yubico = yubico_client.Yubico(config.get("yubico", "clientid"), config.get("yubico", "key")) | |
if len(sys.argv) > 1: | |
for otp in sys.argv[1:]: | |
checkKey(otp, yubico) | |
else: | |
otp = raw_input("Input your OTP: ") | |
checkKey(otp, yubico) | |
else: | |
print "Please create a config file in one of the following locations: \n" | |
for location in configs: | |
print location | |
print "\n\nLooking something like this: \n" | |
print "[yubico]" | |
print "clientid: <clientid>" | |
print "key: <key>" | |
print "\n" | |
print "You can pick up a clientid and key from" | |
print "https://upgrade.yubico.com/getapikey/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment