-
Notifications
You must be signed in to change notification settings - Fork 0
/
userCon.py
303 lines (270 loc) · 9.7 KB
/
userCon.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# std:
import time
import secrets
import random
# pip-int:
import dotsi
# pip-ext:
# n/a
# loc:
import bu
from appDef import app
from constants import K
import userMod
import magitokMod
import utils
import auth
import emailer
@app.post("/userCon/setupFirstUser")
def post_userCon_setupFirstUser():
email, fname, lname = bu.unpack_jdata("email fname lname")
userCount = userMod.getUserCount()
if userCount >= 1:
return bu.abort("Setup already completed. Please visit /login to log in.")
# ==> No users yet.
user = userMod.buildUser(
email=email,
fname=fname,
lname=lname,
isPrime=True,
isVerified=True,
isAdmin=True,
)
userMod.insertUser(user)
return auth.sendAuthSuccessResponse(user)
def sendMagicLink(user, token):
emailer.send(
toList=[user.email],
subject="ZapFeedback Login Link",
body=f"""Hi {user.fname},\n\nHere's your login link:\n
{K.SITE_URL}/userCon/verifyMagiLogin/{user._id}/{token}""",
subtype="plain",
)
@app.post("/userCon/startMagiLogin")
def post_userCon_startMagiLogin():
email = bu.unpack_jdata("email")[0]
user = userMod.getUserByEmail(email)
SUCCESS = {"status": "success"}
NOT_FOUND = SUCCESS # <-- Mitigates user enumeration
if not (user and user.isVerified):
time.sleep(2 + random.random() * 3)
print(f"No such verified user: {email}")
return NOT_FOUND
# ==> User found
print(f"User found: {email}")
token = secrets.token_urlsafe()
magitok = magitokMod.buildMagitok(user._id, token)
magitokMod.upsertMagitok(magitok)
sendMagicLink(user, token)
return SUCCESS
@app.get("/userCon/verifyMagiLogin/<magitokId>/<token>")
def get_userCon_verifyMagiLogin(magitokId, token):
magitok = magitokMod.getMagitok(magitokId)
REDO = " Please visit /login to restart the log in process."
ASK_ADMIN = " Please contact your admin for help."
# ^-- Leading space in both above constants.
if not magitok:
return bu.abort("No such login link." + REDO)
# ==> Magitok Found
expiresAt = magitok.issuedAt + utils.minutes_to_ms(15)
if expiresAt < utils.now():
# Note: Not deleting magitok here, to aid w/ testing.
return bu.abort("Login link expired." + REDO)
# ==> Magitok Not Yet Expired
if not utils.checkPw(token, magitok.hToken):
return bu.abort("Incorrect login link." + REDO)
# ==> Token Matches
user = userMod.getUser({"_id": magitokId})
assert user
if not user.isVerified:
return bu.abort("Unconfirmed account." + ASK_ADMIN)
# ==> User is verified.
if user.isDeactivated:
return bu.abort("Account deactivated." + ASK_ADMIN)
# ==> Login successful!
magitokMod.deleteMagitok(magitok) # <-- Delete OTP
auth.sendAuthSuccessResponse(user) # <-- Sets cookies.
# TODO: Check if bu.redirect() retains set cookies.
dashUrl = f"{K.SITE_URL}/front/dash.html"
return f"""
<h2>Redirecting ...</h2>
<p>Please <a href="{dashUrl}">click here</a> if you aren't redirected soon.</p>
<script>window.location.href = "{dashUrl}";</script>
"""
@app.post("/userCon/detectLogin")
@auth.seshful
def post_userCon_detectLogin(sesh):
assert bu.get_jdata() == {}
return {
"user": userMod.snipUser(sesh.user),
}
# IMPORTANT: Do NOT use auth.sendAuthSuccessResponse().
# That's only for when a password-based success is
# obtained: login/join. Here, a login is
# merely being detected. No fresh login has occured.
# Note: auth.sendAuthSuccessResponse() sends the
# XCSRF token downstream. We don't wanna do that here.
@app.post("/userCon/logout")
def post_userCon_logout():
bu.clearCookie("userId")
bu.clearCookie("xCsrfToken")
return {"status": "success"}
@app.post("/userCon/fetchUserList")
@auth.seshful
def post_userCon_fetchUserList(sesh):
userList = userMod.getUserList()
# print("userList = ", userList);
snippedUserList = utils.map(userList, userMod.snipUser)
# print("snippedUserList = ", snippedUserList);
return {"userList": snippedUserList}
def genInviteLink(invitee, veriCode):
inviteLinkQs = utils.qs.dumps(
{
"inviteeId": invitee._id,
"veriCode": veriCode,
}
)
return "%s/front/join.html?%s" % (K.SITE_URL, inviteLinkQs)
def sendInviteEmail(invitee, veriCode):
emailer.send(
toList=[invitee.email],
subject="ZapFeedback Invitation Link",
body=genInviteLink(invitee, veriCode),
subtype="plain",
)
@app.post("/userCon/inviteUser")
@auth.seshful
def post_userCon_inviteUser(sesh):
assert sesh.user.isAdmin
jdata = bu.get_jdata(
ensure="""
invitee_fname, invitee_lname, invitee_email,
invitee_isAdmin,
"""
)
inviter = sesh.user
invitee = userMod.getUserByEmail(jdata.invitee_email)
newVeriCode = userMod.genVeriCode()
if not invitee:
# ==> Invitee doesn't already exists. (fresh invite)
invitee = userMod.buildUser(
email=jdata.invitee_email,
fname=jdata.invitee_fname,
lname=jdata.invitee_lname,
inviterId=inviter._id,
veriCode=newVeriCode,
isAdmin=jdata.invitee_isAdmin,
)
else:
# ==> Invitee already exists. (re-invite)
if invitee.isVerified:
return bu.abort(
"That email address is already associated with a confirmed user."
)
# ==> __NOT__ already verified.
invitee.update(
{
"fname": jdata.invitee_fname,
# ^-- Allows re-inviting w/ updated name.
"lname": jdata.invitee_lname,
"inviterId": inviter._id,
# ^-- We update to the latest inviter's id.
"hVeriCode": utils.hashPw(newVeriCode),
# ^-- On reinvite, new veriCode is gen'd, prev expires.
"isAdmin": jdata.invitee_isAdmin,
}
)
assert invitee.email == jdata.invitee_email
# ^-- fname/lname/inviterId/hVeriCode can change, but not email.
# ==> `invitee` object is now available.
userMod.upsertUser(invitee)
if emailer.checkSendingEnabled():
return_inviteLink = None
sendInviteEmail(invitee, newVeriCode)
else:
return_inviteLink = genInviteLink(invitee, newVeriCode)
return {
"user": userMod.snipUser(invitee),
"inviteLink": return_inviteLink,
}
def getUnverifiedUserByVeriCode(userId, veriCode):
"Helper. Returns non-deactivated, unverified user."
user = userMod.getUser(userId)
if not user:
return bu.abort("No such user or invitation.")
if user.isVerified:
return bu.abort("Already joined. Please log in.")
if user.isDeactivated:
return bu.abort("Account deactivated.")
if not utils.checkPw(veriCode, user.hVeriCode):
# print("veriCode = %r" % veriCode)
# print("hVeriCode = %r" % user.hVeriCode)
return bu.abort("Verification failed.")
# ==> Valid veriCode.
return user
@app.post("/userCon/fetchInvitedUserByVeriCode")
def post_userCon_fetchInvitedUserByVeriCode():
j = bu.get_jdata(ensure="userId, veriCode")
user = getUnverifiedUserByVeriCode(j.userId, j.veriCode)
return {"user": userMod.snipUser(user)}
@app.post("/userCon/acceptInvite")
def post_userCon_acceptInvite():
j = bu.get_jdata("userId email fname lname veriCode")
user = getUnverifiedUserByVeriCode(j.userId, j.veriCode)
assert all(
[
user._id == j.userId,
user.email == j.email,
user.fname == j.fname, # <--- TODO: Allow changing?
user.lname == j.lname, # <--^
utils.checkPw(j.veriCode, user.hVeriCode),
]
)
user.update({"isVerified": True}) # In-memory update.
userMod.replaceUser(user)
return auth.sendAuthSuccessResponse(user)
@app.post("/userCon/toggleUser_isDeactivated")
@auth.seshful
def post_userCon_toggleUser_isDeactivated(sesh):
assert sesh.user.isAdmin
jdata = bu.get_jdata(ensure="thatUserId, preToggle_isDeactivated")
thatUser = userMod.getUser(
{
"_id": jdata.thatUserId,
# No "isVerified" condition, as unverified (invited/pre-joined) users can be deactivated.
"isDeactivated": jdata.preToggle_isDeactivated,
# ^-- This helps ensure that we don't accidently perform the opposite op.
}
)
if not thatUser:
# ==> Couldn't find user to be de/reactivated.
return bu.abort(
"No such user. Can't de/reactivate. Please refresh (Ctrl+R) and retry."
)
# ==> User to be de/reactivated found.
if thatUser._id == sesh.user._id:
assert not thatUser.isDeactivated
# ^-- Assert active. If deactivated, this line must be unreachable.
return bu.abort("One can't deactivate their own account.")
# ==> Not trying to deactivate own account.
if thatUser.isPrime:
assert not thatUser.isDeactivated
# ^-- Assert active. Primary admin user cannot be deactivated.
return bu.abort("Can't de/reactivate primary admin.")
# ==> `thatUser` is NOT the primary admin.
assert (
thatUser
and thatUser._id != sesh.user._id
and
# ^-- Like w/ db-query above, no 'isVerified' related assertion.
thatUser.isDeactivated == jdata.preToggle_isDeactivated # and
)
thatUser.update(
{
"isDeactivated": not thatUser.isDeactivated,
}
)
assert userMod.validateUser(thatUser)
userMod.replaceUser(thatUser)
return {"user": userMod.snipUser(thatUser)}
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx