Codebase list golang-github-go-kit-kit / aa677fd
Move jwtToken strings into a const variable & rename metadata key to 'authorization' Brian Kassouf 7 years ago
4 changed file(s) with 30 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1212 "github.com/go-kit/kit/endpoint"
1313 )
1414
15 const (
16 // JWTContextKey holds the key used to store a JWT Token in the context
17 JWTTokenContextKey = "JWTToken"
18 // JWTContextKey holds the key used to store a JWT in the context
19 JWTClaimsContextKey = "JWTClaims"
20 )
21
1522 // Create a new JWT token generating middleware, specifying signing method and the claims
1623 // you would like it to contain. Particulary useful for clients.
1724 func NewSigner(key string, method jwt.SigningMethod, claims jwt.Claims) endpoint.Middleware {
2431 if err != nil {
2532 return nil, err
2633 }
27 md := metadata.Pairs("jwtToken", tokenString)
34 md := metadata.MD{JWTTokenContextKey: []string{tokenString}}
2835 ctx = metadata.NewContext(ctx, md)
2936
3037 return next(ctx, request)
3946 return func(next endpoint.Endpoint) endpoint.Endpoint {
4047 return func(ctx context.Context, request interface{}) (response interface{}, err error) {
4148 // tokenString is stored in the context from the transport handlers
42 tokenString, ok := ctx.Value("jwtToken").(string)
49 tokenString, ok := ctx.Value(JWTTokenContextKey).(string)
4350 if !ok {
4451 return nil, errors.New("Token up for parsing was not passed through the context")
4552 }
6471 }
6572
6673 if claims, ok := token.Claims.(jwt.MapClaims); ok {
67 ctx = context.WithValue(ctx, "jwtClaims", claims)
74 ctx = context.WithValue(ctx, JWTClaimsContextKey, claims)
6875 }
6976
7077 return next(ctx, request)
3232 t.Fatal("Could not retrieve metadata from context")
3333 }
3434
35 token, ok := md["jwttoken"]
35 token, ok := md[jwt.JWTTokenContextKey]
3636 if !ok {
3737 t.Fatal("Token did not exist in context")
3838 }
4848 keyfunc := func(token *stdjwt.Token) (interface{}, error) { return []byte(key), nil }
4949
5050 parser := jwt.NewParser(keyfunc, method)(e)
51 ctx := context.WithValue(context.Background(), "jwtToken", signedKey)
51 ctx := context.WithValue(context.Background(), jwt.JWTTokenContextKey, signedKey)
5252 ctx1, err := parser(ctx, struct{}{})
5353 if err != nil {
5454 t.Fatalf("Parser returned error: %s", err)
5555 }
5656
57 cl, ok := ctx1.(context.Context).Value("jwtClaims").(stdjwt.MapClaims)
57 cl, ok := ctx1.(context.Context).Value(jwt.JWTClaimsContextKey).(stdjwt.MapClaims)
5858 if !ok {
5959 t.Fatal("Claims were not passed into context correctly")
6060 }
1717 return ctx
1818 }
1919
20 return context.WithValue(ctx, "jwtToken", token)
20 return context.WithValue(ctx, JWTTokenContextKey, token)
2121 }
2222 }
2323
2424 func ToGRPCContext() grpc.RequestFunc {
2525 return func(ctx context.Context, md *metadata.MD) context.Context {
26 authHeader, ok := (*md)["Authorization"]
26 // capital "Key" is illegal in HTTP/2.
27 authHeader, ok := (*md)["authorization"]
2728 if !ok {
2829 return ctx
2930 }
3031
3132 token, ok := extractTokenFromAuthHeader(authHeader[0])
3233 if ok {
33 ctx = context.WithValue(ctx, "jwtToken", token)
34 ctx = context.WithValue(ctx, JWTTokenContextKey, token)
3435 }
3536
3637 return ctx
4445 return ctx
4546 }
4647
47 token, ok := md1["jwttoken"]
48 token, ok := md1[JWTTokenContextKey]
4849 if ok {
49 (*md)["Authorization"] = []string{generateAuthHeaderFromToken(token[0])}
50 // capital "Key" is illegal in HTTP/2.
51 (*md)["authorization"] = []string{generateAuthHeaderFromToken(token[0])}
5052 }
5153
5254 return ctx
1111
1212 func TestToGRPCContext(t *testing.T) {
1313 md := metadata.MD{}
14 md["Authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
14 md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
1515 ctx := context.Background()
1616 reqFunc := jwt.ToGRPCContext()
1717
1818 ctx = reqFunc(ctx, &md)
19 token, ok := ctx.Value("jwtToken").(string)
19 token, ok := ctx.Value(jwt.JWTTokenContextKey).(string)
2020 if !ok {
2121 t.Fatal("JWT Token not passed to context correctly")
2222 }
2727 }
2828
2929 func TestFromGRPCContext(t *testing.T) {
30 ctx := context.WithValue(context.Background(), "jwtToken", signedKey)
30 ctx := metadata.NewContext(context.Background(), metadata.MD{jwt.JWTTokenContextKey: []string{signedKey}})
3131
3232 reqFunc := jwt.FromGRPCContext()
3333 md := metadata.MD{}
3434 reqFunc(ctx, &md)
35 token, ok := md["jwttoken"]
35
36 token, ok := md["authorization"]
3637 if !ok {
3738 t.Fatal("JWT Token not passed to metadata correctly")
3839 }
3940
40 if token[0] != signedKey {
41 if token[0] != generateAuthHeaderFromToken(signedKey) {
4142 t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
4243 }
4344 }
45
46 func generateAuthHeaderFromToken(token string) string {
47 return fmt.Sprintf("Bearer %s", token)
48 }