@@ -116,25 +116,67 @@ func WithPaddingAllowed() ParserOption {
116
116
// WithStrictDecoding will switch the codec used for decoding JWTs into strict
117
117
// mode. In this mode, the decoder requires that trailing padding bits are zero,
118
118
// as described in RFC 4648 section 3.5.
119
+ //
120
+ // Note: This is only supported when using [encoding/base64.Encoding], but not
121
+ // by any other decoder specified with [WithBase64Decoder].
119
122
func WithStrictDecoding () ParserOption {
120
123
return func (p * Parser ) {
121
124
p .decodeStrict = true
122
125
}
123
126
}
124
127
125
- // WithJSONDecoder supports a custom [JSONUnmarshal] to use in parsing the JWT.
126
- func WithJSONDecoder [T JSONDecoder ](f JSONUnmarshalFunc , f2 JSONNewDecoderFunc [T ]) ParserOption {
128
+ // WithJSONDecoder supports a custom JSON decoder to use in parsing the JWT.
129
+ // There are two functions that can be supplied:
130
+ // - jsonUnmarshal is a [JSONUnmarshalFunc] that is used for the
131
+ // un-marshalling the header and claims when no other options are specified
132
+ // - jsonNewDecoder is a [JSONNewDecoderFunc] that is used to create an object
133
+ // satisfying the [JSONDecoder] interface.
134
+ //
135
+ // The latter is used when the [WithJSONNumber] option is used.
136
+ //
137
+ // If any of the supplied functions is set to nil, the defaults from the Go
138
+ // standard library, [encoding/json.Unmarshal] and [encoding/json.NewDecoder]
139
+ // are used.
140
+ //
141
+ // Example using the https://github.com/bytedance/sonic library.
142
+ //
143
+ // import (
144
+ // "github.com/bytedance/sonic"
145
+ // )
146
+ //
147
+ // var parser = NewParser(WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
148
+ func WithJSONDecoder [T JSONDecoder ](jsonUnmarshal JSONUnmarshalFunc , jsonNewDecoder JSONNewDecoderFunc [T ]) ParserOption {
127
149
return func (p * Parser ) {
128
- p .jsonUnmarshal = f
150
+ p .jsonUnmarshal = jsonUnmarshal
129
151
// This seems to be necessary, since we don't want to store the specific
130
- // JSONDecoder type in our parser, but need it in the function interface.
152
+ // JSONDecoder type in our parser, but need it in the function
153
+ // interface.
131
154
p .jsonNewDecoder = func (r io.Reader ) JSONDecoder {
132
- return f2 (r )
155
+ return jsonNewDecoder (r )
133
156
}
134
157
}
135
158
}
136
159
137
- // WithBase64Decoder supports a custom [Base64Encoding] to use in parsing the JWT.
160
+ // WithBase64Decoder supports a custom Base64 when decoding a base64 encoded
161
+ // token. Two encoding can be specified:
162
+ // - rawURL needs to contain a [Base64Encoding] that is based on base64url
163
+ // without padding. This is used for parsing tokens with the default
164
+ // options.
165
+ // - url needs to contain a [Base64Encoding] based on base64url with padding.
166
+ // The sole use of this to decode tokens when [WithPaddingAllowed] is
167
+ // enabled.
168
+ //
169
+ // If any of the supplied encodings are set to nil, the defaults from the Go
170
+ // standard library, [encoding/base64.RawURLEncoding] and
171
+ // [encoding/base64.URLEncoding] are used.
172
+ //
173
+ // Example using the https://github.com/segmentio/asm library.
174
+ //
175
+ // import (
176
+ // asmbase64 "github.com/segmentio/asm/base64"
177
+ // )
178
+ //
179
+ // var parser = NewParser(WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
138
180
func WithBase64Decoder (rawURL Base64Encoding , url Base64Encoding ) ParserOption {
139
181
return func (p * Parser ) {
140
182
p .rawUrlBase64Encoding = rawURL
0 commit comments