Skip to content

Commit c0ffb89

Browse files
Improve code comments, including security consideration (#107)
* improve code comments, including security consideration * Add link to URL with details about security vulnerabilities. * Update token.go Co-authored-by: Christian Banse <[email protected]> * Update token.go Co-authored-by: Christian Banse <[email protected]> * update code comments Co-authored-by: Christian Banse <[email protected]>
1 parent 65357b9 commit c0ffb89

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

parser.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ func NewParser(options ...ParserOption) *Parser {
3636
return p
3737
}
3838

39-
// Parse parses, validates, and returns a token.
39+
// Parse parses, validates, verifies the signature and returns the parsed token.
4040
// keyFunc will receive the parsed token and should return the key for validating.
41-
// If everything is kosher, err will be nil
4241
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
4342
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
4443
}

token.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ type Token struct {
2929
Valid bool // Is the token valid? Populated when you Parse/Verify a token
3030
}
3131

32-
// New creates a new Token. Takes a signing method
32+
// New creates a new Token with the specified signing method and an empty map of claims.
3333
func New(method SigningMethod) *Token {
3434
return NewWithClaims(method, MapClaims{})
3535
}
3636

37+
// NewWithClaims creates a new Token with the specified signing method and claims.
3738
func NewWithClaims(method SigningMethod, claims Claims) *Token {
3839
return &Token{
3940
Header: map[string]interface{}{
@@ -45,7 +46,8 @@ func NewWithClaims(method SigningMethod, claims Claims) *Token {
4546
}
4647
}
4748

48-
// SignedString retrieves the complete, signed token
49+
// SignedString creates and returns a complete, signed JWT.
50+
// The token is signed using the SigningMethod specified in the token.
4951
func (t *Token) SignedString(key interface{}) (string, error) {
5052
var sig, sstr string
5153
var err error
@@ -82,9 +84,13 @@ func (t *Token) SigningString() (string, error) {
8284
return strings.Join(parts, "."), nil
8385
}
8486

85-
// Parse parses, validates, and returns a token.
86-
// keyFunc will receive the parsed token and should return the key for validating.
87-
// If everything is kosher, err will be nil
87+
// Parse parses, validates, verifies the signature and returns the parsed token.
88+
// keyFunc will receive the parsed token and should return the cryptographic key
89+
// for verifying the signature.
90+
// The caller is strongly encouraged to set the WithValidMethods option to
91+
// validate the 'alg' claim in the token matches the expected algorithm.
92+
// For more details about the importance of validating the 'alg' claim,
93+
// see https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
8894
func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
8995
return NewParser(options...).Parse(tokenString, keyFunc)
9096
}

0 commit comments

Comments
 (0)