-
Notifications
You must be signed in to change notification settings - Fork 0
/
totpcvt.py
executable file
·189 lines (150 loc) · 4.42 KB
/
totpcvt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
##
# Feed this script a andOTP JSON Backup.
#
# https://github.com/vs49688/scripts/
# Zane van Iperen <zane@zanevaniperen.com>
#
# SPDX-License-Identifier: CC0-1.0
#
# * Make sure all arguments are LaTeX compatible
# * `qrencode` needs to be installed
# * Compile the resulting .tex file with pdflatex/xelatex
#
# Usage: totpcvt.py <name> <email>
##
import json
import sys
import urllib.parse
import re
from collections import namedtuple
import subprocess
import os.path
class Entry(object):
__slots__ = [
'filename',
'issuer',
'label',
'username',
'algorithm',
'digits',
'secret',
'type',
'period', # TOTP only, optional, default 30
'counter', # HOTP only
]
def build_url(self) -> str:
query = {
'secret': ee.secret,
'issuer': ee.issuer
}
if ee.algorithm != 'SHA1':
query['algorithm'] = ee.algorithm
if ee.digits != 6:
query['digits'] = ee.digits
if ee.period and ee.period != 30:
query['period'] = ee.period
if ee.counter:
query['counter'] = ee.counter
return urllib.parse.urlunparse((
'otpauth',
'',
urllib.parse.quote(os.path.join('//', ee.type.lower(), f'{ee.issuer}:{ee.label}')),
'',
urllib.parse.urlencode(query),
''
))
REGEX_LABEL = re.compile(r'^(?:(.+):)?(.+)$')
if len(sys.argv) != 3:
print('Usage: {0} <name> <email>'.format(sys.argv[0]), file=sys.stderr)
exit(2)
name = sys.argv[1]
email = sys.argv[2]
data = json.loads(sys.stdin.read())
urls = []
# Quickly check this before doing anything
for e in data:
if 'issuer' in e:
continue
print('issuer not found in backup, please upgrade your andOTP and re-export', file=sys.stderr)
exit(1)
i = 0
for e in data:
ee = Entry()
ee.algorithm = e['algorithm']
ee.digits = int(e['digits'])
ee.secret = e['secret']
ee.label = e['label']
ee.issuer = e['issuer']
ee.type = e['type']
ee.period = ''
# These won't actually import correctly, but the user
# should be able to decode it and add it manually
if ee.type == 'TOTP':
ee.period = int(e['period'])
ee.counter = None
elif ee.type == 'HOTP':
ee.period = None
ee.counter = int(e['counter'])
elif ee.type == 'STEAM':
ee.period = None
ee.counter = None
else:
raise Exception(f'unknown type {ee.type}')
m = re.match(REGEX_LABEL, ee.label)
if not m:
raise Exception(f'invalid label {ee.label}')
ee.filename = 'qr_{0:0>2}.png'.format(i)
ee.username = m[2]
subprocess.run(['qrencode', '-t', 'PNG', '-v', '10', '-o', ee.filename, '--', ee.build_url()], check=True)
urls.append(ee)
#print('\\noindent\\includegraphics{{../qr_{0:0>2}.png}}'.format(i))
i += 1
#print(urls)
print(rf"""
\documentclass[oneside]{{article}}
\usepackage[margin=1in]{{geometry}}
\usepackage{{graphicx}}
\usepackage{{parskip}}
\usepackage{{hyperref}}
\usepackage{{xurl}}
\usepackage{{fancyhdr}}
\usepackage[style=default]{{datetime2}}
\usepackage{{lastpage}}
\pagestyle{{fancy}}
\lhead{{Backup MFA Codes}}
\chead{{{name}}}
\rhead{{{email}}}
\lfoot{{Generated at \DTMnow}}
\cfoot{{}}
\rfoot{{Page \thepage\ of \pageref{{LastPage}}}}
\begin{{document}}
""")
for i in range(len(urls)):
# if i % 6 == 0 and i != 0:
# print()
# print(r' \newpage')
if i % 2 == 0:
print()
secret = urls[i].secret
sec = ' '.join([secret[i:i+4] for i in range(0, len(secret), 4)])
typestring = f'{urls[i].type} -- {urls[i].algorithm} -- {urls[i].digits}'
if urls[i].period:
typestring = typestring + f' -- {urls[i].period}'
print(r' \begin{minipage}[t]{0.5\textwidth}')
print(r' \centering')
print(r' \includegraphics[width=0.9\textwidth]{{{0}}}'.format(urls[i].filename))
print(fr"""
\begin{{tabular}}{{p{{0.2\linewidth}}p{{0.7\linewidth}}}}
Username & \texttt{{{urls[i].username}}} \\
Issuer & {urls[i].issuer} \\
Type & {typestring} \\
Secret & \texttt{{{sec}}} \\
\end{{tabular}}
""")
print(r' \vspace{\baselineskip}')
print(r' \end{minipage}', end='')
if i % 2 == 0:
print('%', end='')
print()
print(r'\end{document}')