Skip to content

Commit 441e06d

Browse files
committed
supporting strict() for all libraries
1 parent 22b0855 commit 441e06d

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

encoder.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ type Base64Encoding interface {
99
DecodeString(s string) ([]byte, error)
1010
}
1111

12+
type StrictFunc[T Base64Encoding] func() T
13+
1214
type Stricter[T Base64Encoding] interface {
1315
Strict() T
1416
}
1517

18+
func DoStrict[S Base64Encoding, T Stricter[S]](x T) Base64Encoding {
19+
return x.Strict()
20+
}
21+
1622
// JSONMarshalFunc is an function type that allows to implement custom JSON
1723
// encoding algorithms.
1824
type JSONMarshalFunc func(v any) ([]byte, error)

parser.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ 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.
16-
useJSONNumber bool
17-
1815
// Skip claims validation during token parsing.
1916
skipClaimsValidation bool
2017

2118
validator *validator
2219

23-
decoders
20+
decoding
2421
}
2522

26-
type decoders struct {
23+
type decoding struct {
2724
jsonUnmarshal JSONUnmarshalFunc
2825
jsonNewDecoder JSONNewDecoderFunc[JSONDecoder]
2926

3027
rawUrlBase64Encoding Base64Encoding
3128
urlBase64Encoding Base64Encoding
29+
strict StrictFunc[Base64Encoding]
30+
31+
// Use JSON Number format in JSON decoder.
32+
useJSONNumber bool
3233

3334
decodeStrict bool
3435
decodePaddingAllowed bool
@@ -246,13 +247,15 @@ func (p *Parser) DecodeSegment(seg string) ([]byte, error) {
246247
}
247248

248249
if p.decodeStrict {
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)
250+
if p.strict != nil {
251+
encoding = p.strict()
252+
} else {
253+
stricter, ok := encoding.(Stricter[*base64.Encoding])
254+
if !ok {
255+
return nil, newError("WithStrictDecoding() was enabled but supplied base64 encoder does not support strict mode", ErrUnsupported)
256+
}
257+
encoding = stricter.Strict()
254258
}
255-
encoding = stricter.Strict()
256259
}
257260

258261
return encoding.DecodeString(seg)

parser_option.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func WithStrictDecoding() ParserOption {
144144
// "github.com/bytedance/sonic"
145145
// )
146146
//
147-
// var parser = NewParser(WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
147+
// var parser = jwt.NewParser(jwt.WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
148148
func WithJSONDecoder[T JSONDecoder](jsonUnmarshal JSONUnmarshalFunc, jsonNewDecoder JSONNewDecoderFunc[T]) ParserOption {
149149
return func(p *Parser) {
150150
p.jsonUnmarshal = jsonUnmarshal
@@ -176,10 +176,20 @@ func WithJSONDecoder[T JSONDecoder](jsonUnmarshal JSONUnmarshalFunc, jsonNewDeco
176176
// asmbase64 "github.com/segmentio/asm/base64"
177177
// )
178178
//
179-
// var parser = NewParser(WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
180-
func WithBase64Decoder(rawURL Base64Encoding, url Base64Encoding) ParserOption {
179+
// var parser = jwt.NewParser(jwt.WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
180+
func WithBase64Decoder[T Base64Encoding](rawURL Base64Encoding, url T) ParserOption {
181181
return func(p *Parser) {
182182
p.rawUrlBase64Encoding = rawURL
183183
p.urlBase64Encoding = url
184+
185+
// Check, whether the library supports the Strict() function
186+
stricter, ok := rawURL.(Stricter[T])
187+
if ok {
188+
// We need to get rid of the type parameter T, so we need to wrap it
189+
// here
190+
p.strict = func() Base64Encoding {
191+
return stricter.Strict()
192+
}
193+
}
184194
}
185195
}

0 commit comments

Comments
 (0)