From 4dc5e64ddea7cef6243067d117bfc0484f117da4 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Wed, 18 Jun 2025 16:02:03 +0800 Subject: [PATCH] add jwt custom validate supported --- middleware/auth/jwt.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/middleware/auth/jwt.go b/middleware/auth/jwt.go index 328ced4..b0137c7 100644 --- a/middleware/auth/jwt.go +++ b/middleware/auth/jwt.go @@ -30,13 +30,15 @@ const ( type Option func(*options) -type ValidateFunc func(ctx context.Context, token string) error +type Validate interface { + Validate(ctx context.Context, token string) error +} // Parser is a jwt parser type options struct { - allows []string - claims reflect.Type - validateFunc ValidateFunc + allows []string + claims reflect.Type + validate Validate } // WithAllow with allow path @@ -55,9 +57,9 @@ func WithClaims(claims reflect.Type) Option { } } -func WithValidateFunc(fn ValidateFunc) Option { +func WithValidate(fn Validate) Option { return func(o *options) { - o.validateFunc = fn + o.validate = fn } } @@ -101,8 +103,8 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware { if !ok { return errors.ErrAccessDenied } - if opts.validateFunc != nil { - if err = opts.validateFunc(ctx, token); err != nil { + if opts.validate != nil { + if err = opts.validate.Validate(ctx, token); err != nil { return err } }