feat: init repo

This commit is contained in:
Rogee
2025-01-09 19:11:01 +08:00
parent b9cc63fe8a
commit 1c7b603769
149 changed files with 20066 additions and 10 deletions

View File

@@ -0,0 +1,17 @@
package middlewares
import (
"strings"
"github.com/gofiber/fiber/v3"
)
func (m *Middlewares) CheckUA(ctx fiber.Ctx) error {
keyword := strings.ToLower("MicroMessenger")
userAgent := ctx.GetReqHeaders()["User-Agent"][0]
if strings.Contains(userAgent, keyword) {
return ctx.SendString("")
}
return ctx.Next()
}

View File

@@ -0,0 +1,37 @@
package middlewares
import (
"time"
"backend/app/errorx"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
)
func (f *Middlewares) ParseJWT(c fiber.Ctx) error {
tokens := c.GetReqHeaders()["Authorization"]
if len(tokens) == 0 {
queryToken := c.Query("token")
tokens = []string{queryToken}
if len(tokens) == 0 {
return c.Next()
}
}
token := tokens[0]
claim, err := f.jwt.Parse(token)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "token",
Value: "",
Expires: time.Now().Add(-1 * time.Hour),
HTTPOnly: true,
})
log.Errorf("failed to parse jwt from token: %s", token)
return errorx.Unauthorized
}
_ = claim
return c.Next()
}

View File

@@ -0,0 +1,66 @@
package middlewares
import (
"fmt"
"strings"
"time"
"backend/providers/wechat"
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const StatePrefix = "sns_basic_auth"
func (f *Middlewares) WeChatAuth(c fiber.Ctx) error {
log := log.WithField("module", "middleware.AuthUserInfo")
log.Debugf("%s, query: %v", c.OriginalURL(), c.Queries())
state := c.Query("state")
code := c.Query("code")
log.Debugf("code: %s, state: %s", code, state)
jwtToken := c.Cookies("token")
if jwtToken != "" {
log.Debugf("jwtToken: %s", jwtToken)
if _, err := f.jwt.Parse(jwtToken); err != nil {
log.WithError(err).Error("failed to parse jwt token")
c.Cookie(&fiber.Cookie{
Name: "token",
Value: "",
Expires: time.Now().Add(-1 * time.Hour),
HTTPOnly: true,
})
return c.Redirect().To(c.Path())
}
}
if state == "" && code == "" {
url := string(c.Request().URI().FullURI())
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(fmt.Sprintf("%s_%d", StatePrefix, time.Now().UnixNano())),
)
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 !strings.HasPrefix(state, StatePrefix) || code == "" {
return errors.New("invalid request")
}
return c.Next()
}

View File

@@ -0,0 +1,33 @@
package middlewares
import (
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
)
// 此方法用于微信首次接入时的数据验证
func (f *Middlewares) WeChatVerify(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.WithField("method", "Verify").WithFields(log.Fields{
"signature": signature,
"timestamp": timestamp,
"nonce": nonce,
"echostr": echostr,
}).Debug("begin verify signature")
// verify the signature
if err := f.client.Verify(signature, timestamp, nonce); err != nil {
return c.SendString(err.Error())
}
return c.SendString(echostr)
}

View File

@@ -0,0 +1,9 @@
package middlewares
import (
"github.com/gofiber/fiber/v3"
)
func (f *Middlewares) DebugMode(c fiber.Ctx) error {
return c.Next()
}

View File

@@ -0,0 +1,25 @@
package middlewares
import (
"backend/providers/app"
"backend/providers/jwt"
"backend/providers/storage"
"backend/providers/wechat"
log "github.com/sirupsen/logrus"
)
// @provider
type Middlewares struct {
log *log.Entry `inject:"false"`
app *app.Config
storagePath *storage.Config
jwt *jwt.JWT
client *wechat.Client
}
func (f *Middlewares) Prepare() error {
f.log = log.WithField("module", "middleware")
return nil
}

View File

@@ -0,0 +1,20 @@
package middlewares
import (
"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() (*Middlewares, error) {
obj := &Middlewares{}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}); err != nil {
return err
}
return nil
}