Skip to content

Commit f64f460

Browse files
committed
supporting strict() for all libraries
1 parent 3ae2a4a commit f64f460

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
@@ -152,7 +152,7 @@ func WithStrictDecoding() ParserOption {
152152
// "github.com/bytedance/sonic"
153153
// )
154154
//
155-
// var parser = NewParser(WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
155+
// var parser = jwt.NewParser(jwt.WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
156156
func WithJSONDecoder[T JSONDecoder](jsonUnmarshal JSONUnmarshalFunc, jsonNewDecoder JSONNewDecoderFunc[T]) ParserOption {
157157
return func(p *Parser) {
158158
p.jsonUnmarshal = jsonUnmarshal
@@ -184,10 +184,20 @@ func WithJSONDecoder[T JSONDecoder](jsonUnmarshal JSONUnmarshalFunc, jsonNewDeco
184184
// asmbase64 "github.com/segmentio/asm/base64"
185185
// )
186186
//
187-
// var parser = NewParser(WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
188-
func WithBase64Decoder(rawURL Base64Encoding, url Base64Encoding) ParserOption {
187+
// var parser = jwt.NewParser(jwt.WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
188+
func WithBase64Decoder[T Base64Encoding](rawURL Base64Encoding, url T) ParserOption {
189189
return func(p *Parser) {
190190
p.rawUrlBase64Encoding = rawURL
191191
p.urlBase64Encoding = url
192+
193+
// Check, whether the library supports the Strict() function
194+
stricter, ok := rawURL.(Stricter[T])
195+
if ok {
196+
// We need to get rid of the type parameter T, so we need to wrap it
197+
// here
198+
p.strict = func() Base64Encoding {
199+
return stricter.Strict()
200+
}
201+
}
192202
}
193203
}

0 commit comments

Comments
 (0)