#!/usr/bin/env python3 # (c) 2023 th\@bogus.net import qrcode # pip install from PIL import Image import pyotp # pip install import argparse parser = argparse.ArgumentParser(description='Generate Google Auth TOTP QR code .png', prog='gauth-qrcode.py') parser.add_argument('--secret', type=str, dest='secret', action='store', help="TOTP secret string (optional)") parser.add_argument('--uri', type=str, dest='uri', action='store', help="Identifying string e.g. user@myapp") parser.add_argument('--out', type=str, dest="outfile", action='store', help="Output filename (default: gauth_qr_code.png)") parser.add_argument('--print-key', dest="pk", action='store_true', help="Print the generated secret (default: no)") opts = parser.parse_args() if opts.secret: secret = opts.secret else: secret = pyotp.random_base32() if opts.pk: print(secret) if opts.uri: uri = opts.uri else: print("Error: need --uri to identify this TOTP key") exit(1) if opts.outfile: outfile = opts.outfile else: outfile = "gauth_qr_code.png" # Create the TOTP object totp = pyotp.TOTP(secret) # Create the QR code object qr = qrcode.QRCode( version=1, box_size=10, border=5 ) # Make the QR code qr.add_data(totp.provisioning_uri(uri)) qr.make(fit=True) # Create an image from the QR code img = qr.make_image(fill_color="black", back_color="white") # Save the QR code as an image file img.save(outfile)