Skip to content

Commit d81e689

Browse files
committed
Extract encoders/decoders to extra struct
1 parent 836be3f commit d81e689

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

parser.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ type Parser struct {
2020

2121
validator *validator
2222

23+
decoders
24+
}
25+
26+
type decoders struct {
27+
jsonUnmarshal JSONUnmarshalFunc
28+
base64Decode Base64DecodeFunc
29+
2330
// This field is disabled when using a custom base64 encoder.
2431
decodeStrict bool
2532

2633
// This field is disabled when using a custom base64 encoder.
2734
decodePaddingAllowed bool
28-
29-
unmarshalFunc JSONUnmarshalFunc
30-
base64DecodeFunc Base64DecodeFunc
3135
}
3236

3337
// NewParser creates a new Parser with the specified options
@@ -156,8 +160,8 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
156160

157161
// Choose our JSON decoder. If no custom function is supplied, we use the standard library.
158162
var unmarshal JSONUnmarshalFunc
159-
if p.unmarshalFunc != nil {
160-
unmarshal = p.unmarshalFunc
163+
if p.jsonUnmarshal != nil {
164+
unmarshal = p.jsonUnmarshal
161165
} else {
162166
unmarshal = json.Unmarshal
163167
}
@@ -214,8 +218,8 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke
214218
// take into account whether the [Parser] is configured with additional options,
215219
// such as [WithStrictDecoding] or [WithPaddingAllowed].
216220
func (p *Parser) DecodeSegment(seg string) ([]byte, error) {
217-
if p.base64DecodeFunc != nil {
218-
return p.base64DecodeFunc(seg)
221+
if p.base64Decode != nil {
222+
return p.base64Decode(seg)
219223
}
220224

221225
encoding := base64.RawURLEncoding

parser_option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ func WithStrictDecoding() ParserOption {
122122
// WithJSONUnmarshal supports a custom [JSONUnmarshal] to use in parsing the JWT.
123123
func WithJSONUnmarshal(f JSONUnmarshalFunc) ParserOption {
124124
return func(p *Parser) {
125-
p.unmarshalFunc = f
125+
p.jsonUnmarshal = f
126126
}
127127
}
128128

129129
// WithBase64Decoder supports a custom [Base64Decoder] to use in parsing the JWT.
130130
func WithBase64Decoder(f Base64DecodeFunc) ParserOption {
131131
return func(p *Parser) {
132-
p.base64DecodeFunc = f
132+
p.base64Decode = f
133133
}
134134
}

token.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ type VerificationKeySet struct {
2828
// Token represents a JWT Token. Different fields will be used depending on
2929
// whether you're creating or parsing/verifying a token.
3030
type Token struct {
31-
Raw string // Raw contains the raw token. Populated when you [Parse] a token
32-
Method SigningMethod // Method is the signing method used or to be used
33-
Header map[string]interface{} // Header is the first segment of the token in decoded form
34-
Claims Claims // Claims is the second segment of the token in decoded form
35-
Signature []byte // Signature is the third segment of the token in decoded form. Populated when you Parse a token
36-
Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
37-
jsonEncoder JSONMarshalFunc // jsonEncoder is the custom json encoder/decoder
38-
base64Encoder Base64EncodeFunc // base64Encoder is the custom base64 encoder/decoder
31+
Raw string // Raw contains the raw token. Populated when you [Parse] a token
32+
Method SigningMethod // Method is the signing method used or to be used
33+
Header map[string]interface{} // Header is the first segment of the token in decoded form
34+
Claims Claims // Claims is the second segment of the token in decoded form
35+
Signature []byte // Signature is the third segment of the token in decoded form. Populated when you Parse a token
36+
Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
37+
38+
encoders
39+
}
40+
41+
type encoders struct {
42+
jsonMarshal JSONMarshalFunc // jsonEncoder is the custom json encoder/decoder
43+
base64Encode Base64EncodeFunc // base64Encoder is the custom base64 encoder/decoder
3944
}
4045

4146
// New creates a new [Token] with the specified signing method and an empty map
@@ -85,8 +90,8 @@ func (t *Token) SignedString(key interface{}) (string, error) {
8590
// straight for the SignedString.
8691
func (t *Token) SigningString() (string, error) {
8792
var marshal JSONMarshalFunc
88-
if t.jsonEncoder != nil {
89-
marshal = t.jsonEncoder
93+
if t.jsonMarshal != nil {
94+
marshal = t.jsonMarshal
9095
} else {
9196
marshal = json.Marshal
9297
}
@@ -110,8 +115,8 @@ func (t *Token) SigningString() (string, error) {
110115
// than a global function.
111116
func (t *Token) EncodeSegment(seg []byte) string {
112117
var enc Base64EncodeFunc
113-
if t.base64Encoder != nil {
114-
enc = t.base64Encoder
118+
if t.base64Encode != nil {
119+
enc = t.base64Encode
115120
} else {
116121
enc = base64.RawURLEncoding.EncodeToString
117122
}

token_option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ type TokenOption func(*Token)
66

77
func WithJSONEncoder(f JSONMarshalFunc) TokenOption {
88
return func(token *Token) {
9-
token.jsonEncoder = f
9+
token.jsonMarshal = f
1010
}
1111
}
1212

1313
func WithBase64Encoder(f Base64EncodeFunc) TokenOption {
1414
return func(token *Token) {
15-
token.base64Encoder = f
15+
token.base64Encode = f
1616
}
1717
}

0 commit comments

Comments
 (0)