Compare commits

..

No commits in common. "3aaa14c98603752a1c45a07de271a8d13e31f92e" and "4fb09e8fd747688baccd7fb6c61bae2708c8c89a" have entirely different histories.

3 changed files with 32 additions and 43 deletions

View File

@ -4,12 +4,11 @@ from flask import request, abort
from flasgger import swag_from from flasgger import swag_from
from app.doc.user_swag import login_swagger, logout_swagger, delete_swagger, register_swagger from app.doc.user_swag import login_swagger, logout_swagger, delete_swagger
from app.services.user_service import UserService from app.services.user_service import UserService
@bp_user.route('/register', methods=['POST']) @bp_user.route('/register', methods=['POST'])
@swag_from(register_swagger)
def register(): def register():
username = request.json.get('username') username = request.json.get('username')
displayname = request.json.get('displayname') displayname = request.json.get('displayname')
@ -41,9 +40,13 @@ def login():
@jwt_required() @jwt_required()
def logout(): def logout():
jwt = get_jwt() jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
user_id = get_jwt_identity() user_id = get_jwt_identity()
result, status_code = UserService.logout(jwt, user_id) result, status_code = UserService.logout(jti, exp, user_id)
return result, status_code return result, status_code
@ -59,7 +62,10 @@ def update_username():
result, status_code = UserService.update_username(user_id, new_username) result, status_code = UserService.update_username(user_id, new_username)
jwt = get_jwt() jwt = get_jwt()
UserService.logout(jwt, user_id)
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
return result, status_code return result, status_code
@ -75,7 +81,10 @@ def update_displayname():
result, status_code = UserService.update_username(user_id, new_displayname) result, status_code = UserService.update_username(user_id, new_displayname)
jwt = get_jwt() jwt = get_jwt()
UserService.logout(jwt, user_id)
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
return result, status_code return result, status_code
@ -91,7 +100,10 @@ def update_email():
result, status_code = UserService.update_email(username, new_mail) result, status_code = UserService.update_email(username, new_mail)
jwt = get_jwt() jwt = get_jwt()
UserService.logout(jwt, username)
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, username)
return result, status_code return result, status_code
@ -108,7 +120,10 @@ def update_password():
result, status_code = UserService.update_password(username, new_password) result, status_code = UserService.update_password(username, new_password)
jwt = get_jwt() jwt = get_jwt()
UserService.logout(jwt, username)
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, username)
return result, status_code return result, status_code
@ -121,6 +136,9 @@ def delete_user():
result, status_code = UserService.delete_user(user_id) result, status_code = UserService.delete_user(user_id)
jwt = get_jwt() jwt = get_jwt()
UserService.logout(jwt, user_id)
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
return result, status_code return result, status_code

View File

@ -1,29 +1,3 @@
register_swagger = {
"methods": ["POST"],
"tags": ["User"],
"description": "Registers a new user in the app. Also sends a notification to the user via the provided email",
"parameters":
[
{
"in": "body",
"name": "body",
"description": "Username, displayname and password of the new user\n- Username can be only lowercase and up to 64 characters\n- Displayname can contain special characters (. _ -) and lower and upper characters\n- Password must be at least 8 characters long, contain both lower and upper characters, numbers and special characters\n- Email has to be in format \"name@domain.tld\" and up to 64 characters long in total",
"required": True,
"schema":
{
"type": "object",
"properties":
{
"username": {"type": "string", "example": "mycoolusername"},
"email": {"type": "string", "example": "mymail@dot.com"},
"displayname": {"type": "string", "example": "MyCoolDisplayName"},
"password": {"type": "string", "example": "My5tr0ngP@55w0rd"}
}
}
}
],
}
login_swagger = { login_swagger = {
"methods": ["POST"], "methods": ["POST"],
"tags": ["User"], "tags": ["User"],
@ -95,7 +69,7 @@ delete_swagger = {
{ {
"200": "200":
{ {
"description": "User successfully deleted" "description": "User successfully deleted out"
} }
} }
} }

View File

@ -62,7 +62,7 @@ class UserService:
print(f"Error: {e}") print(f"Error: {e}")
return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500 return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500
UserService.__send_email("register", email=email) send_mail("register", email)
return {"Success": "User created successfully"}, 200 return {"Success": "User created successfully"}, 200
@ -98,7 +98,7 @@ class UserService:
token = create_access_token(identity=user_id, expires_delta=expire) token = create_access_token(identity=user_id, expires_delta=expire)
UserService.__send_email("login", email=email) send_mail("login", email)
return {"token": token}, 200 return {"token": token}, 200
@ -106,7 +106,7 @@ class UserService:
return {"Failed": f"Failed to login. Error: {e}"}, 500 return {"Failed": f"Failed to login. Error: {e}"}, 500
@staticmethod @staticmethod
def logout(jwt_token, user_id) -> Tuple[Union[dict, str], int]: def logout(jti, exp, user_id) -> Tuple[Union[dict, str], int]:
""" """
Logs out a user by invalidating the provided JWT. Logs out a user by invalidating the provided JWT.
@ -118,9 +118,6 @@ class UserService:
:rtype: Tuple[Union[dict, str], int] :rtype: Tuple[Union[dict, str], int]
""" """
jti = jwt['jti']
exp = jwt['exp']
UserService.__invalidate_token(jti, exp) UserService.__invalidate_token(jti, exp)
UserService.__send_email("logout", id=user_id) UserService.__send_email("logout", id=user_id)
@ -237,7 +234,7 @@ class UserService:
cursor.execute("select email from user where username = %s", (username,)) cursor.execute("select email from user where username = %s", (username,))
result = cursor.fetchone() result = cursor.fetchone()
email = result['email'] email = result['email']
send_mail(message, email) send_mail("logout", email)
except Error as e: except Error as e:
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500 return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
@ -249,7 +246,7 @@ class UserService:
cursor.execute("select email from user where id = %s", (id,)) cursor.execute("select email from user where id = %s", (id,))
result = cursor.fetchone() result = cursor.fetchone()
email = result['email'] email = result['email']
send_mail(message, email) send_mail("logout", email)
except Error as e: except Error as e:
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500 return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500