Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

login.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from flask_restx import Resource, reqparse
  2. from jwt import InvalidTokenError # type: ignore
  3. import services
  4. from controllers.console.auth.error import (
  5. AuthenticationFailedError,
  6. EmailCodeError,
  7. InvalidEmailError,
  8. )
  9. from controllers.console.error import AccountBannedError
  10. from controllers.console.wraps import only_edition_enterprise, setup_required
  11. from controllers.web import api
  12. from libs.helper import email
  13. from libs.password import valid_password
  14. from services.account_service import AccountService
  15. from services.webapp_auth_service import WebAppAuthService
  16. class LoginApi(Resource):
  17. """Resource for web app email/password login."""
  18. @setup_required
  19. @only_edition_enterprise
  20. def post(self):
  21. """Authenticate user and login."""
  22. parser = reqparse.RequestParser()
  23. parser.add_argument("email", type=email, required=True, location="json")
  24. parser.add_argument("password", type=valid_password, required=True, location="json")
  25. args = parser.parse_args()
  26. try:
  27. account = WebAppAuthService.authenticate(args["email"], args["password"])
  28. except services.errors.account.AccountLoginError:
  29. raise AccountBannedError()
  30. except services.errors.account.AccountPasswordError:
  31. raise AuthenticationFailedError()
  32. except services.errors.account.AccountNotFoundError:
  33. raise AuthenticationFailedError()
  34. token = WebAppAuthService.login(account=account)
  35. return {"result": "success", "data": {"access_token": token}}
  36. # class LogoutApi(Resource):
  37. # @setup_required
  38. # def get(self):
  39. # account = cast(Account, flask_login.current_user)
  40. # if isinstance(account, flask_login.AnonymousUserMixin):
  41. # return {"result": "success"}
  42. # flask_login.logout_user()
  43. # return {"result": "success"}
  44. class EmailCodeLoginSendEmailApi(Resource):
  45. @setup_required
  46. @only_edition_enterprise
  47. def post(self):
  48. parser = reqparse.RequestParser()
  49. parser.add_argument("email", type=email, required=True, location="json")
  50. parser.add_argument("language", type=str, required=False, location="json")
  51. args = parser.parse_args()
  52. if args["language"] is not None and args["language"] == "zh-Hans":
  53. language = "zh-Hans"
  54. else:
  55. language = "en-US"
  56. account = WebAppAuthService.get_user_through_email(args["email"])
  57. if account is None:
  58. raise AuthenticationFailedError()
  59. else:
  60. token = WebAppAuthService.send_email_code_login_email(account=account, language=language)
  61. return {"result": "success", "data": token}
  62. class EmailCodeLoginApi(Resource):
  63. @setup_required
  64. @only_edition_enterprise
  65. def post(self):
  66. parser = reqparse.RequestParser()
  67. parser.add_argument("email", type=str, required=True, location="json")
  68. parser.add_argument("code", type=str, required=True, location="json")
  69. parser.add_argument("token", type=str, required=True, location="json")
  70. args = parser.parse_args()
  71. user_email = args["email"]
  72. token_data = WebAppAuthService.get_email_code_login_data(args["token"])
  73. if token_data is None:
  74. raise InvalidTokenError()
  75. if token_data["email"] != args["email"]:
  76. raise InvalidEmailError()
  77. if token_data["code"] != args["code"]:
  78. raise EmailCodeError()
  79. WebAppAuthService.revoke_email_code_login_token(args["token"])
  80. account = WebAppAuthService.get_user_through_email(user_email)
  81. if not account:
  82. raise AuthenticationFailedError()
  83. token = WebAppAuthService.login(account=account)
  84. AccountService.reset_login_error_rate_limit(args["email"])
  85. return {"result": "success", "data": {"access_token": token}}
  86. api.add_resource(LoginApi, "/login")
  87. # api.add_resource(LogoutApi, "/logout")
  88. api.add_resource(EmailCodeLoginSendEmailApi, "/email-code-login")
  89. api.add_resource(EmailCodeLoginApi, "/email-code-login/validity")