yubico-client

raw JSON →
1.13.0 verified Fri May 01 auth: no python

A Python library for verifying YubiKey One Time Passwords (OTPs) via the Yubico validation API. Version 1.13.0 is stable with infrequent releases.

pip install yubico-client
error ImportError: No module named yubico_client
cause Installed old 'yubico' package or forgot to install yubico-client.
fix
Run 'pip install yubico-client' and use 'from yubico_client import Yubico'.
error AttributeError: module 'yubico_client' has no attribute 'Yubico'
cause Used old import 'import yubico' instead of 'from yubico_client import Yubico'.
fix
Change import to 'from yubico_client import Yubico'.
error yubico_client.errors.YubicoVerificationError: No such client
cause Invalid client ID or secret key, or missing environment variables.
fix
Check YUBICO_CLIENT_ID and YUBICO_SECRET_KEY values. Generate credentials at https://upgrade.yubico.com/getapikey/.
error yubico_client.errors.YubicoVerificationError: Bad OTP
cause OTP string is malformed or not a valid YubiKey OTP.
fix
Ensure OTP is a 44-character string from a YubiKey. Example: 'ccccccbcvghfvbjfbbgvhu'.
breaking In version 1.0, the import changed: 'import yubico' no longer works; use 'from yubico_client import Yubico'.
fix Update imports to 'from yubico_client import Yubico'.
gotcha The library uses the Yubico validation API v2. You must have a client ID and secret key from Yubico. Without them, verify() will raise an exception.
fix Set environment variables YUBICO_CLIENT_ID and YUBICO_SECRET_KEY.
gotcha verify() returns True/False, not a Yubico response object. For detailed failure reasons, catch exceptions from yubico_client.errors.
fix Use try/except around verify() to get exceptions like YubicoVerificationError.

Verify a YubiKey OTP using the Yubico validation API.

import os
from yubico_client import Yubico

client_id = os.environ.get('YUBICO_CLIENT_ID', '')
secret_key = os.environ.get('YUBICO_SECRET_KEY', '')
yubi = Yubico(client_id, secret_key)
otp = 'ccccccbcvghfvbjfbbgvhu'  # example OTP
if yubi.verify(otp):
    print('OTP is valid')
else:
    print('OTP verification failed')