ran reformat

pull/22390/head
Yansong Zhang 11 months ago
parent f365677fd5
commit 227eac79c8

@ -30,11 +30,13 @@ class PasswordResetRateLimitExceededError(BaseHTTPException):
description = "Too many password reset emails have been sent. Please try again in 1 minutes." description = "Too many password reset emails have been sent. Please try again in 1 minutes."
code = 429 code = 429
class EmailChangeRateLimitExceededError(BaseHTTPException): class EmailChangeRateLimitExceededError(BaseHTTPException):
error_code = "email_change_rate_limit_exceeded" error_code = "email_change_rate_limit_exceeded"
description = "Too many email change emails have been sent. Please try again in 1 minutes." description = "Too many email change emails have been sent. Please try again in 1 minutes."
code = 429 code = 429
class EmailCodeError(BaseHTTPException): class EmailCodeError(BaseHTTPException):
error_code = "email_code_error" error_code = "email_code_error"
description = "Email code is invalid or expired." description = "Email code is invalid or expired."
@ -70,11 +72,13 @@ class EmailPasswordResetLimitError(BaseHTTPException):
description = "Too many failed password reset attempts. Please try again in 24 hours." description = "Too many failed password reset attempts. Please try again in 24 hours."
code = 429 code = 429
class EmailChangeLimitError(BaseHTTPException): class EmailChangeLimitError(BaseHTTPException):
error_code = "email_change_limit" error_code = "email_change_limit"
description = "Too many failed email change attempts. Please try again in 24 hours." description = "Too many failed email change attempts. Please try again in 24 hours."
code = 429 code = 429
class EmailAlreadyInUseError(BaseHTTPException): class EmailAlreadyInUseError(BaseHTTPException):
error_code = "email_already_in_use" error_code = "email_already_in_use"
description = "A user with this email already exists." description = "A user with this email already exists."

