2021-10-13 20:36:33 +03:00
|
|
|
package jwt
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
import "time"
|
|
|
|
|
|
|
|
// ParserOption is used to implement functional-style options that modify the
|
|
|
|
// behavior 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.
|
2021-10-13 20:36:33 +03:00
|
|
|
type ParserOption func(*Parser)
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// 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/.
|
2021-10-13 20:36:33 +03:00
|
|
|
func WithValidMethods(methods []string) ParserOption {
|
|
|
|
return func(p *Parser) {
|
2023-02-21 16:32:25 +03:00
|
|
|
p.validMethods = methods
|
2021-10-13 20:36:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// WithJSONNumber is an option to configure the underlying JSON parser with
|
|
|
|
// UseNumber.
|
2021-10-13 20:36:33 +03:00
|
|
|
func WithJSONNumber() ParserOption {
|
|
|
|
return func(p *Parser) {
|
2023-02-21 16:32:25 +03:00
|
|
|
p.useJSONNumber = true
|
2021-10-13 20:36:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// WithoutClaimsValidation is an option to disable claims validation. This
|
|
|
|
// option should only be used if you exactly know what you are doing.
|
2021-10-13 20:36:33 +03:00
|
|
|
func WithoutClaimsValidation() ParserOption {
|
|
|
|
return func(p *Parser) {
|
2023-02-21 16:32:25 +03:00
|
|
|
p.skipClaimsValidation = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLeeway returns the ParserOption for specifying the leeway window.
|
|
|
|
func WithLeeway(leeway time.Duration) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.leeway = leeway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTimeFunc returns the ParserOption for specifying the time func. The
|
|
|
|
// primary use-case for this is testing. If you are looking for a way to account
|
|
|
|
// for clock-skew, WithLeeway should be used instead.
|
|
|
|
func WithTimeFunc(f func() time.Time) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.timeFunc = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIssuedAt returns the ParserOption to enable verification
|
|
|
|
// of issued-at.
|
|
|
|
func WithIssuedAt() ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.verifyIat = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 20:58:20 +03:00
|
|
|
// WithExpirationRequired returns the ParserOption to make exp claim required.
|
|
|
|
// By default exp claim is optional.
|
|
|
|
func WithExpirationRequired() ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.requireExp = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// WithAudience configures the validator to require the specified audience in
|
|
|
|
// the `aud` claim. Validation will fail if the audience is not listed in the
|
|
|
|
// token or the `aud` claim is missing.
|
|
|
|
//
|
|
|
|
// NOTE: While the `aud` claim is OPTIONAL in a JWT, the handling of it is
|
|
|
|
// application-specific. Since this validation API is helping developers in
|
|
|
|
// writing secure application, we decided to REQUIRE the existence of the claim,
|
|
|
|
// if an audience is expected.
|
|
|
|
func WithAudience(aud string) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.expectedAud = aud
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIssuer configures the validator to require the specified issuer in the
|
|
|
|
// `iss` claim. Validation will fail if a different issuer is specified in the
|
|
|
|
// token or the `iss` claim is missing.
|
|
|
|
//
|
|
|
|
// NOTE: While the `iss` claim is OPTIONAL in a JWT, the handling of it is
|
|
|
|
// application-specific. Since this validation API is helping developers in
|
|
|
|
// writing secure application, we decided to REQUIRE the existence of the claim,
|
|
|
|
// if an issuer is expected.
|
|
|
|
func WithIssuer(iss string) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.expectedIss = iss
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSubject configures the validator to require the specified subject in the
|
|
|
|
// `sub` claim. Validation will fail if a different subject is specified in the
|
|
|
|
// token or the `sub` claim is missing.
|
|
|
|
//
|
|
|
|
// NOTE: While the `sub` claim is OPTIONAL in a JWT, the handling of it is
|
|
|
|
// application-specific. Since this validation API is helping developers in
|
|
|
|
// writing secure application, we decided to REQUIRE the existence of the claim,
|
|
|
|
// if a subject is expected.
|
|
|
|
func WithSubject(sub string) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.validator.expectedSub = sub
|
2021-10-13 20:36:33 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-24 21:13:09 +03:00
|
|
|
|
|
|
|
// WithPaddingAllowed will enable the codec used for decoding JWTs to allow
|
|
|
|
// padding. Note that the JWS RFC7515 states that the tokens will utilize a
|
|
|
|
// Base64url encoding with no padding. Unfortunately, some implementations of
|
|
|
|
// JWT are producing non-standard tokens, and thus require support for decoding.
|
|
|
|
func WithPaddingAllowed() ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.decodePaddingAllowed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStrictDecoding will switch the codec used for decoding JWTs into strict
|
|
|
|
// mode. In this mode, the decoder requires that trailing padding bits are zero,
|
2023-04-10 11:33:52 +03:00
|
|
|
// as described in RFC 4648 section 3.5.
|
2023-03-24 21:13:09 +03:00
|
|
|
func WithStrictDecoding() ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.decodeStrict = true
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 18:54:36 +03:00
|
|
|
|
|
|
|
// WithJSONUnmarshal supports a custom [JSONUnmarshal] to use in parsing the JWT.
|
|
|
|
func WithJSONUnmarshal(f JSONUnmarshalFunc) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.unmarshalFunc = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBase64Decoder supports a custom [Base64Decoder] to use in parsing the JWT.
|
|
|
|
func WithBase64Decoder(f Base64DecodeFunc) ParserOption {
|
|
|
|
return func(p *Parser) {
|
|
|
|
p.base64DecodeFunc = f
|
|
|
|
}
|
|
|
|
}
|