...

Source file src/github.com/go-kit/kit/auth/jwt/transport.go

Documentation: github.com/go-kit/kit/auth/jwt

     1  package jwt
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	stdhttp "net/http"
     7  	"strings"
     8  
     9  	"google.golang.org/grpc/metadata"
    10  
    11  	"github.com/go-kit/kit/transport/grpc"
    12  	"github.com/go-kit/kit/transport/http"
    13  )
    14  
    15  const (
    16  	bearer       string = "bearer"
    17  	bearerFormat string = "Bearer %s"
    18  )
    19  
    20  // HTTPToContext moves a JWT from request header to context. Particularly
    21  // useful for servers.
    22  func HTTPToContext() http.RequestFunc {
    23  	return func(ctx context.Context, r *stdhttp.Request) context.Context {
    24  		token, ok := extractTokenFromAuthHeader(r.Header.Get("Authorization"))
    25  		if !ok {
    26  			return ctx
    27  		}
    28  
    29  		return context.WithValue(ctx, JWTContextKey, token)
    30  	}
    31  }
    32  
    33  // ContextToHTTP moves a JWT from context to request header. Particularly
    34  // useful for clients.
    35  func ContextToHTTP() http.RequestFunc {
    36  	return func(ctx context.Context, r *stdhttp.Request) context.Context {
    37  		token, ok := ctx.Value(JWTContextKey).(string)
    38  		if ok {
    39  			r.Header.Add("Authorization", generateAuthHeaderFromToken(token))
    40  		}
    41  		return ctx
    42  	}
    43  }
    44  
    45  // GRPCToContext moves a JWT from grpc metadata to context. Particularly
    46  // userful for servers.
    47  func GRPCToContext() grpc.ServerRequestFunc {
    48  	return func(ctx context.Context, md metadata.MD) context.Context {
    49  		// capital "Key" is illegal in HTTP/2.
    50  		authHeader, ok := md["authorization"]
    51  		if !ok {
    52  			return ctx
    53  		}
    54  
    55  		token, ok := extractTokenFromAuthHeader(authHeader[0])
    56  		if ok {
    57  			ctx = context.WithValue(ctx, JWTContextKey, token)
    58  		}
    59  
    60  		return ctx
    61  	}
    62  }
    63  
    64  // ContextToGRPC moves a JWT from context to grpc metadata. Particularly
    65  // useful for clients.
    66  func ContextToGRPC() grpc.ClientRequestFunc {
    67  	return func(ctx context.Context, md *metadata.MD) context.Context {
    68  		token, ok := ctx.Value(JWTContextKey).(string)
    69  		if ok {
    70  			// capital "Key" is illegal in HTTP/2.
    71  			(*md)["authorization"] = []string{generateAuthHeaderFromToken(token)}
    72  		}
    73  
    74  		return ctx
    75  	}
    76  }
    77  
    78  func extractTokenFromAuthHeader(val string) (token string, ok bool) {
    79  	authHeaderParts := strings.Split(val, " ")
    80  	if len(authHeaderParts) != 2 || !strings.EqualFold(authHeaderParts[0], bearer) {
    81  		return "", false
    82  	}
    83  
    84  	return authHeaderParts[1], true
    85  }
    86  
    87  func generateAuthHeaderFromToken(token string) string {
    88  	return fmt.Sprintf(bearerFormat, token)
    89  }
    90  

View as plain text