-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
110 lines (86 loc) · 3.15 KB
/
run.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
"""
A toy app demonstrating 2-factor auth
"""
from __future__ import print_function
import getpass
import sys
from bobcraft import mfa, user
from bobcraft.login import login
from bobcraft.made_up import get_user_choice
class ToyApplication(object):
def __init__(self):
self.user_db = user.user_db = user.UserDB()
def dispatch(self, text, *args):
method_name = text.lower().strip().replace(' ', '_')
method = getattr(self, method_name)
return method(*args)
def quit(self):
sys.exit()
def start(self):
options = ("Add user", "Select user", "Quit")
choice = get_user_choice(options)
self.dispatch(choice)
def add_user(self):
while True:
username = raw_input("username: ")
if username.strip():
password = getpass.getpass("password: ")
try:
self.user_db.add_user(username, password)
except user.UserAlreadyExists:
print("User already exists.\n")
else:
return self.start()
else:
print("Username cannot be blank.\n")
def select_user(self, *args):
options = self.user_db.pwd_db.keys()
options.append("Back to start")
options.append("Quit")
choice = get_user_choice(options)
if choice == "Back to start":
choice = "Start"
if choice in ("Start", "Quit"):
self.dispatch(choice)
else:
self.user_screen(choice)
def user_screen(self, username):
options = ("Login", "Add factor", "Remove factor", "Back to Select User")
choice = get_user_choice(options)
if choice == "Back to Select User":
choice = "Select user"
self.dispatch(choice, username)
def login(self, username):
password = getpass.getpass("password: ")
def post_login(usr):
print("{0} logged in successfully!\n".format(usr.username))
self.user_screen(usr.username)
try:
login(username, password, post_login)
except:
print("Invalid login credentials\n")
self.user_screen(username)
def add_factor(self, username):
options = ("TOTP", "SMS", "Yubikey", "Cancel")
choice = get_user_choice(options)
if choice == "Cancel":
return self.user_screen(username)
usr = user.User(username, self.user_db)
method = getattr(mfa, "add_{0}".format(choice.lower()))
method(usr)
self.user_screen(username)
def remove_factor(self, username):
usr = user.User(username, self.user_db)
options = list(usr.possession_factors) + ["Cancel"]
choice = get_user_choice(options)
if choice == "Cancel":
return self.user_screen(username)
print("Please enter credentials to remove factor:\n")
try:
mfa.secure_remove_factor(usr, choice)
except:
print("Unable to remove factor due to invalid login credentials\n")
self.user_screen(username)
if __name__ == "__main__":
app = ToyApplication()
app.start()