Codebase list golang-github-go-kit-kit / 8864ce8
updates to reflect change in ServerRequestFunc Bas van Beek 7 years ago
4 changed file(s) with 11 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
4343
4444 // ToGRPCContext moves JWT token from grpc metadata to context. Particularly
4545 // 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 {
4848 // capital "Key" is illegal in HTTP/2.
49 authHeader, ok := (*md)["authorization"]
49 authHeader, ok := md["authorization"]
5050 if !ok {
5151 return ctx
5252 }
6262
6363 // FromGRPCContext moves JWT token from context to grpc metadata. Particularly
6464 // useful for clients.
65 func FromGRPCContext() grpc.RequestFunc {
65 func FromGRPCContext() grpc.ClientRequestFunc {
6666 return func(ctx context.Context, md *metadata.MD) context.Context {
6767 token, ok := ctx.Value(JWTTokenContextKey).(string)
6868 if ok {
6868 reqFunc := ToGRPCContext()
6969
7070 // No Authorization header is passed
71 ctx := reqFunc(context.Background(), &md)
71 ctx := reqFunc(context.Background(), md)
7272 token := ctx.Value(JWTTokenContextKey)
7373 if token != nil {
7474 t.Error("Context should not contain a JWT Token")
7676
7777 // Invalid Authorization header is passed
7878 md["authorization"] = []string{fmt.Sprintf("%s", signedKey)}
79 ctx = reqFunc(context.Background(), &md)
79 ctx = reqFunc(context.Background(), md)
8080 token = ctx.Value(JWTTokenContextKey)
8181 if token != nil {
8282 t.Error("Context should not contain a JWT Token")
8484
8585 // Authorization header is correct
8686 md["authorization"] = []string{fmt.Sprintf("Bearer %s", signedKey)}
87 ctx = reqFunc(context.Background(), &md)
87 ctx = reqFunc(context.Background(), md)
8888 token, ok := ctx.Value(JWTTokenContextKey).(string)
8989 if !ok {
9090 t.Fatal("JWT Token not passed to context correctly")
3131 // `operationName` accordingly. If no trace could be found in `req`, the Span
3232 // will be a trace root. The Span is incorporated in the returned Context and
3333 // 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 {
3636 var span opentracing.Span
37 wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{md})
37 wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{&md})
3838 if err != nil && err != opentracing.ErrSpanContextNotFound {
3939 logger.Log("err", err)
4040 }
4040
4141 // Use FromGRPCRequest to verify that we can join with the trace given MD.
4242 fromGRPCFunc := kitot.FromGRPCRequest(tracer, "joined", logger)
43 joinCtx := fromGRPCFunc(afterCtx, &md)
43 joinCtx := fromGRPCFunc(afterCtx, md)
4444 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
4545
4646 joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)