Browse Source

test:文件密码校验失败解决

master
刘子旺 7 months ago
parent
commit
924f0f7647
  1. 14
      godo/files/os.go
  2. 27
      godo/files/pwdfile.go

14
godo/files/os.go

@ -28,6 +28,7 @@ import (
"godo/libs" "godo/libs"
"io" "io"
"io/fs" "io/fs"
"log/slog"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -342,12 +343,13 @@ func CheckFilePwd(fpwd, salt string) bool {
// 1. 对输入的密码进行哈希 // 1. 对输入的密码进行哈希
pwd := libs.HashPassword(fpwd, salt) pwd := libs.HashPassword(fpwd, salt)
// 2. 获取存储的密码哈希 // 2. 获取存储的密码哈希
oldpwd, err := libs.GetConfig("filePwd") oldPwd, isHas := libs.GetConfig("filePwd")
if !err { if !isHas {
slog.Error("文件密码获取失败")
return false return false
} }
// 3. 比对密码哈希 // 3. 比对密码哈希
return oldpwd == pwd return oldPwd == pwd
} }
func IsHavePwd(pwd string) bool { func IsHavePwd(pwd string) bool {
@ -380,8 +382,10 @@ func GetPwdFlag() (bool, error) {
Name: "isPwd", Name: "isPwd",
Value: false, Value: false,
} }
libs.SetConfig(req) err := libs.SetConfig(req)
libs.SaveConfig() if err != nil {
return false, err
}
return false, nil // 返回默认值和无错误 return false, nil // 返回默认值和无错误
} }

27
godo/files/pwdfile.go

@ -101,18 +101,26 @@ func HandleSetFilePwd(w http.ResponseWriter, r *http.Request) {
hashPwd := libs.HashPassword(fPwd, salt) hashPwd := libs.HashPassword(fPwd, salt)
// 服务端存储 // 服务端存储
req := libs.ReqBody{ reqPwd := libs.ReqBody{
Name: "filePwd", Name: "filePwd",
Value: hashPwd, Value: hashPwd,
} }
libs.SetConfig(req) err = libs.SetConfig(reqPwd)
if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, err.Error())
return
}
// salt值存储 // salt值存储
reqSalt := libs.ReqBody{ reqSalt := libs.ReqBody{
Name: "salt", Name: "salt",
Value: salt, Value: salt,
} }
libs.SetConfig(reqSalt) err = libs.SetConfig(reqSalt)
if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, err.Error())
return
}
res := libs.APIResponse{Message: "密码设置成功"} res := libs.APIResponse{Message: "密码设置成功"}
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }
@ -130,7 +138,11 @@ func HandleChangeFilePwd(w http.ResponseWriter, r *http.Request) {
Name: "filePwd", Name: "filePwd",
Value: newPwd, Value: newPwd,
} }
libs.SetConfig(pwdReq) err = libs.SetConfig(pwdReq)
if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, err.Error())
return
}
libs.SuccessMsg(w, "success", "The file password change success!") libs.SuccessMsg(w, "success", "The file password change success!")
} }
@ -153,7 +165,10 @@ func HandleSetIsPwd(w http.ResponseWriter, r *http.Request) {
Name: "isPwd", Name: "isPwd",
Value: isPwdBool, Value: isPwdBool,
} }
libs.SetConfig(pwdReq) err = libs.SetConfig(pwdReq)
libs.SaveConfig() if err != nil {
libs.HTTPError(w, http.StatusInternalServerError, err.Error())
return
}
libs.SuccessMsg(w, "success", "") libs.SuccessMsg(w, "success", "")
} }

Loading…
Cancel
Save