perf: sha256 for user's password (close #3552)
This commit is contained in:
@ -28,6 +28,26 @@ type LoginReq struct {
|
||||
|
||||
// Login Deprecated
|
||||
func Login(c *gin.Context) {
|
||||
var req LoginReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
req.Password = model.HashPwd(req.Password)
|
||||
loginHash(c, &req)
|
||||
}
|
||||
|
||||
// LoginHash login with password hashed by sha256
|
||||
func LoginHash(c *gin.Context) {
|
||||
var req LoginReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
loginHash(c, &req)
|
||||
}
|
||||
|
||||
func loginHash(c *gin.Context, req *LoginReq) {
|
||||
// check count of login
|
||||
ip := c.ClientIP()
|
||||
count, ok := loginCache.Get(ip)
|
||||
@ -37,19 +57,14 @@ func Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// check username
|
||||
var req LoginReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
user, err := op.GetUserByName(req.Username)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
loginCache.Set(ip, count+1)
|
||||
return
|
||||
}
|
||||
// validate password
|
||||
if err := user.ValidatePassword(req.Password); err != nil {
|
||||
// validate password hash
|
||||
if err := user.ValidatePwdHash(req.Password); err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
loginCache.Set(ip, count+1)
|
||||
return
|
||||
|
@ -46,6 +46,7 @@ func Init(e *gin.Engine) {
|
||||
auth := api.Group("", middlewares.Auth)
|
||||
|
||||
api.POST("/auth/login", handles.Login)
|
||||
api.POST("/auth/login/hash", handles.LoginHash)
|
||||
auth.GET("/me", handles.CurrentUser)
|
||||
auth.POST("/me/update", handles.UpdateCurrent)
|
||||
auth.POST("/auth/2fa/generate", handles.Generate2FA)
|
||||
|
Reference in New Issue
Block a user