2018-01-01 11:10:39 +01:00
|
|
|
import re
|
2021-02-20 21:37:31 +01:00
|
|
|
from typing import Optional, Tuple
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2021-02-20 21:37:31 +01:00
|
|
|
from flask import Request
|
2021-01-20 16:47:00 +01:00
|
|
|
|
2021-01-01 16:39:25 +01:00
|
|
|
from fittrackee.responses import (
|
|
|
|
ForbiddenErrorResponse,
|
2021-01-02 19:28:03 +01:00
|
|
|
HttpResponse,
|
2021-01-01 16:39:25 +01:00
|
|
|
UnauthorizedErrorResponse,
|
|
|
|
)
|
2017-12-25 17:45:28 +01:00
|
|
|
|
|
|
|
from .models import User
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def is_valid_email(email: str) -> bool:
|
|
|
|
"""
|
|
|
|
Return if email format is valid
|
|
|
|
"""
|
2018-05-09 16:50:30 +02:00
|
|
|
mail_pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
|
|
|
|
return re.match(mail_pattern, email) is not None
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def check_passwords(password: str, password_conf: str) -> str:
|
|
|
|
"""
|
|
|
|
Verify if password and password confirmation are the same and have
|
|
|
|
more than 8 characters
|
|
|
|
|
|
|
|
If not, it returns not empty string
|
|
|
|
"""
|
2020-05-17 16:42:44 +02:00
|
|
|
ret = ''
|
|
|
|
if password_conf != password:
|
2021-11-01 09:44:10 +01:00
|
|
|
ret = 'password: password and password confirmation do not match\n'
|
2020-05-17 16:42:44 +02:00
|
|
|
if len(password) < 8:
|
2021-11-01 09:44:10 +01:00
|
|
|
ret += 'password: 8 characters required\n'
|
2020-05-17 16:42:44 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def register_controls(
|
|
|
|
username: str, email: str, password: str, password_conf: str
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Verify if user name, email and passwords are valid
|
|
|
|
|
|
|
|
If not, it returns not empty string
|
|
|
|
"""
|
2018-05-09 17:00:22 +02:00
|
|
|
ret = ''
|
|
|
|
if not 2 < len(username) < 13:
|
2021-11-01 09:44:10 +01:00
|
|
|
ret += 'username: 3 to 12 characters required\n'
|
2018-05-09 17:00:22 +02:00
|
|
|
if not is_valid_email(email):
|
2021-11-01 09:44:10 +01:00
|
|
|
ret += 'email: valid email must be provided\n'
|
2020-05-17 16:42:44 +02:00
|
|
|
ret += check_passwords(password, password_conf)
|
2018-05-09 17:00:22 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def verify_user(
|
|
|
|
current_request: Request, verify_admin: bool
|
2021-12-01 19:22:47 +01:00
|
|
|
) -> Tuple[Optional[HttpResponse], Optional[User]]:
|
2021-01-02 19:28:03 +01:00
|
|
|
"""
|
2021-12-01 19:39:45 +01:00
|
|
|
Return authenticated user, if the provided token is valid and user has
|
|
|
|
admin rights if 'verify_admin' is True
|
2021-01-02 19:28:03 +01:00
|
|
|
"""
|
2021-11-01 09:44:10 +01:00
|
|
|
default_message = 'provide a valid auth token'
|
2018-05-09 17:00:22 +02:00
|
|
|
auth_header = current_request.headers.get('Authorization')
|
|
|
|
if not auth_header:
|
2021-01-01 16:39:25 +01:00
|
|
|
return UnauthorizedErrorResponse(default_message), None
|
|
|
|
auth_token = auth_header.split(' ')[1]
|
2018-05-09 17:00:22 +02:00
|
|
|
resp = User.decode_auth_token(auth_token)
|
|
|
|
if isinstance(resp, str):
|
2021-01-01 16:39:25 +01:00
|
|
|
return UnauthorizedErrorResponse(resp), None
|
2018-05-09 17:00:22 +02:00
|
|
|
user = User.query.filter_by(id=resp).first()
|
|
|
|
if not user:
|
2021-01-01 16:39:25 +01:00
|
|
|
return UnauthorizedErrorResponse(default_message), None
|
2021-12-01 19:22:47 +01:00
|
|
|
if verify_admin and not user.admin:
|
2021-01-01 16:39:25 +01:00
|
|
|
return ForbiddenErrorResponse(), None
|
2021-12-01 19:22:47 +01:00
|
|
|
return None, user
|
2018-05-09 17:00:22 +02:00
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
def can_view_workout(
|
|
|
|
auth_user_id: int, workout_user_id: int
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Optional[HttpResponse]:
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Return error response if user has no right to view workout
|
2021-01-02 19:28:03 +01:00
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
if auth_user_id != workout_user_id:
|
2021-01-01 16:39:25 +01:00
|
|
|
return ForbiddenErrorResponse()
|
|
|
|
return None
|