From 5e270fa6cdfaa49531d766eab8061de542376780 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Tue, 12 Apr 2016 16:25:25 -0700 Subject: [PATCH] changed argument order to put claims type before keyfunc. this is easier to read when keyfunc is an inline closure --- example_test.go | 4 ++-- parser.go | 4 ++-- parser_test.go | 4 ++-- request/request.go | 8 ++++---- request/request_test.go | 2 +- token.go | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/example_test.go b/example_test.go index c1e399c..ae8b788 100644 --- a/example_test.go +++ b/example_test.go @@ -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) diff --git a/parser.go b/parser.go index 884fea9..ab3b234 100644 --- a/parser.go +++ b/parser.go @@ -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} diff --git a/parser_test.go b/parser_test.go index 3736b09..03e3b2c 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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 diff --git a/request/request.go b/request/request.go index c812488..98e1be2 100644 --- a/request/request.go +++ b/request/request.go @@ -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 diff --git a/request/request_test.go b/request/request_test.go index 0f2fb9b..6b09521 100644 --- a/request/request_test.go +++ b/request/request_test.go @@ -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) diff --git a/token.go b/token.go index 7ac24fa..5eb365c 100644 --- a/token.go +++ b/token.go @@ -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