parser_option.go 1.2 KB

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