| #!/usr/bin/env python3
|
|
|
| import os
|
| import sys
|
| from pyzbar.pyzbar import decode
|
| from PIL import Image
|
|
|
| if len(sys.argv) < 2:
|
| print(sys.argv[0], '<input_image>')
|
| sys.exit(-1)
|
|
|
| if os.path.splitext(sys.argv[1])[1].lower() == '.heic':
|
| from pillow_heif import register_heif_opener
|
| register_heif_opener()
|
|
|
| img = Image.open(sys.argv[1])
|
| # Rescaled image is much faster and is more robust for large QR codes.
|
| size = [1024, round(img.size[1]*1024.0/img.size[0])]
|
| img2 = img.resize(size)
|
| decoded = decode(img2)
|
| # If that didn't work, try scanning the unresized image.
|
| if len(decoded) < 1:
|
| decoded = decode(img)
|
|
|
| if len(decoded) < 1:
|
| print('No QR codes found!')
|
| sys.exit(-1)
|
|
|
| result = decoded[0].data.decode()
|
|
|
| print(result)
|
|
|
| if result[:15] == 'otpauth://totp/':
|
| name = ''
|
| issuer = ''
|
| desc = ''
|
| key = ''
|
| split = result[15:].split('?')
|
| if len(split) == 2:
|
| name = split[0]
|
| desc = split[0]
|
| key = split[1]
|
| split = name.split(':')
|
| if len(split) == 2:
|
| issuer = split[0]
|
| name = split[1]
|
| desc = split[1]
|
| split = name.split('@')
|
| if len(split) == 2:
|
| name = split[0]
|
| if not issuer:
|
| issuer = split[1]
|
| split = key.split('&')
|
| for s in split:
|
| kv = s.split('=')
|
| if len(kv) == 2:
|
| if kv[0] == 'secret':
|
| key = kv[1]
|
| elif kv[0] == 'issuer':
|
| issuer = kv[1]
|
| if key:
|
| issuer = issuer.lower()
|
| name = name.lower()
|
| print()
|
| print('TOTP found:')
|
| print()
|
| print(f'echo -n "{key}" | secret-tool store --label="{desc}" "{issuer}" "{name}"')
|
| print()
|
| print(f'secret-tool lookup {issuer} {name}')
|
| print()
|