Extract encoders/decoders to extra struct

This commit is contained in:
Christian Banse 2023-09-13 16:53:08 +02:00 committed by Christian Banse
parent 8089d9eb78
commit 7a5e5d6efe
4 changed files with 32 additions and 23 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -34,8 +34,13 @@ type Token struct {
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
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
}

View File

@ -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
}
}