Codebase list golang-github-go-kit-kit / 3ef148a
Updated tests to use the new KeySet object Brian Kassouf 7 years ago
2 changed file(s) with 40 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
99 )
1010
1111 const (
12 // JWTContextKey holds the key used to store a JWT Token in the context
12 // JWTTokenContextKey holds the key used to store a JWT Token in the context
1313 JWTTokenContextKey = "JWTToken"
14 // JWTContextKey holds the key used to store a JWT in the context
14 // JWTClaimsContxtKey holds the key used to store the JWT Claims in the context
1515 JWTClaimsContextKey = "JWTClaims"
1616 )
1717
88 )
99
1010 var (
11 key = "test_signing_key"
11 kid = "kid"
12 key = []byte("test_signing_key")
1213 method = jwt.SigningMethodHS256
1314 invalidMethod = jwt.SigningMethodRS256
14 claims = jwt.MapClaims{"user": "go-kit"}
15 signedKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.MMefQU5pwDeoWBSdyagqNlr1tDGddGUOMGiIWmMlFvk"
15 claims = Claims{"user": "go-kit"}
16 signedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E"
1617 invalidKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.vKVCKto-Wn6rgz3vBdaZaCBGfCBDTXOENSo_X2Gq7qA"
1718 )
1819
1920 func TestSigner(t *testing.T) {
2021 e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
2122
22 signer := NewSigner(key, method, claims)(e)
23 ctx := context.Background()
24 ctx1, err := signer(ctx, struct{}{})
23 keys := KeySet{
24 kid: {
25 Method: method,
26 Key: key,
27 },
28 }
29
30 signer := NewSigner(kid, keys, claims)(e)
31 ctx, err := signer(context.Background(), struct{}{})
2532 if err != nil {
2633 t.Fatalf("Signer returned error: %s", err)
2734 }
2835
29 token, ok := ctx1.(context.Context).Value(JWTTokenContextKey).(string)
36 token, ok := ctx.(context.Context).Value(JWTTokenContextKey).(string)
3037 if !ok {
3138 t.Fatal("Token did not exist in context")
3239 }
3946 func TestJWTParser(t *testing.T) {
4047 e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
4148
42 keyfunc := func(token *jwt.Token) (interface{}, error) { return []byte(key), nil }
43 badKeyfunc := func(token *jwt.Token) (interface{}, error) { return []byte("bad"), nil }
49 keys := KeySet{
50 kid: {
51 Method: method,
52 Key: key,
53 },
54 }
4455
45 parser := NewParser(keyfunc, method)(e)
56 parser := NewParser(keys)(e)
4657
4758 // No Token is passed into the parser
4859 _, err := parser(context.Background(), struct{}{})
5869 }
5970
6071 // Invalid Method is used in the parser
61 badParser := NewParser(keyfunc, invalidMethod)(e)
72 invalidMethodKeys := KeySet{
73 kid: {
74 Method: invalidMethod,
75 Key: key,
76 },
77 }
78
79 badParser := NewParser(invalidMethodKeys)(e)
6280 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
6381 _, err = badParser(ctx, struct{}{})
6482 if err == nil {
6684 }
6785
6886 // Invalid key is used in the parser
69 badParser = NewParser(badKeyfunc, method)(e)
87 invalidKeys := KeySet{
88 kid: {
89 Method: method,
90 Key: []byte("bad"),
91 },
92 }
93
94 badParser = NewParser(invalidKeys)(e)
7095 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
7196 _, err = badParser(ctx, struct{}{})
7297 if err == nil {
80105 t.Fatalf("Parser returned error: %s", err)
81106 }
82107
83 cl, ok := ctx1.(context.Context).Value(JWTClaimsContextKey).(jwt.MapClaims)
108 cl, ok := ctx1.(context.Context).Value(JWTClaimsContextKey).(Claims)
84109 if !ok {
85110 t.Fatal("Claims were not passed into context correctly")
86111 }