def post(cls, user_id: int): """Resend confirmation email""" response_object = {"status": "fail", "message": "User Not found"} user = UserModel.find_by_id(_id=user_id) if not user: return response_object, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: response_object["message"] = "Already confirmed" return response_object, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_mail() response_object["status"] = "success" response_object[ "message"] = "Email confirmation successfully resent" return response_object, 201 except MailGunException as e: response_object["message"] = str(e) user.delete_from_db() return response_object, 500 except Exception: traceback.print_exc() user.delete_from_db() response_object[ "message"] = "Internal Server Error. Failed to resend confirmation email" return response_object, 500
def refresh_user_token(): response_object = { "status": "fail", "message": "Provide a valid refresh token." } auth_header = request.headers.get("Authorization") if not auth_header: return jsonify(response_object), 403 refresh_token = auth_header.split(" ")[1] try: resp = UserModel.decode_token(refresh_token) user = UserModel.find_by_id(resp) if not user: response_object["status"] = "fail" response_object["message"] = "Invalid token" return jsonify(response_object), 401 new_access_token = user.encode_token(user.id, "access") new_refresh_token = user.encode_token(user.id, "refresh") response_object = { "status": "success", "access_token": new_access_token.decode(), "refresh_token": new_refresh_token.decode(), } return response_object, 200 except jwt.ExpiredSignatureError: response_object["status"] = "fail" response_object["message"] = "Signature expired. Please log in again." return jsonify(response_object), 401 except jwt.InvalidTokenError: response_object["status"] = "fail" response_object["message"] = "Invalid token. Please log in again.." return jsonify(response_object), 401
def get(cls, user_id: int): """Returns confirmation for specific user""" response_object = {"status": "fail"} user = UserModel.find_by_id(_id=user_id) if not user: return response_object, 404 else: response_object["status"] = "success" response_object["current_time"] = int(time()) response_object["confirmation"] = [ each.json() for each in user.confirmation.order_by( ConfirmationModel.expire_at) ] return response_object, 200
def decorated_function(*args, **kwargs): response_object = { "status": "fail", "message": "Provide a valid auth token." } auth_header = request.headers.get("Authorization") if not auth_header: return response_object, 403 auth_token = auth_header.split(" ")[1] resp = UserModel.decode_token(auth_token) if isinstance(resp, str): response_object["message"] = resp return response_object, 401 user = UserModel.find_by_id(_id=resp) if not user: return response_object, 401 confirmation = user.most_recent_confirmation if not confirmation or not confirmation.confirmed: response_object[ "message"] = "You have not confirmed registration. Please check your email." return response_object, 401 return f(resp, *args, **kwargs)