Skip to content

Commit

Permalink
Extract encoders/decoders to extra struct
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 13, 2023
1 parent 836be3f commit d81e689
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
18 changes: 11 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions parser_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,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
}
}
29 changes: 17 additions & 12 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions token_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit d81e689

Please sign in to comment.