@@ -6,42 +6,49 @@ import (
66 "strings"
77)
88
9- // DecodePaddingAllowed will switch the codec used for decoding JWTs respectively. Note that the JWS RFC7515
10- // states that the tokens will utilize a Base64url encoding with no padding. Unfortunately, some implementations
11- // of JWT are producing non-standard tokens, and thus require support for decoding. Note that this is a global
12- // variable, and updating it will change the behavior on a package level, and is also NOT go-routine safe.
13- // To use the non-recommended decoding, set this boolean to `true` prior to using this package.
9+ // DecodePaddingAllowed will switch the codec used for decoding JWTs
10+ // respectively. Note that the JWS RFC7515 states that the tokens will utilize a
11+ // Base64url encoding with no padding. Unfortunately, some implementations of
12+ // JWT are producing non-standard tokens, and thus require support for decoding.
13+ // Note that this is a global variable, and updating it will change the behavior
14+ // on a package level, and is also NOT go-routine safe. To use the
15+ // non-recommended decoding, set this boolean to `true` prior to using this
16+ // package.
1417var DecodePaddingAllowed bool
1518
1619// DecodeStrict will switch the codec used for decoding JWTs into strict mode.
17- // In this mode, the decoder requires that trailing padding bits are zero, as described in RFC 4648 section 3.5.
18- // Note that this is a global variable, and updating it will change the behavior on a package level, and is also NOT go-routine safe.
19- // To use strict decoding, set this boolean to `true` prior to using this package.
20+ // In this mode, the decoder requires that trailing padding bits are zero, as
21+ // described in RFC 4648 section 3.5. Note that this is a global variable, and
22+ // updating it will change the behavior on a package level, and is also NOT
23+ // go-routine safe. To use strict decoding, set this boolean to `true` prior to
24+ // using this package.
2025var DecodeStrict bool
2126
2227// Keyfunc will be used by the Parse methods as a callback function to supply
23- // the key for verification. The function receives the parsed,
24- // but unverified Token. This allows you to use properties in the
25- // Header of the token (such as `kid`) to identify which key to use.
28+ // the key for verification. The function receives the parsed, but unverified
29+ // Token. This allows you to use properties in the Header of the token (such as
30+ // `kid`) to identify which key to use.
2631type Keyfunc func (* Token ) (interface {}, error )
2732
28- // Token represents a JWT Token. Different fields will be used depending on whether you're
29- // creating or parsing/verifying a token.
33+ // Token represents a JWT Token. Different fields will be used depending on
34+ // whether you're creating or parsing/verifying a token.
3035type Token struct {
31- Raw string // The raw token. Populated when you Parse a token
32- Method SigningMethod // The signing method used or to be used
33- Header map [string ]interface {} // The first segment of the token
34- Claims Claims // The second segment of the token
35- Signature string // The third segment of the token. Populated when you Parse a token
36- Valid bool // Is the token valid? Populated when you Parse/Verify a token
36+ Raw string // Raw contains the raw token. Populated when you [ Parse] a token
37+ Method SigningMethod // Method is the signing method used or to be used
38+ Header map [string ]interface {} // Header is the first segment of the token
39+ Claims Claims // Claims is the second segment of the token
40+ Signature string // Signature is the third segment of the token. Populated when you Parse a token
41+ Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
3742}
3843
39- // New creates a new Token with the specified signing method and an empty map of claims.
44+ // New creates a new [Token] with the specified signing method and an empty map of
45+ // claims.
4046func New (method SigningMethod ) * Token {
4147 return NewWithClaims (method , MapClaims {})
4248}
4349
44- // NewWithClaims creates a new Token with the specified signing method and claims.
50+ // NewWithClaims creates a new [Token] with the specified signing method and
51+ // claims.
4552func NewWithClaims (method SigningMethod , claims Claims ) * Token {
4653 return & Token {
4754 Header : map [string ]interface {}{
@@ -53,8 +60,8 @@ func NewWithClaims(method SigningMethod, claims Claims) *Token {
5360 }
5461}
5562
56- // SignedString creates and returns a complete, signed JWT.
57- // The token is signed using the SigningMethod specified in the token.
63+ // SignedString creates and returns a complete, signed JWT. The token is signed
64+ // using the SigningMethod specified in the token.
5865func (t * Token ) SignedString (key interface {}) (string , error ) {
5966 var sig , sstr string
6067 var err error
@@ -67,10 +74,9 @@ func (t *Token) SignedString(key interface{}) (string, error) {
6774 return strings .Join ([]string {sstr , sig }, "." ), nil
6875}
6976
70- // SigningString generates the signing string. This is the
71- // most expensive part of the whole deal. Unless you
72- // need this for something special, just go straight for
73- // the SignedString.
77+ // SigningString generates the signing string. This is the most expensive part
78+ // of the whole deal. Unless you need this for something special, just go
79+ // straight for the SignedString.
7480func (t * Token ) SigningString () (string , error ) {
7581 var err error
7682 var jsonValue []byte
@@ -90,36 +96,38 @@ func (t *Token) SigningString() (string, error) {
9096
9197// Parse parses, validates, verifies the signature and returns the parsed token.
9298// keyFunc will receive the parsed token and should return the cryptographic key
93- // for verifying the signature.
94- // The caller is strongly encouraged to set the WithValidMethods option to
95- // validate the 'alg' claim in the token matches the expected algorithm.
96- // For more details about the importance of validating the 'alg' claim,
97- // see https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
99+ // for verifying the signature. The caller is strongly encouraged to set the
100+ // WithValidMethods option to validate the 'alg' claim in the token matches the
101+ // expected algorithm. For more details about the importance of validating the
102+ // 'alg' claim, see
103+ // https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
98104func Parse (tokenString string , keyFunc Keyfunc , options ... ParserOption ) (* Token , error ) {
99105 return NewParser (options ... ).Parse (tokenString , keyFunc )
100106}
101107
102108// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
103109//
104- // Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
105- // make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
106- // proper memory for it before passing in the overall claims, otherwise you might run into a panic.
110+ // Note: If you provide a custom claim implementation that embeds one of the
111+ // standard claims (such as RegisteredClaims), make sure that a) you either
112+ // embed a non-pointer version of the claims or b) if you are using a pointer,
113+ // allocate the proper memory for it before passing in the overall claims,
114+ // otherwise you might run into a panic.
107115func ParseWithClaims (tokenString string , claims Claims , keyFunc Keyfunc , options ... ParserOption ) (* Token , error ) {
108116 return NewParser (options ... ).ParseWithClaims (tokenString , claims , keyFunc )
109117}
110118
111119// EncodeSegment encodes a JWT specific base64url encoding with padding stripped
112120//
113- // Deprecated: In a future release, we will demote this function to a non-exported function, since it
114- // should only be used internally
121+ // Deprecated: In a future release, we will demote this function to a
122+ // non-exported function, since it should only be used internally
115123func EncodeSegment (seg []byte ) string {
116124 return base64 .RawURLEncoding .EncodeToString (seg )
117125}
118126
119127// DecodeSegment decodes a JWT specific base64url encoding with padding stripped
120128//
121- // Deprecated: In a future release, we will demote this function to a non-exported function, since it
122- // should only be used internally
129+ // Deprecated: In a future release, we will demote this function to a
130+ // non-exported function, since it should only be used internally
123131func DecodeSegment (seg string ) ([]byte , error ) {
124132 encoding := base64 .RawURLEncoding
125133
0 commit comments