@@ -12,17 +12,22 @@ type Parser struct {
12
12
// If populated, only these methods will be considered valid.
13
13
validMethods []string
14
14
15
- // Use JSON Number format in JSON decoder.
15
+ // Use JSON Number format in JSON decoder. This field is disabled when using a custom json encoder.
16
16
useJSONNumber bool
17
17
18
18
// Skip claims validation during token parsing.
19
19
skipClaimsValidation bool
20
20
21
21
validator * validator
22
22
23
+ // This field is disabled when using a custom base64 encoder.
23
24
decodeStrict bool
24
25
26
+ // This field is disabled when using a custom base64 encoder.
25
27
decodePaddingAllowed bool
28
+
29
+ unmarshalFunc JSONUnmarshalFunc
30
+ base64DecodeFunc Base64DecodeFunc
26
31
}
27
32
28
33
// NewParser creates a new Parser with the specified options
@@ -148,7 +153,17 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
148
153
if headerBytes , err = p .DecodeSegment (parts [0 ]); err != nil {
149
154
return token , parts , newError ("could not base64 decode header" , ErrTokenMalformed , err )
150
155
}
151
- if err = json .Unmarshal (headerBytes , & token .Header ); err != nil {
156
+
157
+ // Choose our JSON decoder. If no custom function is supplied, we use the standard library.
158
+ var unmarshal JSONUnmarshalFunc
159
+ if p .unmarshalFunc != nil {
160
+ unmarshal = p .unmarshalFunc
161
+ } else {
162
+ unmarshal = json .Unmarshal
163
+ }
164
+
165
+ err = unmarshal (headerBytes , & token .Header )
166
+ if err != nil {
152
167
return token , parts , newError ("could not JSON decode header" , ErrTokenMalformed , err )
153
168
}
154
169
@@ -162,13 +177,13 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
162
177
163
178
// If `useJSONNumber` is enabled then we must use *json.Decoder to decode
164
179
// the claims. However, this comes with a performance penalty so only use
165
- // it if we must and, otherwise, simple use json.Unmarshal .
180
+ // it if we must and, otherwise, simple use our decode function .
166
181
if ! p .useJSONNumber {
167
182
// JSON Unmarshal. Special case for map type to avoid weird pointer behavior.
168
183
if c , ok := token .Claims .(MapClaims ); ok {
169
- err = json . Unmarshal (claimBytes , & c )
184
+ err = unmarshal (claimBytes , & c )
170
185
} else {
171
- err = json . Unmarshal (claimBytes , & claims )
186
+ err = unmarshal (claimBytes , & claims )
172
187
}
173
188
} else {
174
189
dec := json .NewDecoder (bytes .NewBuffer (claimBytes ))
@@ -200,6 +215,10 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
200
215
// take into account whether the [Parser] is configured with additional options,
201
216
// such as [WithStrictDecoding] or [WithPaddingAllowed].
202
217
func (p * Parser ) DecodeSegment (seg string ) ([]byte , error ) {
218
+ if p .base64DecodeFunc != nil {
219
+ return p .base64DecodeFunc (seg )
220
+ }
221
+
203
222
encoding := base64 .RawURLEncoding
204
223
205
224
if p .decodePaddingAllowed {
0 commit comments