Skip to content

Instantly share code, notes, and snippets.

@dolph
Created November 18, 2011 20:22
Show Gist options
  • Save dolph/1377644 to your computer and use it in GitHub Desktop.
Save dolph/1377644 to your computer and use it in GitHub Desktop.
Yubikey Registration & Authentication Demo
# pip install yubico
from yubico import yubico
# Yubico API credentials
YUBICO_CLIENT_ID = '6634'
YUBICO_SECRET_KEY = 'HdRb8AA24+Ud8VL2E+sEEZUiySg='
# Initialize our Yubico API access
YUBICO = yubico.Yubico(YUBICO_CLIENT_ID, YUBICO_SECRET_KEY)
REGISTERED_USERS = []
def main():
print
print '[R]egister YubiKey'
print '[A]uthenticate YubiKey'
print '[L]ist Users'
print '[Q]uit'
choice = raw_input('Choose: ').lower()
if len(choice) >= 1:
choice = choice[:1]
if choice == 'r':
return register_user()
elif choice == 'a':
return authenticate_user()
elif choice == 'l':
return list_users()
elif choice == 'q':
return
else:
print 'Unrecognized option, try again'
return main()
def verify_otp():
try:
otp = raw_input('YubiKey: ')
print 'Verifying...'
assert YUBICO.verify(otp)
return otp[:12]
except:
return False
def register_user():
user = verify_otp()
if not user:
print 'Registration FAILED'
elif user in REGISTERED_USERS:
print 'YubiKey already registered to user %s' % REGISTERED_USERS.index(user)
elif user not in REGISTERED_USERS:
print 'Registering user %s...' % len(REGISTERED_USERS)
REGISTERED_USERS.append(user)
print 'Registration SUCCESSFUL'
return main()
def authenticate_user():
user = verify_otp()
if user in REGISTERED_USERS:
print 'Identified user %s' % REGISTERED_USERS.index(user)
print 'Authentication SUCCESSFUL'
else:
print 'Authentication FAILED'
return main()
def list_users():
print "\n".join([str(REGISTERED_USERS.index(user)) + ': ' + user for user in REGISTERED_USERS])
return main()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment