52 async def post_login(self, request: Request) -> Response:
54 The endpoint allowing a user to log into the server.
57 Response: _description_: The data to send back to the user as a response.
61 self.
disp.log_debug(f
"Request body: {request_body}", title)
62 if not request_body
or not all(key
in request_body
for key
in (
"email",
"password")):
64 email = request_body[
"email"]
65 password = request_body[
"password"]
67 CONST.TAB_ACCOUNTS,
"*", f
"email='{email}'"
69 self.
disp.log_debug(f
"Retrived data: {user_info}", title)
70 if isinstance(user_info, int):
77 if data[
"status"] == self.
error:
80 message=
"Login failed.",
85 return HCI.forbidden(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
86 name = user_info[0][
"username"]
89 message=f
"Welcome {name}",
94 body[
"token"] = data[
"token"]
101 request (Request): _description_
104 Response: _description_
108 self.
disp.log_debug(f
"Request body: {request_body}", title)
109 if not request_body
or not all(key
in request_body
for key
in (
"email",
"password")):
111 email: str = request_body[
"email"]
112 password = request_body[
"password"]
113 if not email
or email ==
"" or not password
or password ==
"":
116 CONST.TAB_ACCOUNTS,
"*", f
"email='{email}'")
117 if isinstance(user_info, int)
is False:
120 message=
"Email already exist.",
125 return HCI.conflict(node)
128 username = email.split(
'@')[0]
129 self.
disp.log_debug(f
"Username = {username}", title)
130 admin = str(int(
False))
132 data = [username, email, hashed_password,
"local", favicon, admin]
133 self.
disp.log_debug(f
"Data list = {data}", title)
137 self.
disp.log_debug(f
"Column = {column}", title)
138 if isinstance(column, int):
141 self.
disp.log_debug(f
"Column after id pop = {column}", title)
147 if data[
"status"] == self.
error:
150 message=
"Login failed.",
155 return HCI.forbidden(content=body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
158 message=f
"Welcome {username}",
163 body[
"token"] = data[
"token"]
166 content_type=CONST.CONTENT_TYPE,
173 title =
"Send e-mail verification"
175 self.
disp.log_debug(f
"Request body: {request_body}", title)
176 if not request_body
or (
"email")
not in request_body:
178 email: str = request_body[
"email"]
180 table=CONST.TAB_ACCOUNTS,
182 where=f
"email='{email}'",
185 self.
disp.log_debug(f
"user query = {data}", title)
186 if data == self.
error or len(data) == 0:
188 email_subject =
"[AREA] Verification code"
190 CONST.CHECK_TOKEN_SIZE
193 CONST.EMAIL_VERIFICATION_DELAY
196 expiration_time,
False
199 new_node[
'email'] = email
200 new_node[
'code'] = code
202 CONST.TAB_VERIFICATION)
203 if tab_column == self.
error or len(tab_column) == 0:
207 CONST.TAB_VERIFICATION,
211 table=CONST.TAB_VERIFICATION,
216 expiration_time,
False,
True
221 if status == self.
error:
223 code_style =
"background-color: lightgray;border: 2px lightgray solid;border-radius: 6px;color: black;font-weight: bold;padding: 5px;padding-top: 5px;padding-bottom: 5px;padding-top: 0px;padding-bottom: 0px;"
225 body +=
"<p>The code is: "
226 body += f
"<span style=\"{code_style}\">{code}</span></p>"
227 body +=
"<p>The code will be valid until "
228 body += f
"<span style=\"{code_style}\">"
229 body += f
"{expiration_time_str}</span>.</p>"
230 self.
disp.log_debug(f
"e-mail body: {body}", title)
232 email, email_subject, body
234 if status == self.
error:
238 message=
"Email send successfully.",
243 return HCI.success(body)
247 The function in charge of resetting the user's password.
249 title =
"Reset password"
251 self.
disp.log_debug(f
"Request body: {request_body}", title)
252 if not request_body
or not all(key
in request_body
for key
in (
"email",
"code",
"password")):
254 body_email: str = request_body[
"email"]
255 body_code: str = request_body[
"code"]
256 body_password: str = request_body[
"password"]
257 verified_user: dict = {}
259 CONST.TAB_VERIFICATION,
261 where=f
"term='{body_email}'",
264 self.
disp.log_debug(f
"Current codes: {current_codes}", title)
265 nodes_of_interest = []
266 if current_codes == self.
error or len(current_codes) == 0:
268 for user
in current_codes:
269 if user.get(
"term") == body_email
and user.get(
"definition") == body_code:
271 nodes_of_interest.append(user)
272 if not verified_user:
279 data.append(hashed_password)
280 column.append(
"password")
282 CONST.TAB_ACCOUNTS, data, column, f
"email='{body_email}'"
284 if status == self.
error:
286 self.
disp.log_debug(f
"Nodes found: {nodes_of_interest}", title)
287 for line
in nodes_of_interest:
288 self.
disp.log_debug(f
"line removed: {line}", title)
290 CONST.TAB_VERIFICATION,
295 message=
"Password changed successfully.",
300 return HCI.success(response_body, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
302 async def put_user(self, request: Request) -> Response:
304 Endpoint allowing the user to update it's account data.
307 request (Request): _description_
310 Response: _description_
319 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
320 if token_valid
is False:
323 self.
disp.log_debug(f
"Request body: {request_body}", title)
324 if not request_body
or not all(key
in request_body
for key
in (
"username",
"email",
"password")):
326 body_username: str = request_body[
"username"]
327 body_email: str = request_body[
"email"]
328 body_password: str = request_body[
"password"]
332 if isinstance(usr_id, Response)
is True:
335 table=CONST.TAB_ACCOUNTS,
337 where=f
"id='{usr_id}'",
339 self.
disp.log_debug(f
"User profile = {user_profile}", title)
340 if user_profile == self.
error or len(user_profile) == 0:
346 user_profile[0][
"method"],
347 user_profile[0][
"favicon"],
348 str(user_profile[0][
"admin"])
353 if isinstance(status, Response)
is True:
357 message=
"The account information has been updated.",
362 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
366 Endpoint allowing the user to update it's account data.
369 request (Request): _description_
372 Response: _description_
381 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
382 if token_valid
is False:
385 self.
disp.log_debug(f
"Request body: {request_body}", title)
386 body_username: str = request_body.get(
"username")
387 body_email: str = request_body.get(
"email")
388 body_password: str = request_body.get(
"password")
392 if isinstance(usr_id, Response)
is True:
395 table=CONST.TAB_ACCOUNTS,
397 where=f
"id='{usr_id}'",
399 self.
disp.log_debug(f
"User profile = {user_profile}", title)
400 if user_profile == self.
error or len(user_profile) == 0:
402 email: str = user_profile[0][
"email"]
403 username: str = user_profile[0][
"username"]
404 password: str = user_profile[0][
"password"]
405 msg = f
"body_username = {body_username}, body_email = {body_email}, "
406 msg += f
"body_password = {body_password}, email = {email}, "
407 msg += f
"username = {username}, password = {password}"
408 self.
disp.log_debug(msg, title)
409 if body_username
is not None:
410 username = body_username
411 self.
disp.log_debug(f
"username is now: {username}", title)
412 if body_email
is not None:
414 self.
disp.log_debug(f
"email is now: {email}", title)
415 if body_password
is not None:
419 self.
disp.log_debug(f
"password is now: {password}", title)
421 username, email, password,
422 user_profile[0][
"method"], user_profile[0][
"favicon"],
423 str(user_profile[0][
"admin"])
428 if isinstance(status, Response)
is True:
432 message=
"The account information has been updated.",
437 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
439 async def get_user(self, request: Request) -> Response:
441 Endpoint allowing the user to get it's account data.
444 request (Request): _description_
447 Response: _description_
456 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
457 if token_valid
is False:
462 self.
disp.log_debug(f
"user_id = {usr_id}", title)
463 if isinstance(usr_id, Response)
is True:
466 table=CONST.TAB_ACCOUNTS,
468 where=f
"id='{usr_id}'",
470 self.
disp.log_debug(f
"User profile = {user_profile}", title)
471 if user_profile == self.
error or len(user_profile) == 0:
473 new_profile = user_profile[0]
474 for i
in CONST.USER_INFO_BANNED:
477 if CONST.USER_INFO_ADMIN_NODE
in new_profile:
478 new_profile[CONST.USER_INFO_ADMIN_NODE] = bool(
479 new_profile[CONST.USER_INFO_ADMIN_NODE]
488 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
492 Endpoint allowing the user to delete it's account.
495 request (Request): _description_
498 Response: _description_
500 title =
"Delete user"
507 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
508 if token_valid
is False:
513 self.
disp.log_debug(f
"user_id = {usr_id}", title)
514 if isinstance(usr_id, Response)
is True:
517 table=CONST.TAB_ACCOUNTS,
519 where=f
"id='{usr_id}'",
521 self.
disp.log_debug(f
"User profile = {user_profile}", title)
522 if user_profile == self.
error or len(user_profile) == 0:
524 tables_of_interest = [
525 CONST.TAB_USER_SERVICES, CONST.TAB_ACTIONS,
526 CONST.TAB_CONNECTIONS, CONST.TAB_ACTIVE_OAUTHS
529 f
"user_id={usr_id}", tables_of_interest
531 if isinstance(removal_status, int)
or self.
error in list(removal_status.values()):
534 CONST.TAB_ACCOUNTS, f
"id={usr_id}"
536 if status == self.
error:
540 message=
"The account has successfully been deleted.",
545 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
571 The endpoint allowing a user to log out of the server.
574 Response: _description_: The data to send back to the user as a response.
583 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
584 if token_valid
is False:
587 CONST.TAB_CONNECTIONS,
590 if response == self.
error:
594 message=
"You have successfully logged out...",
599 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)
603 This is an endpoint that will allow the user to query it's id.
606 request (Request): _description_
609 Response: _description_
611 title =
"Get user id"
618 self.
disp.log_debug(f
"token = {token}, valid = {token_valid}", title)
619 if token_valid
is False:
624 self.
disp.log_debug(f
"user_id = {usr_id}", title)
625 if isinstance(usr_id, Response)
is True:
629 message=f
"Your id is {usr_id}",
634 return HCI.success(content=data, content_type=CONST.CONTENT_TYPE, headers=self.
runtime_data_initialised.json_header)