-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserCon.py
223 lines (205 loc) · 8.51 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
# std:
# n/a
# pip-int:
import dotsi;
# pip-ext:
# n/a
# LOC:
import bu;
from appDef import app;
from constants import K;
import userMod;
import utils;
import auth;
import emailer;
@app.post("/userCon/setupFirstUser")
def post_userCon_setupFirstUser ():
jdata = bu.get_jdata(ensure="email fname lname pw");
email, fname, lname, pw = utils.unpack(jdata, "email fname lname pw");
print(email, fname, lname, pw);
userCount = userMod.getUserCount();
if userCount >= 1:
return bu.abort("Setup already completed. Please visite /login to log in.");
user = userMod.buildUser(
email=email, fname=fname, lname=lname, pw=pw,
isRootAdmin=True, isVerified=True,
accessLevel=auth.alm.getMaxLevel(),
);
userMod.insertUser(user);
return auth.sendAuthSuccessResponse(user);
@app.post("/userCon/loginDo")
def post_userCon_loginDo ():
email, pw = bu.unpack_jdata("email pw"); # TODO: 'rememberMe'
user = userMod.getUserByEmail(email);
#print("user=", user);
if not user:
# TODO: Reconsider this
# It's good UX, but susceptible to user enumeration.
return bu.abort("User not found. Please retry or contact your admin for help.");
# ==> User found.
if not user.isVerified:
return bu.abort("Unconfirmed account. Please contact your admin for help.");
# ==> User is verified.
if user.isDeactivated:
return bu.abort("Account deactivated. Please contact your admin for help.");
# ==> User is non-deactivated.
if not utils.checkPw(pw, user.hpw):
return bu.abort("Login failed due to email/password mismatch. Please retry.");
# ==> SUCCESS. User can log in.
return auth.sendAuthSuccessResponse(user);
@app.post("/userCon/detectLogin")
def post_userCon_detectLogin ():
assert bu.get_jdata() == {};
sesh = auth.getSesh();
if not sesh.user:
return {"user": None}; # <-- That's '200 OK'. CLI asked if there's a current user, SER says no.
# ==> Current session detected:
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")
def post_userCon_fetchUserList ():
sesh = auth.getSesh();
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 = "Polydojo Invitation Link",
body = genInviteLink(invitee, veriCode),
subtype = "plain",
);
@app.post("/userCon/inviteUser")
def post_userCon_inviteUser ():
jdata = bu.get_jdata(ensure="""
invitee_fname, invitee_lname, invitee_email,
invitee_accessLevel,
""");
sesh = auth.getSesh();
assert auth.validateAccessLevel("admin", sesh.user);
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,
accessLevel = jdata.invitee_accessLevel,
);
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, # Allow re-inviting w/ updated name.
"lname": jdata.invitee_lname,
"inviterId": inviter._id, # <-- Here, we're updating to latest inviter's id.
"hVeriCode": utils.hashPw(newVeriCode), # <-- On reinvite, new veriCode is gen'd, prev one expires.
"accessLevel": jdata.invitee_accessLevel,
});
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, pw, veriCode");
user = getUnverifiedUserByVeriCode(j.userId, j.veriCode);
assert all([
user._id == j.userId, user.email == j.email,
user.fname == j.fname, user.lname == j.lname,
utils.checkPw(j.veriCode, user.hVeriCode),
user.hpw == "",
]);
user.update({ # In-memory update.
"isVerified": True,
"hpw": utils.hashPw(j.pw),
});
userMod.replaceUser(user);
return auth.sendAuthSuccessResponse(user);
@app.post("/userCon/toggleUser_isDeactivated")
def post_userCon_toggleUser_isDeactivated ():
jdata = bu.get_jdata(ensure="thatUserId, preToggle_isDeactivated");
sesh = auth.getSesh();
assert auth.validateAccessLevel("admin", sesh.user);
thatUser = userMod.getUser({
"_id": jdata.thatUserId,
"isDeactivated": jdata.preToggle_isDeactivated, # This helps ensure that we don't accidently perform the opposite op.
# No "isVerified" condition. # <-- This is as unverified (invited/pre-joined) users can be deactivated.
});
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.isRootAdmin:
assert not thatUser.isDeactivated; # Assert active. Root-admin user cannot be deactivated.
return bu.abort("Can't de/reactivate root admin.");
# ==> `thatUser` is NOT the root 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