Skip to content

Commit

Permalink
Merge pull request #15 from go-cinch/piupuer/dev
Browse files Browse the repository at this point in the history
[fix]skip pub api
  • Loading branch information
piupuer authored Oct 12, 2024
2 parents 7668e2b + 5af09c3 commit 0bcb584
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/server/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/go-cinch/common/utils"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/go-kratos/kratos/v2/transport"
kratosHttp "github.com/go-kratos/kratos/v2/transport/http"
jwtV4 "github.com/golang-jwt/jwt/v4"
"github.com/redis/go-redis/v9"
)
Expand All @@ -27,6 +29,25 @@ const jwtTokenUserKey = "jwt_token_"
func jwtHandler(c *conf.Bootstrap, client redis.UniversalClient) func(handler middleware.Handler) middleware.Handler {
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (rp interface{}, err error) {
tr, ok := transport.FromServerContext(ctx)
if !ok {
err = biz.ErrJwtMissingToken(ctx)
return
}
var path string
switch tr.Kind() {
case transport.KindHTTP:
if ht, ok3 := tr.(kratosHttp.Transporter); ok3 {
path = ht.Request().URL.Path
}
case transport.KindGRPC:
// grpc api for internal no need check
return handler(ctx, req)
}
// public api no need check
if strings.Contains(path, "/pub/") {
return handler(ctx, req)
}
user := jwtLocal.FromServerContext(ctx)
if user.Token == "" && user.Code == "" {
err = biz.ErrJwtMissingToken(ctx)
Expand Down

0 comments on commit 0bcb584

Please sign in to comment.