@@ -12,17 +12,22 @@ type Parser struct {
1212 // If populated, only these methods will be considered valid.
1313 validMethods []string
1414
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.
1616 useJSONNumber bool
1717
1818 // Skip claims validation during token parsing.
1919 skipClaimsValidation bool
2020
2121 validator * Validator
2222
23+ // This field is disabled when using a custom base64 encoder.
2324 decodeStrict bool
2425
26+ // This field is disabled when using a custom base64 encoder.
2527 decodePaddingAllowed bool
28+
29+ unmarshalFunc JSONUnmarshalFunc
30+ base64DecodeFunc Base64DecodeFunc
2631}
2732
2833// NewParser creates a new Parser with the specified options
@@ -148,7 +153,17 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
148153 if headerBytes , err = p .DecodeSegment (parts [0 ]); err != nil {
149154 return token , parts , newError ("could not base64 decode header" , ErrTokenMalformed , err )
150155 }
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 {
152167 return token , parts , newError ("could not JSON decode header" , ErrTokenMalformed , err )
153168 }
154169
@@ -162,13 +177,13 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
162177
163178 // If `useJSONNumber` is enabled then we must use *json.Decoder to decode
164179 // 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 .
166181 if ! p .useJSONNumber {
167182 // JSON Unmarshal. Special case for map type to avoid weird pointer behavior.
168183 if c , ok := token .Claims .(MapClaims ); ok {
169- err = json . Unmarshal (claimBytes , & c )
184+ err = unmarshal (claimBytes , & c )
170185 } else {
171- err = json . Unmarshal (claimBytes , & claims )
186+ err = unmarshal (claimBytes , & claims )
172187 }
173188 } else {
174189 dec := json .NewDecoder (bytes .NewBuffer (claimBytes ))
@@ -200,6 +215,10 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
200215// take into account whether the [Parser] is configured with additional options,
201216// such as [WithStrictDecoding] or [WithPaddingAllowed].
202217func (p * Parser ) DecodeSegment (seg string ) ([]byte , error ) {
218+ if p .base64DecodeFunc != nil {
219+ return p .base64DecodeFunc (seg )
220+ }
221+
203222 encoding := base64 .RawURLEncoding
204223
205224 if p .decodePaddingAllowed {
0 commit comments