def test_get_provisioning_uri(self, backend): secret = b"12345678901234567890" hotp = HOTP(secret, 6, SHA1(), backend) assert hotp.get_provisioning_uri("Alice Smith", 1, None) == ( "otpauth://hotp/Alice%20Smith?digits=6&secret=GEZDGNBV" "GY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&counter=1") assert hotp.get_provisioning_uri("Alice Smith", 1, 'Foo') == ( "otpauth://hotp/Foo:Alice%20Smith?digits=6&secret=GEZD" "GNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&issuer=Foo" "&counter=1")
def test_get_provisioning_uri(self, backend): secret = b"12345678901234567890" hotp = HOTP(secret, 6, SHA1(), backend) assert hotp.get_provisioning_uri( "Alice Smith", 1, None) == ("otpauth://hotp/Alice%20Smith?digits=6&secret=GEZDGNBV" "GY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&counter=1") assert hotp.get_provisioning_uri( "Alice Smith", 1, "Foo") == ("otpauth://hotp/Foo:Alice%20Smith?digits=6&secret=GEZD" "GNBVGY3TQOJQGEZDGNBVGY3TQOJQ&algorithm=SHA1&issuer=Foo" "&counter=1")
def generate_hotp_uri(secret, counter, email): """ Generate a Google authenticator compatible QR code provisioning URI Args: secret: 16 character base32 secret counter: unique integer value email: Authenticator email address Return: URI: otpauth://hotp/[email protected]?secret=JBSWY3DPEHPK3PXP&counter=0&issuer=FrostyWeb """ if isinstance(secret, unicode): secret = secret.encode('utf-8') try: key = base64.b32decode(secret) hotp = HOTP(key, 6, SHA1(), backend=default_backend(), enforce_key_length=False) return hotp.get_provisioning_uri(email, counter, 'FrostyWeb') except (ValueError, TypeError): pass return None
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.twofactor.hotp import HOTP from cryptography.hazmat.primitives.hashes import SHA1 from cryptography.hazmat.primitives.twofactor import InvalidToken import pyqrcode key = os.urandom(16) counter = 1 issuer = 'GruPyPR' account_name = input('Your name: ') hotp = HOTP(key, 6, SHA1(), backend=default_backend()) uri = hotp.get_provisioning_uri(account_name, counter, issuer) url = pyqrcode.create(uri) print('Scan this!\n') url.svg('hotp.svg', scale=8) webbrowser.open('hotp.svg') while True: try: hotp_value = bytes(input('Two factor password: '******'utf-8') hotp.verify(hotp_value, counter) print('You are authenticated!\n') except InvalidToken: print('You shall not pass!\n') continue except KeyboardInterrupt: sys.exit(0)