Skip to content

Commit

Permalink
changed argument order to put claims type before keyfunc. this is eas…
Browse files Browse the repository at this point in the history
…ier to read when keyfunc is an inline closure
  • Loading branch information
dgrijalva committed Apr 12, 2016
1 parent 70eefe1 commit 5e270fa
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func ExampleParseWithClaims_customClaimsType() {

// sample token is expired. override time so it parses as valid
at(time.Unix(0, 0), func() {
token, err := jwt.ParseWithClaims(tokenString, func(token *jwt.Token) (interface{}, error) {
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
}, &MyCustomClaims{})
})

if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
fmt.Printf("%v %v", claims.Foo, claims.StandardClaims.ExpiresAt)
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Parser struct {
// keyFunc will receive the parsed token and should return the key for validating.
// If everything is kosher, err will be nil
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
return p.ParseWithClaims(tokenString, keyFunc, MapClaims{})
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
}

func (p *Parser) ParseWithClaims(tokenString string, keyFunc Keyfunc, claims Claims) (*Token, error) {
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
parts := strings.Split(tokenString, ".")
if len(parts) != 3 {
return nil, &ValidationError{err: "token contains an invalid number of segments", Errors: ValidationErrorMalformed}
Expand Down
4 changes: 2 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ func TestParser_Parse(t *testing.T) {
// Figure out correct claims type
switch data.claims.(type) {
case jwt.MapClaims:
token, err = parser.ParseWithClaims(data.tokenString, data.keyfunc, jwt.MapClaims{})
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)
case *jwt.StandardClaims:
token, err = parser.ParseWithClaims(data.tokenString, data.keyfunc, &jwt.StandardClaims{})
token, err = parser.ParseWithClaims(data.tokenString, &jwt.StandardClaims{}, data.keyfunc)
}

// Verify result matches expectation
Expand Down
8 changes: 4 additions & 4 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ var (
// Currently, it looks in the Authorization header as well as
// looking for an 'access_token' request parameter in req.Form.
func ParseFromRequest(req *http.Request, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
return ParseFromRequestWithClaims(req, keyFunc, &jwt.MapClaims{})
return ParseFromRequestWithClaims(req, jwt.MapClaims{}, keyFunc)
}

func ParseFromRequestWithClaims(req *http.Request, keyFunc jwt.Keyfunc, claims jwt.Claims) (token *jwt.Token, err error) {
func ParseFromRequestWithClaims(req *http.Request, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
// Look for an Authorization header
if ah := req.Header.Get("Authorization"); ah != "" {
// Should be a bearer token
if len(ah) > 6 && strings.ToUpper(ah[0:7]) == "BEARER " {
return jwt.ParseWithClaims(ah[7:], keyFunc, claims)
return jwt.ParseWithClaims(ah[7:], claims, keyFunc)
}
}

// Look for "access_token" parameter
req.ParseMultipartForm(10e6)
if tokStr := req.Form.Get("access_token"); tokStr != "" {
return jwt.ParseWithClaims(tokStr, keyFunc, claims)
return jwt.ParseWithClaims(tokStr, claims, keyFunc)
}

return nil, ErrNoTokenInRequest
Expand Down
2 changes: 1 addition & 1 deletion request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestParseRequest(t *testing.T) {
r.Header.Set(k, tokenString)
}
}
token, err := ParseFromRequestWithClaims(r, keyfunc, jwt.MapClaims{})
token, err := ParseFromRequestWithClaims(r, jwt.MapClaims{}, keyfunc)

if token == nil {
t.Errorf("[%v] Token was not found: %v", data.name, err)
Expand Down
4 changes: 2 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
return new(Parser).Parse(tokenString, keyFunc)
}

func ParseWithClaims(tokenString string, keyFunc Keyfunc, claims Claims) (*Token, error) {
return new(Parser).ParseWithClaims(tokenString, keyFunc, claims)
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
}

// Encode JWT specific base64url encoding with padding stripped
Expand Down

0 comments on commit 5e270fa

Please sign in to comment.