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 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
}
}