mirror of https://github.com/golang-jwt/jwt.git
Extract encoders/decoders to extra struct
This commit is contained in:
parent
8089d9eb78
commit
7a5e5d6efe
18
parser.go
18
parser.go
|
@ -20,14 +20,18 @@ type Parser struct {
|
||||||
|
|
||||||
validator *Validator
|
validator *Validator
|
||||||
|
|
||||||
|
decoders
|
||||||
|
}
|
||||||
|
|
||||||
|
type decoders struct {
|
||||||
|
jsonUnmarshal JSONUnmarshalFunc
|
||||||
|
base64Decode Base64DecodeFunc
|
||||||
|
|
||||||
// This field is disabled when using a custom base64 encoder.
|
// This field is disabled when using a custom base64 encoder.
|
||||||
decodeStrict bool
|
decodeStrict bool
|
||||||
|
|
||||||
// This field is disabled when using a custom base64 encoder.
|
// This field is disabled when using a custom base64 encoder.
|
||||||
decodePaddingAllowed bool
|
decodePaddingAllowed bool
|
||||||
|
|
||||||
unmarshalFunc JSONUnmarshalFunc
|
|
||||||
base64DecodeFunc Base64DecodeFunc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParser creates a new Parser with the specified options
|
// 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.
|
// Choose our JSON decoder. If no custom function is supplied, we use the standard library.
|
||||||
var unmarshal JSONUnmarshalFunc
|
var unmarshal JSONUnmarshalFunc
|
||||||
if p.unmarshalFunc != nil {
|
if p.jsonUnmarshal != nil {
|
||||||
unmarshal = p.unmarshalFunc
|
unmarshal = p.jsonUnmarshal
|
||||||
} else {
|
} else {
|
||||||
unmarshal = json.Unmarshal
|
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,
|
// take into account whether the [Parser] is configured with additional options,
|
||||||
// such as [WithStrictDecoding] or [WithPaddingAllowed].
|
// such as [WithStrictDecoding] or [WithPaddingAllowed].
|
||||||
func (p *Parser) DecodeSegment(seg string) ([]byte, error) {
|
func (p *Parser) DecodeSegment(seg string) ([]byte, error) {
|
||||||
if p.base64DecodeFunc != nil {
|
if p.base64Decode != nil {
|
||||||
return p.base64DecodeFunc(seg)
|
return p.base64Decode(seg)
|
||||||
}
|
}
|
||||||
|
|
||||||
encoding := base64.RawURLEncoding
|
encoding := base64.RawURLEncoding
|
||||||
|
|
|
@ -130,13 +130,13 @@ func WithStrictDecoding() ParserOption {
|
||||||
// WithJSONUnmarshal supports a custom [JSONUnmarshal] to use in parsing the JWT.
|
// WithJSONUnmarshal supports a custom [JSONUnmarshal] to use in parsing the JWT.
|
||||||
func WithJSONUnmarshal(f JSONUnmarshalFunc) ParserOption {
|
func WithJSONUnmarshal(f JSONUnmarshalFunc) ParserOption {
|
||||||
return func(p *Parser) {
|
return func(p *Parser) {
|
||||||
p.unmarshalFunc = f
|
p.jsonUnmarshal = f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithBase64Decoder supports a custom [Base64Decoder] to use in parsing the JWT.
|
// WithBase64Decoder supports a custom [Base64Decoder] to use in parsing the JWT.
|
||||||
func WithBase64Decoder(f Base64DecodeFunc) ParserOption {
|
func WithBase64Decoder(f Base64DecodeFunc) ParserOption {
|
||||||
return func(p *Parser) {
|
return func(p *Parser) {
|
||||||
p.base64DecodeFunc = f
|
p.base64Decode = f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
token.go
17
token.go
|
@ -34,8 +34,13 @@ type Token struct {
|
||||||
Claims Claims // Claims is the second 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
|
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
|
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
|
// 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.
|
// straight for the SignedString.
|
||||||
func (t *Token) SigningString() (string, error) {
|
func (t *Token) SigningString() (string, error) {
|
||||||
var marshal JSONMarshalFunc
|
var marshal JSONMarshalFunc
|
||||||
if t.jsonEncoder != nil {
|
if t.jsonMarshal != nil {
|
||||||
marshal = t.jsonEncoder
|
marshal = t.jsonMarshal
|
||||||
} else {
|
} else {
|
||||||
marshal = json.Marshal
|
marshal = json.Marshal
|
||||||
}
|
}
|
||||||
|
@ -110,8 +115,8 @@ func (t *Token) SigningString() (string, error) {
|
||||||
// than a global function.
|
// than a global function.
|
||||||
func (t *Token) EncodeSegment(seg []byte) string {
|
func (t *Token) EncodeSegment(seg []byte) string {
|
||||||
var enc Base64EncodeFunc
|
var enc Base64EncodeFunc
|
||||||
if t.base64Encoder != nil {
|
if t.base64Encode != nil {
|
||||||
enc = t.base64Encoder
|
enc = t.base64Encode
|
||||||
} else {
|
} else {
|
||||||
enc = base64.RawURLEncoding.EncodeToString
|
enc = base64.RawURLEncoding.EncodeToString
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,12 @@ type TokenOption func(*Token)
|
||||||
|
|
||||||
func WithJSONEncoder(f JSONMarshalFunc) TokenOption {
|
func WithJSONEncoder(f JSONMarshalFunc) TokenOption {
|
||||||
return func(token *Token) {
|
return func(token *Token) {
|
||||||
token.jsonEncoder = f
|
token.jsonMarshal = f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithBase64Encoder(f Base64EncodeFunc) TokenOption {
|
func WithBase64Encoder(f Base64EncodeFunc) TokenOption {
|
||||||
return func(token *Token) {
|
return func(token *Token) {
|
||||||
token.base64Encoder = f
|
token.base64Encode = f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue