diff --git a/core/app/api/v2/auth.go b/core/app/api/v2/auth.go index 87dd37842..43ba52ad4 100644 --- a/core/app/api/v2/auth.go +++ b/core/app/api/v2/auth.go @@ -2,7 +2,6 @@ package v2 import ( "encoding/base64" - "github.com/1Panel-dev/1Panel/core/app/api/v2/helper" "github.com/1Panel-dev/1Panel/core/app/dto" "github.com/1Panel-dev/1Panel/core/app/model" @@ -28,8 +27,8 @@ func (b *BaseApi) Login(c *gin.Context) { } if req.AuthMethod != "jwt" && !req.IgnoreCaptcha { - if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil { - helper.InternalServer(c, err) + if errMsg := captcha.VerifyCode(req.CaptchaID, req.Captcha); errMsg != "" { + helper.BadAuth(c, errMsg, nil) return } } @@ -39,8 +38,12 @@ func (b *BaseApi) Login(c *gin.Context) { entrance, _ = base64.StdEncoding.DecodeString(entranceItem) } - user, err := authService.Login(c, req, string(entrance)) + user, msgKey, err := authService.Login(c, req, string(entrance)) go saveLoginLogs(c, err) + if msgKey == "ErrAuth" { + helper.BadAuth(c, msgKey, err) + return + } if err != nil { helper.InternalServer(c, err) return @@ -67,7 +70,11 @@ func (b *BaseApi) MFALogin(c *gin.Context) { entrance, _ = base64.StdEncoding.DecodeString(entranceItem) } - user, err := authService.MFALogin(c, req, string(entrance)) + user, msgKey, err := authService.MFALogin(c, req, string(entrance)) + if msgKey == "ErrAuth" { + helper.BadAuth(c, msgKey, err) + return + } if err != nil { helper.InternalServer(c, err) return @@ -141,16 +148,7 @@ func saveLoginLogs(c *gin.Context, err error) { logs.Status = constant.StatusSuccess } logs.IP = c.ClientIP() - //lang := c.GetHeader("Accept-Language") - //if lang == "" { - // lang = "zh" - //} - //address, err := geo.GetIPLocation(logs.IP, lang) - //if err != nil { - // global.LOG.Errorf("get ip location failed: %s", err) - //} logs.Agent = c.GetHeader("User-Agent") - //logs.Address = address _ = logService.CreateLoginLog(logs) } diff --git a/core/app/api/v2/helper/helper.go b/core/app/api/v2/helper/helper.go index e8188e0af..15b918284 100644 --- a/core/app/api/v2/helper/helper.go +++ b/core/app/api/v2/helper/helper.go @@ -15,7 +15,7 @@ func ErrorWithDetail(ctx *gin.Context, code int, msgKey string, err error) { Message: "", } if msgKey == "ErrCaptchaCode" || msgKey == "ErrAuth" { - res.Code = 406 + res.Code = 401 res.Message = msgKey } res.Message = i18n.GetMsgWithMap(msgKey, map[string]interface{}{"detail": err}) diff --git a/core/app/repo/setting.go b/core/app/repo/setting.go index 034c2ad92..23eb72811 100644 --- a/core/app/repo/setting.go +++ b/core/app/repo/setting.go @@ -13,6 +13,7 @@ type ISettingRepo interface { GetValueByKey(key string) (string, error) Create(key, value string) error Update(key, value string) error + UpdateOrCreate(key, value string) error } func NewISettingRepo() ISettingRepo { @@ -58,3 +59,7 @@ func (u *SettingRepo) GetValueByKey(key string) (string, error) { func (u *SettingRepo) Update(key, value string) error { return global.DB.Model(&model.Setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error } + +func (u *SettingRepo) UpdateOrCreate(key, value string) error { + return global.DB.Model(&model.Setting{}).Where("key = ?", key).Assign(model.Setting{Key: key, Value: value}).FirstOrCreate(&model.Setting{}).Error +} diff --git a/core/app/service/auth.go b/core/app/service/auth.go index 1a6f44585..f65e16b84 100644 --- a/core/app/service/auth.go +++ b/core/app/service/auth.go @@ -21,9 +21,9 @@ type IAuthService interface { CheckIsSafety(code string) (string, error) GetResponsePage() (string, error) VerifyCode(code string) (bool, error) - Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, error) + Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, string, error) LogOut(c *gin.Context) error - MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) + MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, string, error) GetSecurityEntrance() string IsLogin(c *gin.Context) bool } @@ -32,79 +32,86 @@ func NewIAuthService() IAuthService { return &AuthService{} } -func (u *AuthService) Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, error) { +func (u *AuthService) Login(c *gin.Context, info dto.Login, entrance string) (*dto.UserLoginInfo, string, error) { nameSetting, err := settingRepo.Get(repo.WithByKey("UserName")) if err != nil { - return nil, buserr.New("ErrRecordNotFound") + return nil, "", buserr.New("ErrRecordNotFound") } passwordSetting, err := settingRepo.Get(repo.WithByKey("Password")) if err != nil { - return nil, buserr.New("ErrRecordNotFound") + return nil, "", buserr.New("ErrRecordNotFound") } pass, err := encrypt.StringDecrypt(passwordSetting.Value) if err != nil { - return nil, buserr.New("ErrAuth") + return nil, "ErrAuth", nil } if !hmac.Equal([]byte(info.Password), []byte(pass)) || nameSetting.Value != info.Name { - return nil, buserr.New("ErrAuth") + return nil, "ErrAuth", nil } entranceSetting, err := settingRepo.Get(repo.WithByKey("SecurityEntrance")) if err != nil { - return nil, err + return nil, "", err } if len(entranceSetting.Value) != 0 && entranceSetting.Value != entrance { - return nil, buserr.New("ErrEntrance") + return nil, "ErrEntrance", nil } mfa, err := settingRepo.Get(repo.WithByKey("MFAStatus")) if err != nil { - return nil, err + return nil, "", err } if err = settingRepo.Update("Language", info.Language); err != nil { - return nil, err + return nil, "", err } if mfa.Value == constant.StatusEnable { - return &dto.UserLoginInfo{Name: nameSetting.Value, MfaStatus: mfa.Value}, nil + return &dto.UserLoginInfo{Name: nameSetting.Value, MfaStatus: mfa.Value}, "", nil } - return u.generateSession(c, info.Name, info.AuthMethod) + res, err := u.generateSession(c, info.Name, info.AuthMethod) + if err != nil { + return nil, "", err + } + return res, "", nil } -func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, error) { +func (u *AuthService) MFALogin(c *gin.Context, info dto.MFALogin, entrance string) (*dto.UserLoginInfo, string, error) { nameSetting, err := settingRepo.Get(repo.WithByKey("UserName")) if err != nil { - return nil, buserr.New("ErrRecordNotFound") + return nil, "", buserr.New("ErrRecordNotFound") } passwordSetting, err := settingRepo.Get(repo.WithByKey("Password")) if err != nil { - return nil, buserr.New("ErrRecordNotFound") + return nil, "", buserr.New("ErrRecordNotFound") } pass, err := encrypt.StringDecrypt(passwordSetting.Value) if err != nil { - return nil, err + return nil, "", err } if !hmac.Equal([]byte(info.Password), []byte(pass)) || nameSetting.Value != info.Name { - return nil, buserr.New("ErrAuth") + return nil, "ErrAuth", nil } entranceSetting, err := settingRepo.Get(repo.WithByKey("SecurityEntrance")) if err != nil { - return nil, err + return nil, "", err } if len(entranceSetting.Value) != 0 && entranceSetting.Value != entrance { - return nil, buserr.New("ErrEntrance") + return nil, "", buserr.New("ErrEntrance") } mfaSecret, err := settingRepo.Get(repo.WithByKey("MFASecret")) if err != nil { - return nil, err + return nil, "", err } mfaInterval, err := settingRepo.Get(repo.WithByKey("MFAInterval")) if err != nil { - return nil, err + return nil, "", err } success := mfa.ValidCode(info.Code, mfaInterval.Value, mfaSecret.Value) if !success { - return nil, buserr.New("ErrAuth") + return nil, "ErrAuth", nil } - - return u.generateSession(c, info.Name, info.AuthMethod) + res, err := u.generateSession(c, info.Name, info.AuthMethod) + if err != nil { + return nil, "", err + } + return res, "", nil } func (u *AuthService) generateSession(c *gin.Context, name, authMethod string) (*dto.UserLoginInfo, error) { diff --git a/core/utils/captcha/captcha.go b/core/utils/captcha/captcha.go index 9d0afe0a1..fb85d9d01 100644 --- a/core/utils/captcha/captcha.go +++ b/core/utils/captcha/captcha.go @@ -4,24 +4,23 @@ import ( "strings" "github.com/1Panel-dev/1Panel/core/app/dto" - "github.com/1Panel-dev/1Panel/core/buserr" "github.com/mojocn/base64Captcha" ) var store = base64Captcha.DefaultMemStore -func VerifyCode(codeID string, code string) error { +func VerifyCode(codeID string, code string) string { if codeID == "" { - return buserr.New("ErrCaptchaCode") + return "ErrCaptchaCode" } vv := store.Get(codeID, true) vv = strings.TrimSpace(vv) code = strings.TrimSpace(code) if strings.EqualFold(vv, code) { - return nil + return "" } - return buserr.New("ErrCaptchaCode") + return "ErrCaptchaCode" } func CreateCaptcha() (*dto.CaptchaResponse, error) { diff --git a/frontend/src/components/drawer-pro/index.vue b/frontend/src/components/drawer-pro/index.vue index 7363c031c..b8543b2c5 100644 --- a/frontend/src/components/drawer-pro/index.vue +++ b/frontend/src/components/drawer-pro/index.vue @@ -1,5 +1,5 @@