mirror of https://github.com/golang-jwt/jwt.git
Introducing functional-style options for the Parser type (#108)
This commit is contained in:
parent
cac353cdc2
commit
65357b9e5b
29
parser.go
29
parser.go
|
@ -8,9 +8,32 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
ValidMethods []string // If populated, only these methods will be considered valid
|
// If populated, only these methods will be considered valid.
|
||||||
UseJSONNumber bool // Use JSON Number format in JSON decoder
|
//
|
||||||
SkipClaimsValidation bool // Skip claims validation during token parsing
|
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
|
||||||
|
ValidMethods []string
|
||||||
|
|
||||||
|
// Use JSON Number format in JSON decoder.
|
||||||
|
//
|
||||||
|
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
|
||||||
|
UseJSONNumber bool
|
||||||
|
|
||||||
|
// Skip claims validation during token parsing.
|
||||||
|
//
|
||||||
|
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
|
||||||
|
SkipClaimsValidation bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewParser creates a new Parser with the specified options
|
||||||
|
func NewParser(options ...ParserOption) *Parser {
|
||||||
|
p := &Parser{}
|
||||||
|
|
||||||
|
// loop through our parsing options and apply them
|
||||||
|
for _, option := range options {
|
||||||
|
option(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses, validates, and returns a token.
|
// Parse parses, validates, and returns a token.
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package jwt
|
||||||
|
|
||||||
|
// ParserOption is used to implement functional-style options that modify the behaviour of the parser. To add
|
||||||
|
// new options, just create a function (ideally beginning with With or Without) that returns an anonymous function that
|
||||||
|
// takes a *Parser type as input and manipulates its configuration accordingly.
|
||||||
|
type ParserOption func(*Parser)
|
||||||
|
|
||||||
|
// WithValidMethods is an option to supply algorithm methods that the parser will check. Only those methods will be considered valid.
|
||||||
|
// It is heavily encouraged to use this option in order to prevent attacks such as https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/.
|
||||||
|
func WithValidMethods(methods []string) ParserOption {
|
||||||
|
return func(p *Parser) {
|
||||||
|
p.ValidMethods = methods
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithJSONNumber is an option to configure the underyling JSON parser with UseNumber
|
||||||
|
func WithJSONNumber() ParserOption {
|
||||||
|
return func(p *Parser) {
|
||||||
|
p.UseJSONNumber = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithoutClaimsValidation is an option to disable claims validation. This option should only be used if you exactly know
|
||||||
|
// what you are doing.
|
||||||
|
func WithoutClaimsValidation() ParserOption {
|
||||||
|
return func(p *Parser) {
|
||||||
|
p.SkipClaimsValidation = true
|
||||||
|
}
|
||||||
|
}
|
8
token.go
8
token.go
|
@ -85,12 +85,12 @@ func (t *Token) SigningString() (string, error) {
|
||||||
// Parse parses, validates, and returns a token.
|
// Parse parses, validates, and returns a token.
|
||||||
// keyFunc will receive the parsed token and should return the key for validating.
|
// keyFunc will receive the parsed token and should return the key for validating.
|
||||||
// If everything is kosher, err will be nil
|
// If everything is kosher, err will be nil
|
||||||
func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
|
||||||
return new(Parser).Parse(tokenString, keyFunc)
|
return NewParser(options...).Parse(tokenString, keyFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
|
||||||
return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
|
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeSegment encodes a JWT specific base64url encoding with padding stripped
|
// EncodeSegment encodes a JWT specific base64url encoding with padding stripped
|
||||||
|
|
Loading…
Reference in New Issue