You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser_option.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package jwt
  2. import "time"
  3. // ParserOption is used to implement functional-style options that modify the
  4. // behavior of the parser. To add new options, just create a function (ideally
  5. // beginning with With or Without) that returns an anonymous function that takes
  6. // a *Parser type as input and manipulates its configuration accordingly.
  7. type ParserOption func(*Parser)
  8. // WithValidMethods is an option to supply algorithm methods that the parser
  9. // will check. Only those methods will be considered valid. It is heavily
  10. // encouraged to use this option in order to prevent attacks such as
  11. // https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/.
  12. func WithValidMethods(methods []string) ParserOption {
  13. return func(p *Parser) {
  14. p.validMethods = methods
  15. }
  16. }
  17. // WithJSONNumber is an option to configure the underlying JSON parser with
  18. // UseNumber.
  19. func WithJSONNumber() ParserOption {
  20. return func(p *Parser) {
  21. p.useJSONNumber = true
  22. }
  23. }
  24. // WithoutClaimsValidation is an option to disable claims validation. This
  25. // option should only be used if you exactly know what you are doing.
  26. func WithoutClaimsValidation() ParserOption {
  27. return func(p *Parser) {
  28. p.skipClaimsValidation = true
  29. }
  30. }
  31. // WithLeeway returns the ParserOption for specifying the leeway window.
  32. func WithLeeway(leeway time.Duration) ParserOption {
  33. return func(p *Parser) {
  34. p.validator.leeway = leeway
  35. }
  36. }
  37. // WithTimeFunc returns the ParserOption for specifying the time func. The
  38. // primary use-case for this is testing. If you are looking for a way to account
  39. // for clock-skew, WithLeeway should be used instead.
  40. func WithTimeFunc(f func() time.Time) ParserOption {
  41. return func(p *Parser) {
  42. p.validator.timeFunc = f
  43. }
  44. }
  45. // WithIssuedAt returns the ParserOption to enable verification
  46. // of issued-at.
  47. func WithIssuedAt() ParserOption {
  48. return func(p *Parser) {
  49. p.validator.verifyIat = true
  50. }
  51. }
  52. // WithExpirationRequired returns the ParserOption to make exp claim required.
  53. // By default exp claim is optional.
  54. func WithExpirationRequired() ParserOption {
  55. return func(p *Parser) {
  56. p.validator.requireExp = true
  57. }
  58. }
  59. // WithAudience configures the validator to require the specified audience in
  60. // the `aud` claim. Validation will fail if the audience is not listed in the
  61. // token or the `aud` claim is missing.
  62. //
  63. // NOTE: While the `aud` claim is OPTIONAL in a JWT, the handling of it is
  64. // application-specific. Since this validation API is helping developers in
  65. // writing secure application, we decided to REQUIRE the existence of the claim,
  66. // if an audience is expected.
  67. func WithAudience(aud string) ParserOption {
  68. return func(p *Parser) {
  69. p.validator.expectedAud = aud
  70. }
  71. }
  72. // WithIssuer configures the validator to require the specified issuer in the
  73. // `iss` claim. Validation will fail if a different issuer is specified in the
  74. // token or the `iss` claim is missing.
  75. //
  76. // NOTE: While the `iss` claim is OPTIONAL in a JWT, the handling of it is
  77. // application-specific. Since this validation API is helping developers in
  78. // writing secure application, we decided to REQUIRE the existence of the claim,
  79. // if an issuer is expected.
  80. func WithIssuer(iss string) ParserOption {
  81. return func(p *Parser) {
  82. p.validator.expectedIss = iss
  83. }
  84. }
  85. // WithSubject configures the validator to require the specified subject in the
  86. // `sub` claim. Validation will fail if a different subject is specified in the
  87. // token or the `sub` claim is missing.
  88. //
  89. // NOTE: While the `sub` claim is OPTIONAL in a JWT, the handling of it is
  90. // application-specific. Since this validation API is helping developers in
  91. // writing secure application, we decided to REQUIRE the existence of the claim,
  92. // if a subject is expected.
  93. func WithSubject(sub string) ParserOption {
  94. return func(p *Parser) {
  95. p.validator.expectedSub = sub
  96. }
  97. }
  98. // WithPaddingAllowed will enable the codec used for decoding JWTs to allow
  99. // padding. Note that the JWS RFC7515 states that the tokens will utilize a
  100. // Base64url encoding with no padding. Unfortunately, some implementations of
  101. // JWT are producing non-standard tokens, and thus require support for decoding.
  102. func WithPaddingAllowed() ParserOption {
  103. return func(p *Parser) {
  104. p.decodePaddingAllowed = true
  105. }
  106. }
  107. // WithStrictDecoding will switch the codec used for decoding JWTs into strict
  108. // mode. In this mode, the decoder requires that trailing padding bits are zero,
  109. // as described in RFC 4648 section 3.5.
  110. func WithStrictDecoding() ParserOption {
  111. return func(p *Parser) {
  112. p.decodeStrict = true
  113. }
  114. }