forked from golang-jwt/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing functional-style options for the Parser type (golang-jwt#108
- Loading branch information
Showing
3 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters