Browse Source

更新 SMTP 认证错误处理逻辑

更新了 SMTP 错误码的判断条件,增加了更多错误码的处理,并在用户认证失败时提供了更详细的表单错误信息。同时,在默认错误处理中,改进了错误渲染方式,直接使用错误信息进行渲染。
SongZihuan 1 month ago
parent
commit
b9cf06c505
2 changed files with 5 additions and 4 deletions
  1. 3 2
      internal/auth/smtp/provider.go
  2. 2 2
      internal/route/user/auth.go

+ 3 - 2
internal/auth/smtp/provider.go

@@ -66,8 +66,9 @@ func (p *Provider) Authenticate(login, password string) (*auth.ExternalAccount,
 
 		// Check standard error format first, then fallback to the worse case.
 		tperr, ok := err.(*textproto.Error)
-		if (ok && tperr.Code == 535) ||
-			strings.Contains(err.Error(), "Username and Password not accepted") {
+		if (ok && (tperr.Code == 526 || tperr.Code == 530 || tperr.Code == 534 || tperr.Code == 535 || tperr.Code == 536)) ||
+			strings.Contains(err.Error(), "Username and Password not accepted") ||
+			strings.Contains(err.Error(), "Authentication failure") {
 			return nil, auth.ErrBadCredentials{Args: map[string]any{"login": login}}
 		}
 		return nil, err

+ 2 - 2
internal/route/user/auth.go

@@ -171,9 +171,9 @@ func LoginPost(c *context.Context, f form.SignIn) {
 		case database.IsErrLoginSourceMismatch(err):
 			c.FormErr("LoginSource")
 			c.RenderWithErr(c.Tr("form.auth_source_mismatch"), LOGIN, &f)
-
 		default:
-			c.Error(err, "authenticate user")
+			c.FormErr("UserName", "Password", "LoginSource")
+			c.RenderWithErr(err.Error(), LOGIN, &f)
 		}
 		for i := range loginSources {
 			if loginSources[i].IsDefault {