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