add jwt custom validate supported
This commit is contained in:
parent
0bc3f9d170
commit
805ad282f5
|
@ -30,10 +30,13 @@ const (
|
||||||
|
|
||||||
type Option func(*options)
|
type Option func(*options)
|
||||||
|
|
||||||
|
type ValidateFunc func(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
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithAllow with allow path
|
// WithAllow with allow path
|
||||||
|
@ -52,6 +55,12 @@ func WithClaims(claims reflect.Type) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithValidateFunc(fn ValidateFunc) Option {
|
||||||
|
return func(o *options) {
|
||||||
|
o.validateFunc = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// isAllowed check if the path is allowed
|
// isAllowed check if the path is allowed
|
||||||
func isAllowed(uripath string, allows []string) bool {
|
func isAllowed(uripath string, allows []string) bool {
|
||||||
for _, str := range allows {
|
for _, str := range allows {
|
||||||
|
@ -92,6 +101,11 @@ func JWT(keyFunc jwt.Keyfunc, cbs ...Option) middleware.Middleware {
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.ErrAccessDenied
|
return errors.ErrAccessDenied
|
||||||
}
|
}
|
||||||
|
if opts.validateFunc != nil {
|
||||||
|
if err = opts.validateFunc(ctx, token); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if strings.HasPrefix(token, bearerWord) {
|
if strings.HasPrefix(token, bearerWord) {
|
||||||
token = strings.TrimPrefix(token, bearerWord)
|
token = strings.TrimPrefix(token, bearerWord)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue