-
Notifications
You must be signed in to change notification settings - Fork 2
/
totp.py
executable file
·51 lines (44 loc) · 1.98 KB
/
totp.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
#!/usr/bin/env python
"""Time-based One-time Password Tool
CLI tool that generates rfc6238 TOTPs, using the native system keyring service
to store secret keys for accounts protected by two-factor authentication.
requires:
https://pypi.org/project/onetimepass/
https://pypi.org/project/keyring/
https://pypi.org/project/pyperclip/
"""
from argparse import ArgumentParser
import keyring, pyperclip
import onetimepass as otp
# Use try clause to make a prompt fucntion that works with either python 2 or 3
try: prompt = raw_input
except NameError: prompt = input
# Set up argument parser
parser = ArgumentParser(description='Time-based One-time Password Generator')
parser.add_argument('-a', '--add', action='store_true',
help='Add an account rather than generating TOTP.')
parser.add_argument('-p', '--print-only', action='store_true',
help='Do not copy TOTP to clipboard.')
parser.add_argument('--service', type=str, default='otp_secret',
help='Keyring item name. (Default: otp_secret)')
parser.add_argument('account', type=str, help='Account to generate TOTP for.')
args = parser.parse_args()
# Get shared secret for account
secret = keyring.get_password(args.service, args.account)
# Add an account or print TOTP and copy to clipboard
if args.add:
# If a secret already exists for the account prompt before overwriting
add_secret = True
if secret:
q = "Account %s already has a shared secret. Overwrite? (Y/N) " % args.account
a = prompt(q)
if a and a[0].lower() != 'y': add_secret = False
else: print("Overwriting shared secret for account %s." % args.account)
if add_secret:
secret = prompt("Enter shared secret key (with or without spaces): ")
keyring.set_password(args.service, args.account, secret)
else:
# Generate TOTP, copy to clipboard (unless --print-only) and print
token = otp.get_totp(secret, as_string=True)
args.print_only or pyperclip.copy(token)
print(token)