You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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