From 59c949f627059460ebd4af4670fc432607e44e70 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Thu, 12 Jun 2025 15:06:46 +0800 Subject: [PATCH] fix jwt auth --- middleware/auth/jwt.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/middleware/auth/jwt.go b/middleware/auth/jwt.go index 582af3d..50d9e86 100644 --- a/middleware/auth/jwt.go +++ b/middleware/auth/jwt.go @@ -45,12 +45,12 @@ func WithClaims(f func() jwt.Claims) Option { } // WithAllow with allow path -func WithAllow(path string) Option { +func WithAllow(paths ...string) Option { return func(o *options) { if o.allows == nil { o.allows = make([]string, 0, 16) } - o.allows = append(o.allows, path) + o.allows = append(o.allows, paths...) } } @@ -82,10 +82,6 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware { return func(next middleware.Handler) middleware.Handler { return func(ctx context.Context) (err error) { md := metadata.FromContext(ctx) - authorizationValue, ok := md.Get(authorizationKey) - if !ok { - return errors.ErrAccessDenied - } if len(opts.allows) > 0 { requestPath, ok := md.Get(metadata.RequestPathKey) if ok { @@ -94,6 +90,10 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware { } } } + authorizationValue, ok := md.Get(authorizationKey) + if !ok { + return errors.ErrAccessDenied + } if !strings.HasPrefix(authorizationValue, bearerWord) { return errors.ErrAccessDenied }