@@ -26,12 +26,11 @@ type Parser struct {
2626type decoders struct {
2727 jsonUnmarshal JSONUnmarshalFunc
2828 jsonNewDecoder JSONNewDecoderFunc [JSONDecoder ]
29- base64Decode Base64DecodeFunc
3029
31- // This field is disabled when using a custom base64 encoder.
32- decodeStrict bool
30+ rawUrlBase64Encoding Base64Encoding
31+ urlBase64Encoding Base64Encoding
3332
34- // This field is disabled when using a custom base64 encoder.
33+ decodeStrict bool
3534 decodePaddingAllowed bool
3635}
3736
@@ -227,22 +226,35 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
227226// take into account whether the [Parser] is configured with additional options,
228227// such as [WithStrictDecoding] or [WithPaddingAllowed].
229228func (p * Parser ) DecodeSegment (seg string ) ([]byte , error ) {
230- if p .base64Decode != nil {
231- return p .base64Decode (seg )
229+ var encoding Base64Encoding
230+ if p .rawUrlBase64Encoding != nil {
231+ encoding = p .rawUrlBase64Encoding
232+ } else {
233+ encoding = base64 .RawURLEncoding
232234 }
233235
234- encoding := base64 .RawURLEncoding
235-
236236 if p .decodePaddingAllowed {
237237 if l := len (seg ) % 4 ; l > 0 {
238238 seg += strings .Repeat ("=" , 4 - l )
239239 }
240- encoding = base64 .URLEncoding
240+
241+ if p .urlBase64Encoding != nil {
242+ encoding = p .urlBase64Encoding
243+ } else {
244+ encoding = base64 .URLEncoding
245+ }
241246 }
242247
243248 if p .decodeStrict {
244- encoding = encoding .Strict ()
249+ // For now we can only support the standard library here because of the
250+ // current state of the type parameter system
251+ stricter , ok := encoding .(Stricter [* base64.Encoding ])
252+ if ! ok {
253+ return nil , newError ("strict mode is only supported in encoding/base64" , ErrUnsupported )
254+ }
255+ encoding = stricter .Strict ()
245256 }
257+
246258 return encoding .DecodeString (seg )
247259}
248260
0 commit comments