From 7a5e5d6efe0720be8ae10ceba8040fac23b4906a Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Wed, 13 Sep 2023 16:53:08 +0200 Subject: [PATCH] Extract encoders/decoders to extra struct --- parser.go | 18 +++++++++++------- parser_option.go | 4 ++-- token.go | 29 +++++++++++++++++------------ token_option.go | 4 ++-- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/parser.go b/parser.go index a0a9274..e79b414 100644 --- a/parser.go +++ b/parser.go @@ -20,14 +20,18 @@ type Parser struct { validator *Validator + decoders +} + +type decoders struct { + jsonUnmarshal JSONUnmarshalFunc + base64Decode Base64DecodeFunc + // This field is disabled when using a custom base64 encoder. decodeStrict bool // This field is disabled when using a custom base64 encoder. decodePaddingAllowed bool - - unmarshalFunc JSONUnmarshalFunc - base64DecodeFunc Base64DecodeFunc } // NewParser creates a new Parser with the specified options @@ -156,8 +160,8 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke // Choose our JSON decoder. If no custom function is supplied, we use the standard library. var unmarshal JSONUnmarshalFunc - if p.unmarshalFunc != nil { - unmarshal = p.unmarshalFunc + if p.jsonUnmarshal != nil { + unmarshal = p.jsonUnmarshal } else { unmarshal = json.Unmarshal } @@ -214,8 +218,8 @@ func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Toke // take into account whether the [Parser] is configured with additional options, // such as [WithStrictDecoding] or [WithPaddingAllowed]. func (p *Parser) DecodeSegment(seg string) ([]byte, error) { - if p.base64DecodeFunc != nil { - return p.base64DecodeFunc(seg) + if p.base64Decode != nil { + return p.base64Decode(seg) } encoding := base64.RawURLEncoding diff --git a/parser_option.go b/parser_option.go index 735774e..b724731 100644 --- a/parser_option.go +++ b/parser_option.go @@ -130,13 +130,13 @@ func WithStrictDecoding() ParserOption { // WithJSONUnmarshal supports a custom [JSONUnmarshal] to use in parsing the JWT. func WithJSONUnmarshal(f JSONUnmarshalFunc) ParserOption { return func(p *Parser) { - p.unmarshalFunc = f + p.jsonUnmarshal = f } } // WithBase64Decoder supports a custom [Base64Decoder] to use in parsing the JWT. func WithBase64Decoder(f Base64DecodeFunc) ParserOption { return func(p *Parser) { - p.base64DecodeFunc = f + p.base64Decode = f } } diff --git a/token.go b/token.go index 618a1c8..585770f 100644 --- a/token.go +++ b/token.go @@ -28,14 +28,19 @@ type VerificationKeySet struct { // Token represents a JWT Token. Different fields will be used depending on // whether you're creating or parsing/verifying a token. type Token struct { - Raw string // Raw contains the raw token. Populated when you [Parse] a token - Method SigningMethod // Method is the signing method used or to be used - Header map[string]interface{} // Header is the first segment of the token in decoded form - Claims Claims // Claims is the second segment of the token in decoded form - Signature []byte // Signature is the third segment of the token in decoded form. Populated when you Parse a token - Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token - jsonEncoder JSONMarshalFunc // jsonEncoder is the custom json encoder/decoder - base64Encoder Base64EncodeFunc // base64Encoder is the custom base64 encoder/decoder + Raw string // Raw contains the raw token. Populated when you [Parse] a token + Method SigningMethod // Method is the signing method used or to be used + Header map[string]interface{} // Header is the first segment of the token in decoded form + Claims Claims // Claims is the second segment of the token in decoded form + Signature []byte // Signature is the third segment of the token in decoded form. Populated when you Parse a token + Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token + + encoders +} + +type encoders struct { + jsonMarshal JSONMarshalFunc // jsonEncoder is the custom json encoder/decoder + base64Encode Base64EncodeFunc // base64Encoder is the custom base64 encoder/decoder } // 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) { // straight for the SignedString. func (t *Token) SigningString() (string, error) { var marshal JSONMarshalFunc - if t.jsonEncoder != nil { - marshal = t.jsonEncoder + if t.jsonMarshal != nil { + marshal = t.jsonMarshal } else { marshal = json.Marshal } @@ -110,8 +115,8 @@ func (t *Token) SigningString() (string, error) { // than a global function. func (t *Token) EncodeSegment(seg []byte) string { var enc Base64EncodeFunc - if t.base64Encoder != nil { - enc = t.base64Encoder + if t.base64Encode != nil { + enc = t.base64Encode } else { enc = base64.RawURLEncoding.EncodeToString } diff --git a/token_option.go b/token_option.go index 3e4c20b..3a9ca8d 100644 --- a/token_option.go +++ b/token_option.go @@ -6,12 +6,12 @@ type TokenOption func(*Token) func WithJSONEncoder(f JSONMarshalFunc) TokenOption { return func(token *Token) { - token.jsonEncoder = f + token.jsonMarshal = f } } func WithBase64Encoder(f Base64EncodeFunc) TokenOption { return func(token *Token) { - token.base64Encoder = f + token.base64Encode = f } }