Skip to content

Commit

Permalink
No pointer embedding in the example (golang-jwt#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Mar 29, 2023
1 parent 057b27f commit e4fcde4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions http_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type CustomerInfo struct {
}

type CustomClaimsExample struct {
*jwt.RegisteredClaims
jwt.RegisteredClaims
TokenType string
CustomerInfo
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func createToken(user string) (string, error) {

// set our claims
t.Claims = &CustomClaimsExample{
&jwt.RegisteredClaims{
jwt.RegisteredClaims{
// set the expire time
// see https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute * 1)),
Expand Down
11 changes: 7 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
}

// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default
// object implementing the Claims interface. This provides default values which
// can be overridden and allows a caller to use their own type, rather than
// the default MapClaims implementation of Claims.
// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object implementing the Claims
// interface. This provides default values which can be overridden and allows a caller to use their own type, rather
// than the default MapClaims implementation of Claims.
//
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
token, parts, err := p.ParseUnverified(tokenString, claims)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func Parse(tokenString string, keyFunc Keyfunc, options ...ParserOption) (*Token
return NewParser(options...).Parse(tokenString, keyFunc)
}

// ParseWithClaims is a shortcut for NewParser().ParseWithClaims().
//
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options ...ParserOption) (*Token, error) {
return NewParser(options...).ParseWithClaims(tokenString, claims, keyFunc)
}
Expand Down

0 comments on commit e4fcde4

Please sign in to comment.