@@ -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) {
125126func 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