-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
145 lines (124 loc) · 5.56 KB
/
authentication.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
import json
import os
import requests
from c_utils import get_input, API_URL, p_info, header
class Authentication:
@staticmethod
def save_credentials(credentials, res):
print(res.cookies.values())
try:
os.mkdir(os.path.join(os.path.expanduser("~"), ".sysmon"))
with open(os.path.join(os.path.expanduser("~"), ".sysmon", "sysmon_cli.config"), 'w') as config:
json.dump({
"name": res.json()['name'],
"email": credentials['email'],
"session_token": res.cookies.get("session")
}, config)
except OSError or Exception as e:
p_info(e, pre="ERROR")
return False
@staticmethod
def load_credentials():
try:
with open(os.path.join(os.path.expanduser("~"), ".sysmon", "sysmon_cli.config"), 'r') as config:
credentials = json.load(config)
if "email" in credentials and "session_token" in credentials:
return credentials
return None
except FileNotFoundError or Exception:
return None
@staticmethod
def register():
header("registration")
credentials = {
"name": get_input("Enter your name: "),
"email": get_input("Enter email address: "),
"password": get_input("Enter password: ", echo=False)
}
res = requests.post(API_URL + "/auth/get-started", json=credentials)
if res.status_code == 200:
p_info("Registered successfully!", pre="INFO")
Authentication.verification()
@staticmethod
def verification():
header("verification")
p_info("Enter the verification code given in the email we sent you", pre="INFO")
p_info("If you haven't received the code, enter your email address to resend", pre="INFO")
token_or_email = get_input("Verification Code [or email address]: ")
if "@" in token_or_email:
res = requests.get(API_URL + "/auth/verification", json={"email": token_or_email})
if res.status_code == 200:
p_info("Email sent successfully!", pre="INFO")
token_or_email = get_input("Verification Code: ")
elif res.status_code == 400:
p_info("This account has already been authenticated!")
return True
elif res.status_code == 404:
p_info("Couldn't find the user, verify the credentials or sign up again.", pre="ERROR")
return False
else:
p_info("Couldn't process your request.", pre="ERROR")
res = requests.post(API_URL + "/auth/verification", json={"token": token_or_email})
if res.status_code == 200:
p_info("Email address verified, your account has been activated!", pre="INFO")
return True
elif res.status_code == 404:
p_info("Couldn't find the user, verify the credentials or sign up again.", pre="ERROR")
return False
else:
p_info("Couldn't process your request.", pre="ERROR")
@staticmethod
def handle_login_responses(res, credentials=None):
if res.status_code == 200:
p_info("Logged in successfully!", pre="INFO")
if res and credentials:
Authentication.save_credentials(credentials, res)
return True
elif res.status_code == 404:
p_info("Couldn't find the user, verify the credentials or sign up again.", pre="ERROR")
return False
else:
p_info("Needs re-verification.", pre="ERROR")
try:
os.remove(os.path.join(os.path.expanduser("~"), ".sysmon", "sysmon_cli.config"))
except OSError as e:
p_info(e, pre="ERROR")
@staticmethod
def login(sub=False):
if not sub:
header("login")
p_info("Checking for existing credentials...", pre="INFO", end=" ")
credentials = Authentication.load_credentials()
if credentials:
print("found")
cont_ = get_input(f"Continue as {credentials['email']}? (yes(y) | no(n)): ").strip().lower()
if cont_.startswith('y'):
res = requests.get(API_URL + "/auth/login", cookies={
'session': credentials['session_token']
})
ret = Authentication.handle_login_responses(res)
if ret is not None:
return ret
else:
print("not found")
credentials = {
"email": get_input("Enter email address: "),
"password": get_input("Enter password: ", echo=False)
}
res = requests.post(API_URL + "/auth/login", json=credentials)
return Authentication.handle_login_responses(res, credentials)
@staticmethod
def logout():
credentials = Authentication.load_credentials()
if credentials:
requests.get(API_URL + "/auth/logout", cookies={
"session": credentials["session_token"]
})
p_info("Logged out!", pre="INFO")
else:
p_info("No log in session found", pre="INFO")
try:
os.remove(os.path.join(os.path.expanduser("~"), ".sysmon", 'sysmon_cli.config'))
except OSError or Exception:
if credentials:
p_info("Couldn't remove config file", pre="ERROR")