Skip to content

Commit 2101c1f

Browse files
authored
No pointer embedding in the example (#255)
Fixes #223
1 parent 35053d4 commit 2101c1f

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

http_example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type CustomerInfo struct {
7373
}
7474

7575
type CustomClaimsExample struct {
76-
*jwt.RegisteredClaims
76+
jwt.RegisteredClaims
7777
TokenType string
7878
CustomerInfo
7979
}
@@ -142,7 +142,7 @@ func createToken(user string) (string, error) {
142142

143143
// set our claims
144144
t.Claims = &CustomClaimsExample{
145-
&jwt.RegisteredClaims{
145+
jwt.RegisteredClaims{
146146
// set the expire time
147147
// see https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
148148
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 1)),

parser.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
4242
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
4343
}
4444

45-
// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default
46-
// object implementing the Claims interface. This provides default values which
47-
// can be overridden and allows a caller to use their own type, rather than
48-
// the default MapClaims implementation of Claims.
45+
// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object implementing the Claims
46+
// interface. This provides default values which can be overridden and allows a caller to use their own type, rather
47+
// than the default MapClaims implementation of Claims.
48+
//
49+
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
50+
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
51+
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
4952
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
5053
token, parts, err := p.ParseUnverified(tokenString, claims)
5154
if err != nil {

token.go

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token
9999
return NewParser(options...).Parse(tokenString, keyFunc)
100100
}
101101

102+
// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
103+
//
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.
102107
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
103108
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
104109
}

0 commit comments

Comments
 (0)