def post(cls, token: str): data = request.get_json() user = UserModel.find_by_token_2fa(token) if user: response = EmailSecondFA.check_2fa_code(token, data["code"]) if response: password_salt, password_hash = PassCrypt.generate_password_hash( data["new_password"]) user.password_salt = password_salt user.password_hash = password_hash user.token_2fa = None user.session_key = None user.save_to_db() EmailSecondFA.force_revoke_2fa_code(token) return {"message": response_quote("user_password_reset")}, 201 return {"message": response_quote("email2fa_failed")}, 401 return {"message": response_quote("code_404")}, 404
def post(cls): data = request.get_json() if UserModel.find_by_email(data['email']): return {"message": response_quote("user_email_taken")}, 400 password_salt, password_hash = PassCrypt.generate_password_hash(data["password"]) superuser = UserModel( username=data["username"], email=data["email"], password_hash=password_hash, password_salt=password_salt ) superuser.save_to_db() confirmation = ConfirmationModel(superuser.id) confirmation.confirmed = True confirmation.save_to_db() return {"message": response_quote("user_been_created")}, 201
def post(cls): data = request.get_json() if UserModel.find_by_email(data["email"]): return {"message": response_quote("user_email_taken")}, 400 password_salt, password_hash = PassCrypt.generate_password_hash( data["password"]) user = UserModel(username=data["username"], password_hash=password_hash, password_salt=password_salt, email=data["email"]) try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.confirm() return {"message": response_quote("user_been_created")}, 201 except MailGunException as e: user.delete_from_db() # rollback return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {"message": response_quote("operation_fatal_error")}, 500
def post(cls): data = request.get_json() user = UserModel.find_by_email(data["email"]) if user and PassCrypt.check_password_hash( user.password_hash, user.password_salt, data["password"]): confirmation = user.most_recent_confirmation if confirmation and confirmation.confirmed: # в ключ сессии закладывается текущее время сервера во время авторизации. user.session_key = hashlib.sha256( str.encode(str(datetime.datetime.now()))).hexdigest() user.save_to_db() access_token = create_access_token(identity=user.session_key, expires_delta=EXPIRES_DELTA) refresh_token = create_refresh_token(identity=user.session_key) if user.second_fa_enabled: try: token = hashlib.sha256(str.encode( user.email)).hexdigest() code = EmailSecondFA.generate_2fa_code(token) user.token_2fa = token user.session_key = None user.save_to_db() user.send_email_2fa_code(code) return {"verification_token": token}, 202 except MailGunException as e: return {"message": str(e)} return { "access_token": access_token, "refresh_token": refresh_token }, 201 else: return { "message": response_quote("user_not_confirmed").format(user.username) }, 400 else: return {"message": response_quote("user_invalid_credentials")}, 401