Skip to content

Commit 65357b9

Browse files
authored
Introducing functional-style options for the Parser type (#108)
1 parent cac353c commit 65357b9

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

parser.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,32 @@ import (
88
)
99

1010
type Parser struct {
11-
ValidMethods []string // If populated, only these methods will be considered valid
12-
UseJSONNumber bool // Use JSON Number format in JSON decoder
13-
SkipClaimsValidation bool // Skip claims validation during token parsing
11+
// If populated, only these methods will be considered valid.
12+
//
13+
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
14+
ValidMethods []string
15+
16+
// Use JSON Number format in JSON decoder.
17+
//
18+
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
19+
UseJSONNumber bool
20+
21+
// Skip claims validation during token parsing.
22+
//
23+
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
24+
SkipClaimsValidation bool
25+
}
26+
27+
// NewParser creates a new Parser with the specified options
28+
func NewParser(options ...ParserOption) *Parser {
29+
p := &Parser{}
30+
31+
// loop through our parsing options and apply them
32+
for _, option := range options {
33+
option(p)
34+
}
35+
36+
return p
1437
}
1538

1639
// Parse parses, validates, and returns a token.

parser_option.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package jwt
2+
3+
// ParserOption is used to implement functional-style options that modify the behaviour of the parser. To add
4+
// new options, just create a function (ideally beginning with With or Without) that returns an anonymous function that
5+
// takes a *Parser type as input and manipulates its configuration accordingly.
6+
type ParserOption func(*Parser)
7+
8+
// WithValidMethods is an option to supply algorithm methods that the parser will check. Only those methods will be considered valid.
9+
// 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/.
10+
func WithValidMethods(methods []string) ParserOption {
11+
return func(p *Parser) {
12+
p.ValidMethods = methods
13+
}
14+
}
15+
16+
// WithJSONNumber is an option to configure the underyling JSON parser with UseNumber
17+
func WithJSONNumber() ParserOption {
18+
return func(p *Parser) {
19+
p.UseJSONNumber = true
20+
}
21+
}
22+
23+
// WithoutClaimsValidation is an option to disable claims validation. This option should only be used if you exactly know
24+
// what you are doing.
25+
func WithoutClaimsValidation() ParserOption {
26+
return func(p *Parser) {
27+
p.SkipClaimsValidation = true
28+
}
29+
}

token.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ func (t *Token) SigningString() (string, error) {
8585
// Parse parses, validates, and returns a token.
8686
// keyFunc will receive the parsed token and should return the key for validating.
8787
// If everything is kosher, err will be nil
88-
func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
89-
return new(Parser).Parse(tokenString, keyFunc)
88+
func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
89+
return NewParser(options...).Parse(tokenString, keyFunc)
9090
}
9191

92-
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
93-
return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
92+
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
93+
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
9494
}
9595

9696
// EncodeSegment encodes a JWT specific base64url encoding with padding stripped

0 commit comments

Comments
 (0)