-
Notifications
You must be signed in to change notification settings - Fork 7
/
2fa
executable file
·45 lines (42 loc) · 1.64 KB
/
2fa
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
#!/usr/bin/env python3
import json
import os
import sys
import pyotp # ImportError? pip install pyotp
SAVEFILE = os.path.expanduser("~/.config/2fa.json") # Assumes Linux for now
try:
with open(SAVEFILE) as f: sites = json.load(f)
except FileNotFoundError:
sites = {}
if len(sys.argv) < 2:
print("USAGE: 2fa sitename")
print("Use any name you like to identify the site. If unknown, will set up.")
print(pyotp.TOTP("JBSWY3DPEHPK3PXP", 8).now())
elif sys.argv[1] == "--install":
# Lifted from steamguard
import subprocess
path = subprocess.check_output(["pkg-config", "--variable=completionsdir", "bash-completion"])
path = path.decode("ascii").strip() # Non-ASCII path? I'll figure it out if it ever happens.
os.makedirs(path, exist_ok=True)
with open(path + "/2fa", "w") as f:
print("complete -C '2fa --complete' 2fa", file=f)
elif sys.argv[1] == "--complete":
for site in sorted(sites):
if site.startswith(sys.argv[3]): print(site)
elif sys.argv[1] in sites:
# Plain, no noise. You can pipe this into something or backtick it into
# another command without fiddling around.
print(pyotp.TOTP(*sites[sys.argv[1]]).now())
else:
site = sys.argv[1]
print("Setting up new %r site" % site)
print("Hit Ctrl-C to cancel at any time.")
secret = input("Enter the shared secret: ").replace(" ", "")
digits = int(input("Desired length? [6] ") or "6")
print(pyotp.TOTP(secret, digits).now())
sites[site] = (secret, digits)
with open(SAVEFILE, "w") as f: json.dump(sites, f)
print("Saved. Back this file up securely:")
print(SAVEFILE)
if len(sites) == 1: print("It contains your new shared secret.")
else: print("It contains %d shared secrets." % len(sites))