Codebase list golang-github-go-kit-kit / b2b278c
Fixes from PR requests. Cameron Stitt 7 years ago
1 changed file(s) with 35 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
33 "context"
44 "testing"
55
6 "crypto/subtle"
7
68 jwt "github.com/dgrijalva/jwt-go"
79 "github.com/go-kit/kit/endpoint"
810 )
911
12 type customClaims struct {
13 MyProperty string `json:"my_property"`
14 jwt.StandardClaims
15 }
16
17 func (c customClaims) VerifyMyProperty(p string) bool {
18 return subtle.ConstantTimeCompare([]byte(c.MyProperty), []byte(p)) != 0
19 }
20
1021 var (
1122 kid = "kid"
1223 key = []byte("test_signing_key")
24 myProperty = "some value"
1325 method = jwt.SigningMethodHS256
1426 invalidMethod = jwt.SigningMethodRS256
1527 mapClaims = jwt.MapClaims{"user": "go-kit"}
1628 standardClaims = jwt.StandardClaims{Audience: "go-kit"}
29 myCustomClaims = customClaims{MyProperty: myProperty, StandardClaims: standardClaims}
1730 // Signed tokens generated at https://jwt.io/
1831 signedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E"
1932 standardSignedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJnby1raXQifQ.L5ypIJjCOOv3jJ8G5SelaHvR04UJuxmcBN5QW3m_aoY"
33 customSignedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJteV9wcm9wZXJ0eSI6InNvbWUgdmFsdWUiLCJhdWQiOiJnby1raXQifQ.s8F-IDrV4WPJUsqr7qfDi-3GRlcKR0SRnkTeUT_U-i0"
2034 invalidKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.vKVCKto-Wn6rgz3vBdaZaCBGfCBDTXOENSo_X2Gq7qA"
2135 )
2236
4458
4559 signer = NewSigner(kid, key, method, standardClaims)(e)
4660 signingValidator(t, signer, standardSignedKey)
61
62 signer = NewSigner(kid, key, method, myCustomClaims)(e)
63 signingValidator(t, signer, customSignedKey)
4764 }
4865
4966 func TestJWTParser(t *testing.T) {
123140 t.Fatal("Claims were not passed into context correctly")
124141 }
125142 if !stdCl.VerifyAudience("go-kit", true) {
126 t.Fatal("JWT jwt.StandardClaims.Audience did not match: expecting %s got %s", standardClaims.Audience, stdCl.Audience)
143 t.Fatalf("JWT jwt.StandardClaims.Audience did not match: expecting %s got %s", standardClaims.Audience, stdCl.Audience)
144 }
145
146 parser = NewParser(keys, method, &customClaims{})(e)
147 ctx = context.WithValue(context.Background(), JWTTokenContextKey, customSignedKey)
148 ctx1, err = parser(ctx, struct{}{})
149 if err != nil {
150 t.Fatalf("Parser returned error: %s", err)
151 }
152 custCl, ok := ctx1.(context.Context).Value(JWTClaimsContextKey).(*customClaims)
153 if !ok {
154 t.Fatal("Claims were not passed into context correctly")
155 }
156 if !custCl.VerifyAudience("go-kit", true) {
157 t.Fatalf("JWT customClaims.Audience did not match: expecting %s got %s", standardClaims.Audience, custCl.Audience)
158 }
159 if !custCl.VerifyMyProperty(myProperty) {
160 t.Fatalf("JWT customClaims.MyProperty did not match: expecting %s got %s", myProperty, custCl.MyProperty)
127161 }
128162 }