add jwt custom validate supported
This commit is contained in:
parent
805ad282f5
commit
4dc5e64dde
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue