updates to reflect change in ServerRequestFunc
Bas van Beek
6 years ago
43 | 43 |
|
44 | 44 |
// ToGRPCContext moves JWT token from grpc metadata to context. Particularly
|
45 | 45 |
// userful for servers.
|
46 | |
func ToGRPCContext() grpc.RequestFunc {
|
47 | |
return func(ctx context.Context, md *metadata.MD) context.Context {
|
|
46 |
func ToGRPCContext() grpc.ServerRequestFunc {
|
|
47 |
return func(ctx context.Context, md metadata.MD) context.Context {
|
48 | 48 |
// capital "Key" is illegal in HTTP/2.
|
49 | |
authHeader, ok := (*md)["authorization"]
|
|
49 |
authHeader, ok := md["authorization"]
|
50 | 50 |
if !ok {
|
51 | 51 |
return ctx
|
52 | 52 |
}
|
|
62 | 62 |
|
63 | 63 |
// FromGRPCContext moves JWT token from context to grpc metadata. Particularly
|
64 | 64 |
// useful for clients.
|
65 | |
func FromGRPCContext() grpc.RequestFunc {
|
|
65 |
func FromGRPCContext() grpc.ClientRequestFunc {
|
66 | 66 |
return func(ctx context.Context, md *metadata.MD) context.Context {
|
67 | 67 |
token, ok := ctx.Value(JWTTokenContextKey).(string)
|
68 | 68 |
if ok {
|
68 | 68 |
reqFunc := ToGRPCContext()
|
69 | 69 |
|
70 | 70 |
// No Authorization header is passed
|
71 | |
ctx := reqFunc(context.Background(), &md)
|
|
71 |
ctx := reqFunc(context.Background(), md)
|
72 | 72 |
token := ctx.Value(JWTTokenContextKey)
|
73 | 73 |
if token != nil {
|
74 | 74 |
t.Error("Context should not contain a JWT Token")
|
|
76 | 76 |
|
77 | 77 |
// Invalid Authorization header is passed
|
78 | 78 |
md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
|
79 | |
ctx = reqFunc(context.Background(), &md)
|
|
79 |
ctx = reqFunc(context.Background(), md)
|
80 | 80 |
token = ctx.Value(JWTTokenContextKey)
|
81 | 81 |
if token != nil {
|
82 | 82 |
t.Error("Context should not contain a JWT Token")
|
|
84 | 84 |
|
85 | 85 |
// Authorization header is correct
|
86 | 86 |
md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
|
87 | |
ctx = reqFunc(context.Background(), &md)
|
|
87 |
ctx = reqFunc(context.Background(), md)
|
88 | 88 |
token, ok := ctx.Value(JWTTokenContextKey).(string)
|
89 | 89 |
if !ok {
|
90 | 90 |
t.Fatal("JWT Token not passed to context correctly")
|
31 | 31 |
// `operationName` accordingly. If no trace could be found in `req`, the Span
|
32 | 32 |
// will be a trace root. The Span is incorporated in the returned Context and
|
33 | 33 |
// can be retrieved with opentracing.SpanFromContext(ctx).
|
34 | |
func FromGRPCRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context {
|
35 | |
return func(ctx context.Context, md *metadata.MD) context.Context {
|
|
34 |
func FromGRPCRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md metadata.MD) context.Context {
|
|
35 |
return func(ctx context.Context, md metadata.MD) context.Context {
|
36 | 36 |
var span opentracing.Span
|
37 | |
wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{md})
|
|
37 |
wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{&md})
|
38 | 38 |
if err != nil && err != opentracing.ErrSpanContextNotFound {
|
39 | 39 |
logger.Log("err", err)
|
40 | 40 |
}
|
40 | 40 |
|
41 | 41 |
// Use FromGRPCRequest to verify that we can join with the trace given MD.
|
42 | 42 |
fromGRPCFunc := kitot.FromGRPCRequest(tracer, "joined", logger)
|
43 | |
joinCtx := fromGRPCFunc(afterCtx, &md)
|
|
43 |
joinCtx := fromGRPCFunc(afterCtx, md)
|
44 | 44 |
joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
|
45 | 45 |
|
46 | 46 |
joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)
|