mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 20:58:14 -05:00
🐛 查看密码逻辑修复
This commit is contained in:
parent
daa7e03240
commit
5e528c42c7
@ -33,12 +33,15 @@ func (v *apiV1) serve() {
|
|||||||
mr.Use(mygin.Authorize(mygin.AuthorizeOption{
|
mr.Use(mygin.Authorize(mygin.AuthorizeOption{
|
||||||
MemberOnly: false,
|
MemberOnly: false,
|
||||||
IsPage: false,
|
IsPage: false,
|
||||||
ValidateViewPassword: true,
|
|
||||||
AllowAPI: true,
|
AllowAPI: true,
|
||||||
Msg: "访问此接口需要认证",
|
Msg: "访问此接口需要认证",
|
||||||
Btn: "点此登录",
|
Btn: "点此登录",
|
||||||
Redirect: "/login",
|
Redirect: "/login",
|
||||||
}))
|
}))
|
||||||
|
mr.Use(mygin.ValidateViewPassword(mygin.ValidateViewPasswordOption{
|
||||||
|
IsPage: false,
|
||||||
|
AbortWhenFail: true,
|
||||||
|
}))
|
||||||
mr.GET("/:id", v.monitorHistoriesById)
|
mr.GET("/:id", v.monitorHistoriesById)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,12 +43,14 @@ type commonPage struct {
|
|||||||
|
|
||||||
func (cp *commonPage) serve() {
|
func (cp *commonPage) serve() {
|
||||||
cr := cp.r.Group("")
|
cr := cp.r.Group("")
|
||||||
cr.Use(mygin.Authorize(mygin.AuthorizeOption{
|
cr.Use(mygin.Authorize(mygin.AuthorizeOption{}))
|
||||||
ValidateViewPassword: true,
|
|
||||||
}))
|
|
||||||
cr.Use(mygin.PreferredTheme)
|
cr.Use(mygin.PreferredTheme)
|
||||||
cr.GET("/terminal/:id", cp.terminal)
|
|
||||||
cr.POST("/view-password", cp.issueViewPassword)
|
cr.POST("/view-password", cp.issueViewPassword)
|
||||||
|
cr.GET("/terminal/:id", cp.terminal)
|
||||||
|
cr.Use(mygin.ValidateViewPassword(mygin.ValidateViewPasswordOption{
|
||||||
|
IsPage: true,
|
||||||
|
AbortWhenFail: true,
|
||||||
|
}))
|
||||||
cr.GET("/", cp.home)
|
cr.GET("/", cp.home)
|
||||||
cr.GET("/service", cp.service)
|
cr.GET("/service", cp.service)
|
||||||
// TODO: 界面直接跳转使用该接口
|
// TODO: 界面直接跳转使用该接口
|
||||||
@ -65,6 +67,7 @@ type viewPasswordForm struct {
|
|||||||
func (p *commonPage) issueViewPassword(c *gin.Context) {
|
func (p *commonPage) issueViewPassword(c *gin.Context) {
|
||||||
var vpf viewPasswordForm
|
var vpf viewPasswordForm
|
||||||
err := c.ShouldBind(&vpf)
|
err := c.ShouldBind(&vpf)
|
||||||
|
log.Println("bingo", vpf)
|
||||||
var hash []byte
|
var hash []byte
|
||||||
if err == nil && vpf.Password != singleton.Conf.Site.ViewPassword {
|
if err == nil && vpf.Password != singleton.Conf.Site.ViewPassword {
|
||||||
err = errors.New(singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "WrongAccessPassword"}))
|
err = errors.New(singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "WrongAccessPassword"}))
|
||||||
|
@ -6,8 +6,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
"github.com/naiba/nezha/service/singleton"
|
"github.com/naiba/nezha/service/singleton"
|
||||||
@ -16,7 +14,6 @@ import (
|
|||||||
type AuthorizeOption struct {
|
type AuthorizeOption struct {
|
||||||
GuestOnly bool
|
GuestOnly bool
|
||||||
MemberOnly bool
|
MemberOnly bool
|
||||||
ValidateViewPassword bool
|
|
||||||
IsPage bool
|
IsPage bool
|
||||||
AllowAPI bool
|
AllowAPI bool
|
||||||
Msg string
|
Msg string
|
||||||
@ -82,20 +79,5 @@ func Authorize(opt AuthorizeOption) func(*gin.Context) {
|
|||||||
ShowErrorPage(c, commonErr, opt.IsPage)
|
ShowErrorPage(c, commonErr, opt.IsPage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证查看密码
|
|
||||||
if opt.ValidateViewPassword && singleton.Conf.Site.ViewPassword != "" {
|
|
||||||
viewPassword, _ := c.Cookie(singleton.Conf.Site.CookieName + "-vp")
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(viewPassword), []byte(singleton.Conf.Site.ViewPassword)); err != nil {
|
|
||||||
c.HTML(http.StatusOK, GetPreferredTheme(c, "/viewpassword"), CommonEnvironment(c, gin.H{
|
|
||||||
"Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "VerifyPassword"}),
|
|
||||||
"CustomCode": singleton.Conf.Site.CustomCode,
|
|
||||||
}))
|
|
||||||
c.Abort()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Set(model.CtxKeyViewPasswordVerified, true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
pkg/mygin/view_password.go
Normal file
52
pkg/mygin/view_password.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package mygin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/service/singleton"
|
||||||
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ValidateViewPasswordOption struct {
|
||||||
|
IsPage bool
|
||||||
|
AbortWhenFail bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateViewPassword(opt ValidateViewPasswordOption) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if singleton.Conf.Site.ViewPassword == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, authorized := c.Get(model.CtxKeyAuthorizedUser)
|
||||||
|
if authorized {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
viewPassword, err := c.Cookie(singleton.Conf.Site.CookieName + "-vp")
|
||||||
|
if err == nil {
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(viewPassword), []byte(singleton.Conf.Site.ViewPassword))
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
c.Set(model.CtxKeyViewPasswordVerified, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !opt.AbortWhenFail {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if opt.IsPage {
|
||||||
|
c.HTML(http.StatusOK, GetPreferredTheme(c, "/viewpassword"), CommonEnvironment(c, gin.H{
|
||||||
|
"Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "VerifyPassword"}),
|
||||||
|
"CustomCode": singleton.Conf.Site.CustomCode,
|
||||||
|
}))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, model.Response{
|
||||||
|
Code: http.StatusForbidden,
|
||||||
|
Message: "访问受限",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user