Skip to content

Instantly share code, notes, and snippets.

@andrew-d
Created September 21, 2016 07:35
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrew-d/6095e3cce3877881393a19659430ddf0 to your computer and use it in GitHub Desktop.
Save andrew-d/6095e3cce3877881393a19659430ddf0 to your computer and use it in GitHub Desktop.
Read a FreeOTP tokens.xml file and display the entries as QR codes
#!/usr/bin/env python
from __future__ import print_function
import base64
import ctypes
import json
import subprocess
import sys
import xml.etree.ElementTree as ET
from urllib.parse import urlencode
def main():
tree = ET.parse(sys.argv[1])
root = tree.getroot()
assert root.tag == 'map'
for child in root:
assert child.tag == 'string'
name = child.attrib['name']
if name == 'tokenOrder':
continue
if ':' in name:
service, user = name.split(':', 1)
else:
user = name
service = 'Unknown'
info = json.loads(child.text)
#print("Info for %s: %r" % (name, info))
# Ensure that we only have unsigned values, then get secret
secret_bytes = [ctypes.c_ubyte(x).value for x in info['secret']]
secret_hex = ''.join('%02X' % (x,) for x in secret_bytes)
secret = bytes.fromhex(secret_hex)
secret_b32 = base64.b32encode(secret)
# Make fancy URL.
params = {
'secret': secret_b32,
'issuer': service,
'counter': info['counter'],
'digits': info['digits'],
'period': info['period'],
'algorithm': info['algo'],
}
tmpl = 'otpauth://{type}/{service}:{user}?{params}'
url = tmpl.format(
type=info['type'].lower(),
service=service,
user=user,
params=urlencode(params),
)
#print(url)
print("Displaying: {0}".format(name))
process_qrencode = subprocess.Popen(['qrencode', '-o', '-', url],
stdout=subprocess.PIPE)
process_display = subprocess.Popen(['display'],
stdin=process_qrencode.stdout)
# Allow the qrencode to recieve a SIGPIPE if display exits
process_qrencode.stdout.close()
process_display.communicate()
if __name__ == "__main__":
main()
@hkparker
Copy link

This saved me from a tough spot, thanks!

@naglis
Copy link

naglis commented Dec 31, 2016

It saved me some hours of struggle as well, thanks a bunch for sharing!

@2edff206
Copy link

2edff206 commented Jun 1, 2017

Thank you so much, saved me a lot of time !!!!

@dschrempf
Copy link

Amazing :)! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment