add logics
This commit is contained in:
6
backend/common/consts/jwt.go
Normal file
6
backend/common/consts/jwt.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package consts
|
||||
|
||||
const (
|
||||
JwtToken = "__jwt_token:"
|
||||
SessionUser = "__session_user:"
|
||||
)
|
||||
@@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"backend/modules/middlewares"
|
||||
"backend/modules/users"
|
||||
"backend/providers/app"
|
||||
"backend/providers/http"
|
||||
@@ -31,16 +32,19 @@ func Command() atom.Option {
|
||||
atom.Name("serve"),
|
||||
atom.Short("run http server"),
|
||||
atom.RunE(Serve),
|
||||
atom.Providers(providers),
|
||||
atom.Providers(providers.With(
|
||||
middlewares.Provide,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
type Http struct {
|
||||
dig.In
|
||||
|
||||
Service *http.Service
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Routes []contracts.HttpRoute `group:"routes"`
|
||||
Service *http.Service
|
||||
Initials []contracts.Initial `group:"initials"`
|
||||
Routes []contracts.HttpRoute `group:"routes"`
|
||||
Middlewares *middlewares.Middlewares
|
||||
}
|
||||
|
||||
func Serve(cmd *cobra.Command, args []string) error {
|
||||
@@ -51,6 +55,11 @@ func Serve(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
mid := http.Middlewares
|
||||
http.Service.Engine.Use(mid.Verify)
|
||||
http.Service.Engine.Use(mid.AuthUserInfo)
|
||||
http.Service.Engine.Use(mid.SilentAuth)
|
||||
|
||||
return http.Service.Serve()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"backend/providers/wechat"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Middlewares struct {
|
||||
client *wechat.Client
|
||||
}
|
||||
|
||||
func Init(client *wechat.Client) *Middlewares {
|
||||
return &Middlewares{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Middlewares) Verify(c fiber.Ctx) error {
|
||||
// get the query parameters
|
||||
signature := c.Query("signature")
|
||||
timestamp := c.Query("timestamp")
|
||||
nonce := c.Query("nonce")
|
||||
echostr := c.Query("echostr")
|
||||
|
||||
if signature == "" || timestamp == "" || nonce == "" || echostr == "" {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
log.Infof(
|
||||
"begin verify signature, signature: %s, timestamp: %s, nonce: %s, echostr: %s",
|
||||
signature,
|
||||
timestamp,
|
||||
nonce,
|
||||
echostr,
|
||||
)
|
||||
|
||||
// verify the signature
|
||||
if err := f.client.Verify(signature, timestamp, nonce); err != nil {
|
||||
return c.SendString(err.Error())
|
||||
}
|
||||
|
||||
return c.SendString(echostr)
|
||||
}
|
||||
|
||||
func (f *Middlewares) SilentAuth(c fiber.Ctx) error {
|
||||
// if cookie not exists key "openid", then redirect to the wechat auth page
|
||||
sid := c.Cookies("sid", "")
|
||||
if sid != "" {
|
||||
// TODO: verify sid
|
||||
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())
|
||||
}
|
||||
|
||||
func (f *Middlewares) AuthUserInfo(c fiber.Ctx) error {
|
||||
state := c.Query("state")
|
||||
code := c.Query("code")
|
||||
|
||||
if state == "" && code == "" {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
if state != "sns_basic_auth" {
|
||||
return c.Next()
|
||||
}
|
||||
log.WithField("module", "middleware.AuthUserInfo").Debug("code", code)
|
||||
|
||||
// get the openid
|
||||
token, err := f.client.AuthorizeCode2Token(code)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get openid")
|
||||
}
|
||||
// TODO: store the openid to the session
|
||||
|
||||
// set the openid to the cookie
|
||||
c.Cookie(&fiber.Cookie{
|
||||
Name: "sid",
|
||||
Value: token.Openid,
|
||||
HTTPOnly: true,
|
||||
})
|
||||
|
||||
return c.Redirect().To("/")
|
||||
}
|
||||
Reference in New Issue
Block a user