@ -11,11 +11,11 @@ from configs import dify_config
from constants.languages import supported_language from constants.languages import supported_language
from controllers.console import api from controllers.console import api
from controllers.console.auth.error import ( from controllers.console.auth.error import (
EmailAlreadyInUseError,
EmailChangeLimitError, EmailChangeLimitError,
EmailCodeError, EmailCodeError,
InvalidEmailError, InvalidEmailError,
InvalidTokenError, InvalidTokenError,
EmailAlreadyInUseError,
) )
from controllers.console.error import AccountNotFound, EmailSendIpLimitError from controllers.console.error import AccountNotFound, EmailSendIpLimitError
from controllers.console.workspace.error import ( from controllers.console.workspace.error import (
@ -380,6 +380,7 @@ class EducationAutoCompleteApi(Resource):
return BillingService.EducationIdentity.autocomplete(args["keywords"], args["page"], args["limit"]) return BillingService.EducationIdentity.autocomplete(args["keywords"], args["page"], args["limit"])
class ChangeEmailSendEmailApi(Resource): class ChangeEmailSendEmailApi(Resource):
@enable_change_email @enable_change_email
@setup_required @setup_required
@ -405,12 +406,12 @@ class ChangeEmailSendEmailApi(Resource):
user_email = args["email"] user_email = args["email"]
if args["phase"] is not None and args["phase"] == "new_email": if args["phase"] is not None and args["phase"] == "new_email":
if args["token"] is None: if args["token"] is None:
raise InvalidTokenError() raise InvalidTokenError()
reset_data = AccountService.get_change_email_data(args["token"]) reset_data = AccountService.get_change_email_data(args["token"])
if reset_data is None: if reset_data is None:
raise InvalidTokenError() raise InvalidTokenError()
user_email = reset_data.get("email","") user_email = reset_data.get("email", "")
if user_email == current_user.email: if user_email == current_user.email:
raise InvalidEmailError() raise InvalidEmailError()
@ -420,7 +421,9 @@ class ChangeEmailSendEmailApi(Resource):
if account is None: if account is None:
raise AccountNotFound() raise AccountNotFound()
token = AccountService.send_change_email_email(account=account, email=args["email"], old_email=user_email, language=language, phase=args["phase"]) # noqa: E501 token = AccountService.send_change_email_email(
account=account, email=args["email"], old_email=user_email, language=language, phase=args["phase"]
) # noqa: E501
return {"result": "success", "data": token} return {"result": "success", "data": token}
@ -458,12 +461,13 @@ class ChangeEmailCheckApi(Resource):
# Refresh token data by generating a new token # Refresh token data by generating a new token
_, new_token = AccountService.generate_change_email_token( _, new_token = AccountService.generate_change_email_token(
user_email, code=args["code"],old_email=token_data.get("old_email"), additional_data={} user_email, code=args["code"], old_email=token_data.get("old_email"), additional_data={}
) )
AccountService.reset_change_email_error_rate_limit(args["email"]) AccountService.reset_change_email_error_rate_limit(args["email"])
return {"is_valid": True, "email": token_data.get("email"), "token": new_token} return {"is_valid": True, "email": token_data.get("email"), "token": new_token}
class ChangeEmailResetApi(Resource): class ChangeEmailResetApi(Resource):
@enable_change_email @enable_change_email
@setup_required @setup_required
@ -493,6 +497,7 @@ class ChangeEmailResetApi(Resource):
return updated_account return updated_account
class CheckEmailUnique(Resource): class CheckEmailUnique(Resource):
@setup_required @setup_required
def post(self): def post(self):
@ -506,6 +511,7 @@ class CheckEmailUnique(Resource):
raise EmailAlreadyInUseError() raise EmailAlreadyInUseError()
return {"result": "success"} return {"result": "success"}
# Register API resources # Register API resources
api.add_resource(AccountInitApi, "/account/init") api.add_resource(AccountInitApi, "/account/init")
api.add_resource(AccountProfileApi, "/account/profile") api.add_resource(AccountProfileApi, "/account/profile")

@ -236,6 +236,7 @@ def email_password_login_enabled(view):
return decorated return decorated
def enable_change_email(view): def enable_change_email(view):
@wraps(view) @wraps(view)
def decorated(*args, **kwargs): def decorated(*args, **kwargs):
@ -245,4 +246,5 @@ def enable_change_email(view):
# otherwise, return 403 # otherwise, return 403
abort(403) abort(403)
return decorated return decorated

@ -76,9 +76,7 @@ class AccountService:
email_code_account_deletion_rate_limiter = RateLimiter( email_code_account_deletion_rate_limiter = RateLimiter(
prefix="email_code_account_deletion_rate_limit", max_attempts=1, time_window=60 * 1 prefix="email_code_account_deletion_rate_limit", max_attempts=1, time_window=60 * 1
) )
change_email_rate_limiter = RateLimiter( change_email_rate_limiter = RateLimiter(prefix="change_email_rate_limit", max_attempts=1, time_window=60 * 1)
prefix="change_email_rate_limit", max_attempts=2, time_window=60 * 1
)
LOGIN_MAX_ERROR_LIMITS = 5 LOGIN_MAX_ERROR_LIMITS = 5
FORGOT_PASSWORD_MAX_ERROR_LIMITS = 5 FORGOT_PASSWORD_MAX_ERROR_LIMITS = 5
CHANGE_EMAIL_MAX_ERROR_LIMITS = 5 CHANGE_EMAIL_MAX_ERROR_LIMITS = 5

@ -25,7 +25,7 @@ def send_change_mail_task(language: str, to: str, code: str, phase: str):
# send change email mail using different languages # send change email mail using different languages
try: try:
if phase == "old_email" : if phase == "old_email":
template = "change_mail_confirm_old_template_en-US.html" template = "change_mail_confirm_old_template_en-US.html"
elif phase == "new_email": elif phase == "new_email":
template = "change_mail_confirm_new_template_en-US.html" template = "change_mail_confirm_new_template_en-US.html"

Loading…
Cancel
Save