Skip to content

Commit 82ae2d5

Browse files
authored
chore(handles/auth): improve error response (#2148)
* chore(handles/auth): improve error response Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * Apply suggestion from @xrgzs Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent db0e2ec commit 82ae2d5

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

internal/model/user.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ const (
2020
ADMIN
2121
)
2222

23-
const StaticHashSalt = "https://github.com/alist-org/alist"
23+
const (
24+
StaticHashSalt = "https://github.com/alist-org/alist"
25+
26+
InvalidUsernameOrPassword = "Invalid username or password"
27+
Invalid2FACode = "Invalid 2FA code"
28+
TooManyAttempts = "Too many unsuccessful sign-in attempts have been made using an incorrect username or password, Try again later."
29+
GuestCannotUpdateProfile = "Guest user can not update profile"
30+
GuestCannotGenerate2FA = "Guest user can not generate 2FA code"
31+
)
2432

2533
var LoginCache = cache.NewMemCache[int]()
2634

server/handles/auth.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,36 @@ func loginHash(c *gin.Context, req *LoginReq) {
4545
ip := c.ClientIP()
4646
count, ok := model.LoginCache.Get(ip)
4747
if ok && count >= model.DefaultMaxAuthRetries {
48-
common.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect username or password, Try again later.", 429)
48+
common.ErrorStrResp(c, model.TooManyAttempts, 429)
4949
model.LoginCache.Expire(ip, model.DefaultLockDuration)
5050
return
5151
}
5252
// check username
5353
user, err := op.GetUserByName(req.Username)
5454
if err != nil {
55-
common.ErrorResp(c, err, 400)
55+
common.ErrorStrResp(c, model.InvalidUsernameOrPassword, 401)
5656
model.LoginCache.Set(ip, count+1)
5757
return
5858
}
5959
// validate password hash
6060
if err := user.ValidatePwdStaticHash(req.Password); err != nil {
61-
common.ErrorResp(c, err, 400)
61+
common.ErrorStrResp(c, model.InvalidUsernameOrPassword, 401)
6262
model.LoginCache.Set(ip, count+1)
6363
return
6464
}
6565
// check 2FA
6666
if user.OtpSecret != "" {
6767
if !totp.Validate(req.OtpCode, user.OtpSecret) {
68-
common.ErrorStrResp(c, "Invalid 2FA code", 402)
68+
// 402 - need opt
69+
common.ErrorStrResp(c, model.Invalid2FACode, 402)
6970
model.LoginCache.Set(ip, count+1)
7071
return
7172
}
7273
}
7374
// generate token
7475
token, err := common.GenerateToken(user)
7576
if err != nil {
76-
common.ErrorResp(c, err, 400, true)
77+
common.ErrorResp(c, err, 500, true)
7778
return
7879
}
7980
common.SuccessResp(c, gin.H{"token": token})
@@ -107,7 +108,7 @@ func UpdateCurrent(c *gin.Context) {
107108
}
108109
user := c.Request.Context().Value(conf.UserKey).(*model.User)
109110
if user.IsGuest() {
110-
common.ErrorStrResp(c, "Guest user can not update profile", 403)
111+
common.ErrorStrResp(c, model.GuestCannotUpdateProfile, 403)
111112
return
112113
}
113114
user.Username = req.Username
@@ -125,7 +126,7 @@ func UpdateCurrent(c *gin.Context) {
125126
func Generate2FA(c *gin.Context) {
126127
user := c.Request.Context().Value(conf.UserKey).(*model.User)
127128
if user.IsGuest() {
128-
common.ErrorStrResp(c, "Guest user can not generate 2FA code", 403)
129+
common.ErrorStrResp(c, model.GuestCannotGenerate2FA, 403)
129130
return
130131
}
131132
key, err := totp.Generate(totp.GenerateOpts{
@@ -164,11 +165,11 @@ func Verify2FA(c *gin.Context) {
164165
}
165166
user := c.Request.Context().Value(conf.UserKey).(*model.User)
166167
if user.IsGuest() {
167-
common.ErrorStrResp(c, "Guest user can not generate 2FA code", 403)
168+
common.ErrorStrResp(c, model.GuestCannotGenerate2FA, 403)
168169
return
169170
}
170171
if !totp.Validate(req.Code, req.Secret) {
171-
common.ErrorStrResp(c, "Invalid 2FA code", 400)
172+
common.ErrorStrResp(c, model.Invalid2FACode, 400)
172173
return
173174
}
174175
user.OtpSecret = req.Secret

0 commit comments

Comments
 (0)