Compare commits
2 Commits
4fb09e8fd7
...
3aaa14c986
Author | SHA1 | Date | |
---|---|---|---|
3aaa14c986 | |||
628d210156 |
@ -4,11 +4,12 @@ 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
|
from app.doc.user_swag import login_swagger, logout_swagger, delete_swagger, register_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')
|
||||||
@ -40,13 +41,9 @@ 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(jti, exp, user_id)
|
result, status_code = UserService.logout(jwt, user_id)
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@ -62,10 +59,7 @@ 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
|
||||||
|
|
||||||
@ -81,10 +75,7 @@ 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
|
||||||
|
|
||||||
@ -100,10 +91,7 @@ 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
|
||||||
|
|
||||||
@ -120,10 +108,7 @@ 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
|
||||||
|
|
||||||
@ -136,9 +121,6 @@ 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
|
@ -1,3 +1,29 @@
|
|||||||
|
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"],
|
||||||
@ -69,7 +95,7 @@ delete_swagger = {
|
|||||||
{
|
{
|
||||||
"200":
|
"200":
|
||||||
{
|
{
|
||||||
"description": "User successfully deleted out"
|
"description": "User successfully deleted"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
||||||
|
|
||||||
send_mail("register", email)
|
UserService.__send_email("register", email=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)
|
||||||
|
|
||||||
send_mail("login", email)
|
UserService.__send_email("login", email=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(jti, exp, user_id) -> Tuple[Union[dict, str], int]:
|
def logout(jwt_token, 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,6 +118,9 @@ 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)
|
||||||
|
|
||||||
@ -234,7 +237,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("logout", email)
|
send_mail(message, 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
|
||||||
@ -246,7 +249,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("logout", email)
|
send_mail(message, 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user