def generate_tokens(user_id, email, is_admin, token_type): ''' Method that generates access and refresh tokens :param user_id: administrator or physiotherapist id :param email: user email :param is_admin: flag that tells if user is admin or not :param token_type: token type :return: JWT access or refresh token ''' if token_type == ACCESS_TOKEN_TYPE: ttl = ACCESS_TOKEN_EXPIRY_TIME elif token_type == REFRESH_TOKEN_TYPE: ttl = REFRESH_TOKEN_EXPIRY_TIME token_content = { "id": user_id, "email": email, "is_admin": is_admin, "token_type": token_type, 'exp': Utils.get_expire_time(ttl) } return jwt.encode(token_content, JWT_SECRET_KEY).decode('utf8')
def generate_response(admin, physio): ''' Method that generates json response for login and token refresh requests :param admin: Adminstrator object or None :param physio: Physiotherapist object or None :return: json response with access token, refresh token and user information ''' if admin: user = admin is_admin = True elif physio: user = physio is_admin = False else: raise PermissionDenied("Account not found!") if not user['state']['name'] == "active": raise PermissionDenied("The account is not active!") return { "access": { "token": AuthenticationService.generate_tokens(user['id'], user['person']['email'], is_admin, ACCESS_TOKEN_TYPE), "expiredTime": Utils.get_expire_time(ACCESS_TOKEN_EXPIRY_TIME) }, "refresh": { "token": AuthenticationService.generate_tokens(user['id'], user['person']['email'], is_admin, REFRESH_TOKEN_TYPE), "expiredTime": Utils.get_expire_time(REFRESH_TOKEN_EXPIRY_TIME) }, "is_admin": is_admin, "id": user['id'], "first_name": user['person']['first_name'], "last_name": user['person']['last_name'], "email": user['person']['email'] }