feat: complete
This commit is contained in:
46
backend/modules/middlewares/m_wechat_auth.go
Normal file
46
backend/modules/middlewares/m_wechat_auth.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"backend/providers/wechat"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (f *Middlewares) WeChatAuth(c fiber.Ctx) error {
|
||||
log.WithField("module", "middleware.AuthUserInfo").Debugf("%s, query: %v", c.OriginalURL(), c.Queries())
|
||||
state := c.Query("state")
|
||||
code := c.Query("code")
|
||||
log.WithField("module", "middleware.AuthUserInfo").Debugf("code: %s, state: %s", code, state)
|
||||
|
||||
if state == "" && code == "" {
|
||||
url := string(c.Request().URI().FullURI())
|
||||
if f.app.IsDevMode() && f.app.BaseURI != nil {
|
||||
url = strings.ReplaceAll(url, "http", "https")
|
||||
url = strings.ReplaceAll(url, c.BaseURL(), *f.app.BaseURI)
|
||||
}
|
||||
|
||||
log.WithField("module", "middleware.SilentAuth").Debug("redirect_uri: ", 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())
|
||||
|
||||
}
|
||||
|
||||
if state != "sns_basic_auth" || code == "" {
|
||||
return errors.New("invalid request")
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"backend/pkg/pg"
|
||||
"backend/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (f *Middlewares) WeChatAuthUserInfo(c fiber.Ctx) error {
|
||||
// 如果请求存在 Authorization 头,则跳过
|
||||
if len(c.GetReqHeaders()["Authorization"]) != 0 {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
log.WithField("module", "middleware.AuthUserInfo").Debugf("%s, query: %v", c.OriginalURL(), c.Queries())
|
||||
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").Debugf("code: %s, state: %s", code, state)
|
||||
|
||||
// get the openid
|
||||
token, err := f.client.AuthorizeCode2Token(code)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get openid")
|
||||
}
|
||||
log.Debugf("tokenInfo %+v", token)
|
||||
|
||||
paths := lo.Filter(strings.Split(c.Path(), "/"), func(s string, _ int) bool {
|
||||
return s != ""
|
||||
})
|
||||
if len(paths) < 2 || paths[0] != "t" {
|
||||
return errors.New("invalid path")
|
||||
}
|
||||
|
||||
tenantSlug := paths[1]
|
||||
if tenantSlug == "" {
|
||||
return errors.New("tenant is empty")
|
||||
}
|
||||
|
||||
tenant, err := f.userSvc.GetTenantBySlug(c.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get tenant id")
|
||||
}
|
||||
|
||||
var oauthInfo pg.UserOAuth
|
||||
if err := copier.Copy(&oauthInfo, token); err != nil {
|
||||
return errors.Wrap(err, "failed to copy oauth info")
|
||||
}
|
||||
log.Debugf("oauthInfo %+v", oauthInfo)
|
||||
|
||||
user, err := f.userSvc.GetOrNew(c.Context(), tenant.ID, token.Openid, oauthInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
|
||||
claim := f.jwt.CreateClaims(jwt.BaseClaims{
|
||||
OpenID: user.OpenID,
|
||||
Tenant: tenantSlug,
|
||||
UserID: user.ID,
|
||||
TenantID: tenant.ID,
|
||||
})
|
||||
jwtToken, err := f.jwt.CreateToken(claim)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create token")
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(filepath.Join(f.storagePath.Asset, "index.html"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read file")
|
||||
}
|
||||
|
||||
html := strings.ReplaceAll(string(b), "{{JWT}}", jwtToken)
|
||||
|
||||
c.Set("Content-Type", "text/html")
|
||||
return c.SendString(html)
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"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 := string(c.Request().URI().FullURI())
|
||||
if f.app.IsDevMode() && f.app.BaseURI != nil {
|
||||
url = strings.ReplaceAll(url, "http", "https")
|
||||
url = strings.ReplaceAll(url, c.BaseURL(), *f.app.BaseURI)
|
||||
}
|
||||
|
||||
log.WithField("module", "middleware.SilentAuth").Debug("redirect_uri: ", 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())
|
||||
}
|
||||
@@ -2,12 +2,14 @@ package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (f *Middlewares) DebugMode(c fiber.Ctx) error {
|
||||
// fullURI := c.Request().URI().FullURI()
|
||||
// host := c.BaseURL()
|
||||
// fmt.Println(strings.Split(c.Path(), "/"))
|
||||
// return c.SendString(c.Params("tenant", "no tenant: "+c.Path()))
|
||||
// return c.SendString("ABC" + c.Params("+"))
|
||||
log.SetLevel(log.DebugLevel)
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"backend/modules/users"
|
||||
"backend/providers/app"
|
||||
"backend/providers/jwt"
|
||||
"backend/providers/storage"
|
||||
"backend/providers/wechat"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
@@ -15,13 +16,15 @@ func Provide(opts ...opt.Option) error {
|
||||
app *app.Config,
|
||||
client *wechat.Client,
|
||||
jwt *jwt.JWT,
|
||||
storagePath *storage.Config,
|
||||
userSvc *users.Service,
|
||||
) (*Middlewares, error) {
|
||||
obj := &Middlewares{
|
||||
app: app,
|
||||
client: client,
|
||||
jwt: jwt,
|
||||
userSvc: userSvc,
|
||||
app: app,
|
||||
client: client,
|
||||
jwt: jwt,
|
||||
storagePath: storagePath,
|
||||
userSvc: userSvc,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
|
||||
75
backend/modules/wechat/controller.go
Normal file
75
backend/modules/wechat/controller.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"backend/modules/users"
|
||||
"backend/pkg/pg"
|
||||
"backend/providers/jwt"
|
||||
"backend/providers/storage"
|
||||
"backend/providers/wechat"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
jwt *jwt.JWT
|
||||
storagePath *storage.Config
|
||||
userSvc *users.Service
|
||||
client *wechat.Client
|
||||
}
|
||||
|
||||
func (c *Controller) Render(ctx fiber.Ctx) error {
|
||||
code := ctx.Query("code")
|
||||
|
||||
// get the openid
|
||||
token, err := c.client.AuthorizeCode2Token(code)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get openid")
|
||||
}
|
||||
log.Debugf("tokenInfo %+v", token)
|
||||
|
||||
tenantSlug := ctx.Params("tenant")
|
||||
tenant, err := c.userSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get tenant id")
|
||||
}
|
||||
|
||||
var oauthInfo pg.UserOAuth
|
||||
if err := copier.Copy(&oauthInfo, token); err != nil {
|
||||
return errors.Wrap(err, "failed to copy oauth info")
|
||||
}
|
||||
log.Debugf("oauthInfo %+v", oauthInfo)
|
||||
|
||||
user, err := c.userSvc.GetOrNew(ctx.Context(), tenant.ID, token.Openid, oauthInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
|
||||
claim := c.jwt.CreateClaims(jwt.BaseClaims{
|
||||
OpenID: user.OpenID,
|
||||
Tenant: tenantSlug,
|
||||
UserID: user.ID,
|
||||
TenantID: tenant.ID,
|
||||
})
|
||||
jwtToken, err := c.jwt.CreateToken(claim)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create token")
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(filepath.Join(c.storagePath.Asset, "index.html"))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to read file")
|
||||
}
|
||||
|
||||
html := strings.ReplaceAll(string(b), "{{JWT}}", jwtToken)
|
||||
|
||||
ctx.Set("Content-Type", "text/html")
|
||||
return ctx.SendString(html)
|
||||
}
|
||||
32
backend/modules/wechat/provider.gen.go
Executable file
32
backend/modules/wechat/provider.gen.go
Executable file
@@ -0,0 +1,32 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"backend/modules/users"
|
||||
"backend/providers/jwt"
|
||||
"backend/providers/storage"
|
||||
"backend/providers/wechat"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
client *wechat.Client,
|
||||
jwt *jwt.JWT,
|
||||
storagePath *storage.Config,
|
||||
userSvc *users.Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
client: client,
|
||||
jwt: jwt,
|
||||
storagePath: storagePath,
|
||||
userSvc: userSvc,
|
||||
}
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user