add jwt custom validate supported

This commit is contained in:
Yavolte 2025-06-18 16:02:03 +08:00
parent 805ad282f5
commit 4dc5e64dde
1 changed files with 10 additions and 8 deletions

View File

@ -30,13 +30,15 @@ const (
type Option func(*options) 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 // Parser is a jwt parser
type options struct { type options struct {
allows []string allows []string
claims reflect.Type claims reflect.Type
validateFunc ValidateFunc validate Validate
} }
// WithAllow with allow path // 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) { 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 { if !ok {
return errors.ErrAccessDenied return errors.ErrAccessDenied
} }
if opts.validateFunc != nil { if opts.validate != nil {
if err = opts.validateFunc(ctx, token); err != nil { if err = opts.validate.Validate(ctx, token); err != nil {
return err return err
} }
} }