Codebase list golang-github-go-kit-kit / 1b9d538
Simplify the passing of tokens by removing the metadata requirement Brian Kassouf 7 years ago
4 changed file(s) with 18 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
33 "errors"
44 "fmt"
55 "reflect"
6
7 "google.golang.org/grpc/metadata"
86
97 "golang.org/x/net/context"
108
3129 if err != nil {
3230 return nil, err
3331 }
34 md := metadata.MD{JWTTokenContextKey: []string{tokenString}}
35 ctx = metadata.NewContext(ctx, md)
32 ctx = context.WithValue(ctx, JWTTokenContextKey, tokenString)
3633
3734 return next(ctx, request)
3835 }
11
22 import (
33 "testing"
4
5 "google.golang.org/grpc/metadata"
64
75 stdjwt "github.com/dgrijalva/jwt-go"
86
2725 t.Fatalf("Signer returned error: %s", err)
2826 }
2927
30 md, ok := metadata.FromContext(ctx1.(context.Context))
31 if !ok {
32 t.Fatal("Could not retrieve metadata from context")
33 }
34
35 token, ok := md[jwt.JWTTokenContextKey]
28 token, ok := ctx1.(context.Context).Value(jwt.JWTTokenContextKey).(string)
3629 if !ok {
3730 t.Fatal("Token did not exist in context")
3831 }
3932
40 if token[0] != signedKey {
41 t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token[0])
33 if token != signedKey {
34 t.Fatalf("JWT tokens did not match: expecting %s got %s", signedKey, token)
4235 }
4336 }
4437
1010 "google.golang.org/grpc/metadata"
1111 )
1212
13 // moves JWT token from request header to context
14 // particularly useful for servers
1315 func ToHTTPContext() http.RequestFunc {
1416 return func(ctx context.Context, r *stdhttp.Request) context.Context {
1517 token, ok := extractTokenFromAuthHeader(r.Header.Get("Authorization"))
2123 }
2224 }
2325
26 // moves JWT token from context to request header
27 // particularly useful for clients
2428 func FromHTTPContext() http.RequestFunc {
2529 return func(ctx context.Context, r *stdhttp.Request) context.Context {
26 md1, ok := metadata.FromContext(ctx)
27 if !ok {
28 return ctx
30 token, ok := ctx.Value(JWTTokenContextKey).(string)
31 if ok {
32 r.Header.Add("Authorization", generateAuthHeaderFromToken(token))
2933 }
30
31 token, ok := md1[JWTTokenContextKey]
32 if ok {
33 r.Header.Add("Authorization", generateAuthHeaderFromToken(token[0]))
34 }
35 fmt.Println(r.Header)
3634 return ctx
3735 }
3836 }
3937
38 // moves JWT token from grpc metadata to context
39 // particularly userful for servers
4040 func ToGRPCContext() grpc.RequestFunc {
4141 return func(ctx context.Context, md *metadata.MD) context.Context {
4242 // capital "Key" is illegal in HTTP/2.
5454 }
5555 }
5656
57 // moves JWT token from context to grpc metadata
58 // particularly useful for clients
5759 func FromGRPCContext() grpc.RequestFunc {
5860 return func(ctx context.Context, md *metadata.MD) context.Context {
59 md1, ok := metadata.FromContext(ctx)
60 if !ok {
61 return ctx
62 }
63
64 token, ok := md1[JWTTokenContextKey]
61 token, ok := ctx.Value(JWTTokenContextKey).(string)
6562 if ok {
6663 // capital "Key" is illegal in HTTP/2.
67 (*md)["authorization"] = []string{generateAuthHeaderFromToken(token[0])}
64 (*md)["authorization"] = []string{generateAuthHeaderFromToken(token)}
6865 }
6966
7067 return ctx
5555 }
5656
5757 // Correct JWT Token is passed in the context
58 ctx = metadata.NewContext(context.Background(), metadata.MD{jwt.JWTTokenContextKey: []string{signedKey}})
58 ctx = context.WithValue(context.Background(), jwt.JWTTokenContextKey, signedKey)
5959 md = metadata.MD{}
6060 reqFunc(ctx, &md)
6161