-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auth/jwt: MapClaims: passing #568
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ func TestJWTParser(t *testing.T) { | |
return key, nil | ||
} | ||
|
||
parser := NewParser(keys, method, jwt.MapClaims{})(e) | ||
parser := NewParser(keys, method, func() jwt.Claims { return jwt.MapClaims{} })(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This anonymous func should be provided as a top-level function in the package, e.g. // MapClaimsFactory is a ClaimsFactory that returns
// an empty jwt.MapClaims.
func MapClaimsFactory() jwt.Claims {
return jwt.MapClaims{}
} |
||
|
||
// No Token is passed into the parser | ||
_, err := parser(context.Background(), struct{}{}) | ||
|
@@ -94,7 +94,7 @@ func TestJWTParser(t *testing.T) { | |
} | ||
|
||
// Invalid Method is used in the parser | ||
badParser := NewParser(keys, invalidMethod, jwt.MapClaims{})(e) | ||
badParser := NewParser(keys, invalidMethod, func() jwt.Claims { return jwt.MapClaims{} })(e) | ||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey) | ||
_, err = badParser(ctx, struct{}{}) | ||
if err == nil { | ||
|
@@ -110,7 +110,7 @@ func TestJWTParser(t *testing.T) { | |
return []byte("bad"), nil | ||
} | ||
|
||
badParser = NewParser(invalidKeys, method, jwt.MapClaims{})(e) | ||
badParser = NewParser(invalidKeys, method, func() jwt.Claims { return jwt.MapClaims{} })(e) | ||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey) | ||
_, err = badParser(ctx, struct{}{}) | ||
if err == nil { | ||
|
@@ -134,15 +134,15 @@ func TestJWTParser(t *testing.T) { | |
} | ||
|
||
// Test for malformed token error response | ||
parser = NewParser(keys, method, &jwt.StandardClaims{})(e) | ||
parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, this anonymous func should be provided as a top-level function in the package, e.g. // StandardClaimsFactory is a ClaimsFactory that returns
// an empty jwt.StandardClaims.
func StandardClaimsFactory() jwt.Claims {
return &jwt.StandardClaims{}
} |
||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, malformedKey) | ||
ctx1, err = parser(ctx, struct{}{}) | ||
if want, have := ErrTokenMalformed, err; want != have { | ||
t.Fatalf("Expected %+v, got %+v", want, have) | ||
} | ||
|
||
// Test for expired token error response | ||
parser = NewParser(keys, method, &jwt.StandardClaims{})(e) | ||
parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e) | ||
expired := jwt.NewWithClaims(method, jwt.StandardClaims{ExpiresAt: time.Now().Unix() - 100}) | ||
token, err := expired.SignedString(key) | ||
if err != nil { | ||
|
@@ -155,7 +155,7 @@ func TestJWTParser(t *testing.T) { | |
} | ||
|
||
// Test for not activated token error response | ||
parser = NewParser(keys, method, &jwt.StandardClaims{})(e) | ||
parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e) | ||
notactive := jwt.NewWithClaims(method, jwt.StandardClaims{NotBefore: time.Now().Unix() + 100}) | ||
token, err = notactive.SignedString(key) | ||
if err != nil { | ||
|
@@ -168,7 +168,7 @@ func TestJWTParser(t *testing.T) { | |
} | ||
|
||
// test valid standard claims token | ||
parser = NewParser(keys, method, &jwt.StandardClaims{})(e) | ||
parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e) | ||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, standardSignedKey) | ||
ctx1, err = parser(ctx, struct{}{}) | ||
if err != nil { | ||
|
@@ -183,7 +183,7 @@ func TestJWTParser(t *testing.T) { | |
} | ||
|
||
// test valid customized claims token | ||
parser = NewParser(keys, method, &customClaims{})(e) | ||
parser = NewParser(keys, method, func() jwt.Claims { return &customClaims{} })(e) | ||
ctx = context.WithValue(context.Background(), JWTTokenContextKey, customSignedKey) | ||
ctx1, err = parser(ctx, struct{}{}) | ||
if err != nil { | ||
|
@@ -204,7 +204,7 @@ func TestJWTParser(t *testing.T) { | |
func TestIssue562(t *testing.T) { | ||
var ( | ||
kf = func(token *jwt.Token) (interface{}, error) { return []byte("secret"), nil } | ||
e = NewParser(kf, jwt.SigningMethodHS256, jwt.MapClaims{})(endpoint.Nop) | ||
e = NewParser(kf, jwt.SigningMethodHS256, func() jwt.Claims { return jwt.MapClaims{} })(endpoint.Nop) | ||
key = JWTTokenContextKey | ||
val = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E" | ||
ctx = context.WithValue(context.Background(), key, val) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be exported, ClaimsFactory.