Skip to content

Commit 3ae2a4a

Browse files
committed
Added more Godoc
1 parent 7684d3e commit 3ae2a4a

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

encoder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package jwt
22

33
import "io"
44

5+
// Base64Encoding represents an object that can encode and decode base64. A
6+
// common example is [encoding/base64.Encoding].
57
type Base64Encoding interface {
68
EncodeToString(src []byte) string
79
DecodeString(s string) ([]byte, error)

parser_option.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,67 @@ func WithPaddingAllowed() ParserOption {
124124
// WithStrictDecoding will switch the codec used for decoding JWTs into strict
125125
// mode. In this mode, the decoder requires that trailing padding bits are zero,
126126
// as described in RFC 4648 section 3.5.
127+
//
128+
// Note: This is only supported when using [encoding/base64.Encoding], but not
129+
// by any other decoder specified with [WithBase64Decoder].
127130
func WithStrictDecoding() ParserOption {
128131
return func(p *Parser) {
129132
p.decodeStrict = true
130133
}
131134
}
132135

133-
// WithJSONDecoder supports a custom [JSONUnmarshal] to use in parsing the JWT.
134-
func WithJSONDecoder[T JSONDecoder](f JSONUnmarshalFunc, f2 JSONNewDecoderFunc[T]) ParserOption {
136+
// WithJSONDecoder supports a custom JSON decoder to use in parsing the JWT.
137+
// There are two functions that can be supplied:
138+
// - jsonUnmarshal is a [JSONUnmarshalFunc] that is used for the
139+
// un-marshalling the header and claims when no other options are specified
140+
// - jsonNewDecoder is a [JSONNewDecoderFunc] that is used to create an object
141+
// satisfying the [JSONDecoder] interface.
142+
//
143+
// The latter is used when the [WithJSONNumber] option is used.
144+
//
145+
// If any of the supplied functions is set to nil, the defaults from the Go
146+
// standard library, [encoding/json.Unmarshal] and [encoding/json.NewDecoder]
147+
// are used.
148+
//
149+
// Example using the https://github.com/bytedance/sonic library.
150+
//
151+
// import (
152+
// "github.com/bytedance/sonic"
153+
// )
154+
//
155+
// var parser = NewParser(WithJSONDecoder(sonic.Unmarshal, sonic.ConfigDefault.NewDecoder))
156+
func WithJSONDecoder[T JSONDecoder](jsonUnmarshal JSONUnmarshalFunc, jsonNewDecoder JSONNewDecoderFunc[T]) ParserOption {
135157
return func(p *Parser) {
136-
p.jsonUnmarshal = f
158+
p.jsonUnmarshal = jsonUnmarshal
137159
// This seems to be necessary, since we don't want to store the specific
138-
// JSONDecoder type in our parser, but need it in the function interface.
160+
// JSONDecoder type in our parser, but need it in the function
161+
// interface.
139162
p.jsonNewDecoder = func(r io.Reader) JSONDecoder {
140-
return f2(r)
163+
return jsonNewDecoder(r)
141164
}
142165
}
143166
}
144167

145-
// WithBase64Decoder supports a custom [Base64Encoding] to use in parsing the JWT.
168+
// WithBase64Decoder supports a custom Base64 when decoding a base64 encoded
169+
// token. Two encoding can be specified:
170+
// - rawURL needs to contain a [Base64Encoding] that is based on base64url
171+
// without padding. This is used for parsing tokens with the default
172+
// options.
173+
// - url needs to contain a [Base64Encoding] based on base64url with padding.
174+
// The sole use of this to decode tokens when [WithPaddingAllowed] is
175+
// enabled.
176+
//
177+
// If any of the supplied encodings are set to nil, the defaults from the Go
178+
// standard library, [encoding/base64.RawURLEncoding] and
179+
// [encoding/base64.URLEncoding] are used.
180+
//
181+
// Example using the https://github.com/segmentio/asm library.
182+
//
183+
// import (
184+
// asmbase64 "github.com/segmentio/asm/base64"
185+
// )
186+
//
187+
// var parser = NewParser(WithBase64Decoder(asmbase64.RawURLEncoding, asmbase64.URLEncoding))
146188
func WithBase64Decoder(rawURL Base64Encoding, url Base64Encoding) ParserOption {
147189
return func(p *Parser) {
148190
p.rawUrlBase64Encoding = rawURL

0 commit comments

Comments
 (0)