李伟乐 2 years ago
parent 8b2e767347
commit ee2175422c
  1. 4
      middleware/jwt/jwt.go
  2. 3
      middleware/jwt/token.go

@ -15,7 +15,7 @@ type userIdKey struct{}
type authKey struct { type authKey struct {
} }
type ParseFunc func(key string, tokenStr string) (*UserInfo, error) type ParseFunc func(ctx context.Context, key string, tokenStr string) (*UserInfo, error)
type JwtOption func(o *options) type JwtOption func(o *options)
func WithJwtKey(jwtKey string) JwtOption { func WithJwtKey(jwtKey string) JwtOption {
@ -71,7 +71,7 @@ func JWT(opts ...JwtOption) middleware.Middleware {
if tr, ok := transport.FromServerContext(ctx); ok { if tr, ok := transport.FromServerContext(ctx); ok {
tokenStr = tr.RequestHeader().Get("token") tokenStr = tr.RequestHeader().Get("token")
} }
userInfo, err := cfg.parseFunc(cfg.jwtKey, tokenStr) userInfo, err := cfg.parseFunc(ctx, cfg.jwtKey, tokenStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -1,6 +1,7 @@
package jwt package jwt
import ( import (
"context"
"encoding/json" "encoding/json"
"git.diulo.com/mogfee/kit/errors" "git.diulo.com/mogfee/kit/errors"
"git.diulo.com/mogfee/kit/internal/xuuid" "git.diulo.com/mogfee/kit/internal/xuuid"
@ -53,7 +54,7 @@ func GetToken(key string, info *UserInfo) (token string, uniqId string, err erro
return return
} }
func Parse(key string, tokenStr string) (*UserInfo, error) { func Parse(ctx context.Context, key string, tokenStr string) (*UserInfo, error) {
if tokenStr == "" { if tokenStr == "" {
return nil, errors.Unauthorized("TOKEN_ERROR", "") return nil, errors.Unauthorized("TOKEN_ERROR", "")
} }

Loading…
Cancel
Save