Package list golang-github-go-kit-kit / f78850e
auth/jwt: use improved FooToBar syntax Peter Bourgon 4 years ago
2 changed file(s) with 16 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1616 bearerFormat string = "Bearer %s"
1717 )
1818
19 // ToHTTPContext moves JWT token from request header to context. Particularly
19 // HTTPToContext moves JWT token from request header to context. Particularly
2020 // useful for servers.
21 func ToHTTPContext() http.RequestFunc {
21 func HTTPToContext() http.RequestFunc {
2222 return func(ctx context.Context, r *stdhttp.Request) context.Context {
2323 token, ok := extractTokenFromAuthHeader(r.Header.Get("Authorization"))
2424 if !ok {
2929 }
3030 }
3131
32 // FromHTTPContext moves JWT token from context to request header. Particularly
32 // ContextToHTTP moves JWT token from context to request header. Particularly
3333 // useful for clients.
34 func FromHTTPContext() http.RequestFunc {
34 func ContextToHTTP() http.RequestFunc {
3535 return func(ctx context.Context, r *stdhttp.Request) context.Context {
3636 token, ok := ctx.Value(JWTTokenContextKey).(string)
3737 if ok {
4141 }
4242 }
4343
44 // ToGRPCContext moves JWT token from grpc metadata to context. Particularly
44 // GRPCToContext moves JWT token from grpc metadata to context. Particularly
4545 // userful for servers.
46 func ToGRPCContext() grpc.ServerRequestFunc {
46 func GRPCToContext() grpc.ServerRequestFunc {
4747 return func(ctx context.Context, md metadata.MD) context.Context {
4848 // capital "Key" is illegal in HTTP/2.
4949 authHeader, ok := md["authorization"]
6060 }
6161 }
6262
63 // FromGRPCContext moves JWT token from context to grpc metadata. Particularly
63 // ContextToGRPC moves JWT token from context to grpc metadata. Particularly
6464 // useful for clients.
65 func FromGRPCContext() grpc.ClientRequestFunc {
65 func ContextToGRPC() grpc.ClientRequestFunc {
6666 return func(ctx context.Context, md *metadata.MD) context.Context {
6767 token, ok := ctx.Value(JWTTokenContextKey).(string)
6868 if ok {
88 "google.golang.org/grpc/metadata"
99 )
1010
11 func TestToHTTPContext(t *testing.T) {
12 reqFunc := ToHTTPContext()
11 func TestHTTPToContext(t *testing.T) {
12 reqFunc := HTTPToContext()
1313
1414 // When the header doesn't exist
1515 ctx := reqFunc(context.Background(), &http.Request{})
3737 }
3838 }
3939
40 func TestFromHTTPContext(t *testing.T) {
41 reqFunc := FromHTTPContext()
40 func TestContextToHTTP(t *testing.T) {
41 reqFunc := ContextToHTTP()
4242
4343 // No JWT Token is passed in the context
4444 ctx := context.Background()
6363 }
6464 }
6565
66 func TestToGRPCContext(t *testing.T) {
66 func TestGRPCToContext(t *testing.T) {
6767 md := metadata.MD{}
68 reqFunc := ToGRPCContext()
68 reqFunc := GRPCToContext()
6969
7070 // No Authorization header is passed
7171 ctx := reqFunc(context.Background(), md)
9595 }
9696 }
9797
98 func TestFromGRPCContext(t *testing.T) {
99 reqFunc := FromGRPCContext()
98 func TestContextToGRPC(t *testing.T) {
99 reqFunc := ContextToGRPC()
100100
101101 // No JWT Token is passed in the context
102102 ctx := context.Background()