Skip to content

Commit 69bdcf4

Browse files
GiteaBotlunny
andauthored
Log the real reason when authentication fails (but don't show the user) (#25414) (#25660)
Backport #25414 by @lunny Fix #24498 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent e610b03 commit 69bdcf4

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

routers/web/auth/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func SignInPost(ctx *context.Context) {
201201

202202
u, source, err := auth_service.UserSignIn(form.UserName, form.Password)
203203
if err != nil {
204-
if user_model.IsErrUserNotExist(err) || user_model.IsErrEmailAddressNotExist(err) {
204+
if errors.Is(err, util.ErrNotExist) || errors.Is(err, util.ErrInvalidArgument) {
205205
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form)
206206
log.Info("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err)
207207
} else if user_model.IsErrEmailAlreadyUsed(err) {

routers/web/auth/linkaccount.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/base"
1515
"code.gitea.io/gitea/modules/context"
16+
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/util"
1719
"code.gitea.io/gitea/modules/web"
1820
auth_service "code.gitea.io/gitea/services/auth"
1921
"code.gitea.io/gitea/services/auth/source/oauth2"
@@ -81,6 +83,32 @@ func LinkAccount(ctx *context.Context) {
8183
ctx.HTML(http.StatusOK, tplLinkAccount)
8284
}
8385

86+
func handleSignInError(ctx *context.Context, userName string, ptrForm any, tmpl base.TplName, invoker string, err error) {
87+
if errors.Is(err, util.ErrNotExist) {
88+
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tmpl, ptrForm)
89+
} else if errors.Is(err, util.ErrInvalidArgument) {
90+
ctx.Data["user_exists"] = true
91+
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tmpl, ptrForm)
92+
} else if user_model.IsErrUserProhibitLogin(err) {
93+
ctx.Data["user_exists"] = true
94+
log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err)
95+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
96+
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
97+
} else if user_model.IsErrUserInactive(err) {
98+
ctx.Data["user_exists"] = true
99+
if setting.Service.RegisterEmailConfirm {
100+
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
101+
ctx.HTML(http.StatusOK, TplActivate)
102+
} else {
103+
log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err)
104+
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
105+
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
106+
}
107+
} else {
108+
ctx.ServerError(invoker, err)
109+
}
110+
}
111+
84112
// LinkAccountPostSignIn handle the coupling of external account with another account using signIn
85113
func LinkAccountPostSignIn(ctx *context.Context) {
86114
signInForm := web.GetForm(ctx).(*forms.SignInForm)
@@ -116,12 +144,7 @@ func LinkAccountPostSignIn(ctx *context.Context) {
116144

117145
u, _, err := auth_service.UserSignIn(signInForm.UserName, signInForm.Password)
118146
if err != nil {
119-
if user_model.IsErrUserNotExist(err) {
120-
ctx.Data["user_exists"] = true
121-
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm)
122-
} else {
123-
ctx.ServerError("UserLinkAccount", err)
124-
}
147+
handleSignInError(ctx, signInForm.UserName, &signInForm, tplLinkAccount, "UserLinkAccount", err)
125148
return
126149
}
127150

routers/web/auth/openid.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,7 @@ func ConnectOpenIDPost(ctx *context.Context) {
282282

283283
u, _, err := auth.UserSignIn(form.UserName, form.Password)
284284
if err != nil {
285-
if user_model.IsErrUserNotExist(err) {
286-
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplConnectOID, &form)
287-
} else {
288-
ctx.ServerError("ConnectOpenIDPost", err)
289-
}
285+
handleSignInError(ctx, form.UserName, &form, tplConnectOID, "ConnectOpenIDPost", err)
290286
return
291287
}
292288

services/auth/source/db/authenticate.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,54 @@
44
package db
55

66
import (
7+
"fmt"
8+
79
"code.gitea.io/gitea/models/db"
810
user_model "code.gitea.io/gitea/models/user"
911
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/util"
1013
)
1114

15+
// ErrUserPasswordNotSet represents a "ErrUserPasswordNotSet" kind of error.
16+
type ErrUserPasswordNotSet struct {
17+
UID int64
18+
Name string
19+
}
20+
21+
func (err ErrUserPasswordNotSet) Error() string {
22+
return fmt.Sprintf("user's password isn't set [uid: %d, name: %s]", err.UID, err.Name)
23+
}
24+
25+
// Unwrap unwraps this error as a ErrInvalidArgument error
26+
func (err ErrUserPasswordNotSet) Unwrap() error {
27+
return util.ErrInvalidArgument
28+
}
29+
30+
// ErrUserPasswordInvalid represents a "ErrUserPasswordInvalid" kind of error.
31+
type ErrUserPasswordInvalid struct {
32+
UID int64
33+
Name string
34+
}
35+
36+
func (err ErrUserPasswordInvalid) Error() string {
37+
return fmt.Sprintf("user's password is invalid [uid: %d, name: %s]", err.UID, err.Name)
38+
}
39+
40+
// Unwrap unwraps this error as a ErrInvalidArgument error
41+
func (err ErrUserPasswordInvalid) Unwrap() error {
42+
return util.ErrInvalidArgument
43+
}
44+
1245
// Authenticate authenticates the provided user against the DB
1346
func Authenticate(user *user_model.User, login, password string) (*user_model.User, error) {
1447
if user == nil {
1548
return nil, user_model.ErrUserNotExist{Name: login}
1649
}
1750

18-
if !user.IsPasswordSet() || !user.ValidatePassword(password) {
19-
return nil, user_model.ErrUserNotExist{UID: user.ID, Name: user.Name}
51+
if !user.IsPasswordSet() {
52+
return nil, ErrUserPasswordNotSet{UID: user.ID, Name: user.Name}
53+
} else if !user.ValidatePassword(password) {
54+
return nil, ErrUserPasswordInvalid{UID: user.ID, Name: user.Name}
2055
}
2156

2257
// Update password hash if server password hash algorithm have changed

0 commit comments

Comments
 (0)