feat: add middlewares
This commit is contained in:
33
backend/modules/middlewares/m_wechat_silent_auth.go
Normal file
33
backend/modules/middlewares/m_wechat_silent_auth.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"backend/providers/wechat"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (f *Middlewares) WeChatSilentAuth(c fiber.Ctx) error {
|
||||
// if cookie not exists key "openid", then redirect to the wechat auth page
|
||||
token := c.GetReqHeaders()["Authorization"]
|
||||
if len(token) != 0 {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// get current full url
|
||||
url := c.BaseURL()
|
||||
url = "https://qvyun.mp.jdwan.com"
|
||||
log.WithField("module", "middleware.SilentAuth").Debug("url:", url)
|
||||
|
||||
to, err := f.client.ScopeAuthorizeURL(
|
||||
wechat.ScopeAuthorizeURLWithRedirectURI(url),
|
||||
wechat.ScopeAuthorizeURLWithState("sns_basic_auth"),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get wechat auth url")
|
||||
}
|
||||
log.WithField("module", "middleware.SilentAuth").Debug("redirectTo: ", to.String())
|
||||
|
||||
return c.Redirect().To(to.String())
|
||||
}
|
||||
Reference in New Issue
Block a user