Package list golang-github-go-kit-kit / 44bb404
auth/jwt: MapClaims: export ClaimsFactory and provide implementations for Map and Standard claims factories Jaco Esterhuizen 4 years ago
2 changed file(s) with 22 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
6565 }
6666 }
6767
68 type claimsFactory func() jwt.Claims
68 type ClaimsFactory func() jwt.Claims
69
70 // MapClaimsFactory is a ClaimsFactory that returns
71 // an empty jwt.MapClaims.
72 func MapClaimsFactory() jwt.Claims {
73 return jwt.MapClaims{}
74 }
75
76 // StandardClaimsFactory is a ClaimsFactory that returns
77 // an empty jwt.StandardClaims.
78 func StandardClaimsFactory() jwt.Claims {
79 return &jwt.StandardClaims{}
80 }
6981
7082 // NewParser creates a new JWT token parsing middleware, specifying a
7183 // jwt.Keyfunc interface, the signing method and the claims type to be used. NewParser
7284 // adds the resulting claims to endpoint context or returns error on invalid token.
7385 // Particularly useful for servers.
74 func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod, newClaims claimsFactory) endpoint.Middleware {
86 func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod, newClaims ClaimsFactory) endpoint.Middleware {
7587 return func(next endpoint.Endpoint) endpoint.Endpoint {
7688 return func(ctx context.Context, request interface{}) (response interface{}, err error) {
7789 // tokenString is stored in the context from the transport handlers.
7373 return key, nil
7474 }
7575
76 parser := NewParser(keys, method, func() jwt.Claims { return jwt.MapClaims{} })(e)
76 parser := NewParser(keys, method, MapClaimsFactory)(e)
7777
7878 // No Token is passed into the parser
7979 _, err := parser(context.Background(), struct{}{})
9393 }
9494
9595 // Invalid Method is used in the parser
96 badParser := NewParser(keys, invalidMethod, func() jwt.Claims { return jwt.MapClaims{} })(e)
96 badParser := NewParser(keys, invalidMethod, MapClaimsFactory)(e)
9797 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
9898 _, err = badParser(ctx, struct{}{})
9999 if err == nil {
109109 return []byte("bad"), nil
110110 }
111111
112 badParser = NewParser(invalidKeys, method, func() jwt.Claims { return jwt.MapClaims{} })(e)
112 badParser = NewParser(invalidKeys, method, MapClaimsFactory)(e)
113113 ctx = context.WithValue(context.Background(), JWTTokenContextKey, signedKey)
114114 _, err = badParser(ctx, struct{}{})
115115 if err == nil {
133133 }
134134
135135 // Test for malformed token error response
136 parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e)
136 parser = NewParser(keys, method, StandardClaimsFactory)(e)
137137 ctx = context.WithValue(context.Background(), JWTTokenContextKey, malformedKey)
138138 ctx1, err = parser(ctx, struct{}{})
139139 if want, have := ErrTokenMalformed, err; want != have {
141141 }
142142
143143 // Test for expired token error response
144 parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e)
144 parser = NewParser(keys, method, StandardClaimsFactory)(e)
145145 expired := jwt.NewWithClaims(method, jwt.StandardClaims{ExpiresAt: time.Now().Unix() - 100})
146146 token, err := expired.SignedString(key)
147147 if err != nil {
154154 }
155155
156156 // Test for not activated token error response
157 parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e)
157 parser = NewParser(keys, method, StandardClaimsFactory)(e)
158158 notactive := jwt.NewWithClaims(method, jwt.StandardClaims{NotBefore: time.Now().Unix() + 100})
159159 token, err = notactive.SignedString(key)
160160 if err != nil {
167167 }
168168
169169 // test valid standard claims token
170 parser = NewParser(keys, method, func() jwt.Claims { return &jwt.StandardClaims{} })(e)
170 parser = NewParser(keys, method, StandardClaimsFactory)(e)
171171 ctx = context.WithValue(context.Background(), JWTTokenContextKey, standardSignedKey)
172172 ctx1, err = parser(ctx, struct{}{})
173173 if err != nil {
203203 func TestIssue562(t *testing.T) {
204204 var (
205205 kf = func(token *jwt.Token) (interface{}, error) { return []byte("secret"), nil }
206 e = NewParser(kf, jwt.SigningMethodHS256, func() jwt.Claims { return jwt.MapClaims{} })(endpoint.Nop)
206 e = NewParser(kf, jwt.SigningMethodHS256, MapClaimsFactory)(endpoint.Nop)
207207 key = JWTTokenContextKey
208208 val = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtpZCIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiZ28ta2l0In0.14M2VmYyApdSlV_LZ88ajjwuaLeIFplB8JpyNy0A19E"
209209 ctx = context.WithValue(context.Background(), key, val)