@@ -4,20 +4,34 @@ import (
44 "context"
55 "testing"
66
7+ "crypto/subtle"
8+ 79 jwt "github.com/dgrijalva/jwt-go"
810 "github.com/go-kit/kit/endpoint"
911)
1012
13+ type customClaims struct {
14+ MyProperty string `json:"my_property"`
15+ jwt.StandardClaims
16+ }
17+ 18+ func (c customClaims ) VerifyMyProperty (p string ) bool {
19+ return subtle .ConstantTimeCompare ([]byte (c .MyProperty ), []byte (p )) != 0
20+ }
21+ 1122var (
1223 kid = "kid"
1324 key = []byte ("test_signing_key" )
25+ myProperty = "some value"
1426 method = jwt .SigningMethodHS256
1527 invalidMethod = jwt .SigningMethodRS256
1628 mapClaims = jwt.MapClaims {"user" : "go-kit" }
1729 standardClaims = jwt.StandardClaims {Audience : "go-kit" }
30+ myCustomClaims = customClaims {MyProperty : myProperty , StandardClaims : standardClaims }
1831 // Signed tokens generated at https://jwt.io/
1932 signedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E"
2033 standardSignedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJnby1raXQifQ.L5ypIJjCOOv3jJ8G5SelaHvR04UJuxmcBN5QW3m_aoY"
34+ customSignedKey = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJteV9wcm9wZXJ0eSI6InNvbWUgdmFsdWUiLCJhdWQiOiJnby1raXQifQ.s8F-IDrV4WPJUsqr7qfDi-3GRlcKR0SRnkTeUT_U-i0"
2135 invalidKey = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.vKVCKto-Wn6rgz3vBdaZaCBGfCBDTXOENSo_X2Gq7qA"
2236)
2337
@@ -45,6 +59,9 @@ func TestNewSigner(t *testing.T) {
4559
4660 signer = NewSigner (kid , key , method , standardClaims )(e )
4761 signingValidator (t , signer , standardSignedKey )
62+ 63+ signer = NewSigner (kid , key , method , myCustomClaims )(e )
64+ signingValidator (t , signer , customSignedKey )
4865}
4966
5067func TestJWTParser (t * testing.T ) {
@@ -124,6 +141,23 @@ func TestJWTParser(t *testing.T) {
124141 t .Fatal ("Claims were not passed into context correctly" )
125142 }
126143 if ! stdCl .VerifyAudience ("go-kit" , true ) {
127- t .Fatal ("JWT jwt.StandardClaims.Audience did not match: expecting %s got %s" , standardClaims .Audience , stdCl .Audience )
144+ t .Fatalf ("JWT jwt.StandardClaims.Audience did not match: expecting %s got %s" , standardClaims .Audience , stdCl .Audience )
145+ }
146+ 147+ parser = NewParser (keys , method , & customClaims {})(e )
148+ ctx = context .WithValue (context .Background (), JWTTokenContextKey , customSignedKey )
149+ ctx1 , err = parser (ctx , struct {}{})
150+ if err != nil {
151+ t .Fatalf ("Parser returned error: %s" , err )
152+ }
153+ custCl , ok := ctx1 .(context.Context ).Value (JWTClaimsContextKey ).(* customClaims )
154+ if ! ok {
155+ t .Fatal ("Claims were not passed into context correctly" )
156+ }
157+ if ! custCl .VerifyAudience ("go-kit" , true ) {
158+ t .Fatalf ("JWT customClaims.Audience did not match: expecting %s got %s" , standardClaims .Audience , custCl .Audience )
159+ }
160+ if ! custCl .VerifyMyProperty (myProperty ) {
161+ t .Fatalf ("JWT customClaims.MyProperty did not match: expecting %s got %s" , myProperty , custCl .MyProperty )
128162 }
129163}
0 commit comments