github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/middleware/security/jwt/context.go (about)

     1  package jwt
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	jwt "github.com/dgrijalva/jwt-go"
     7  )
     8  
     9  type contextKey int
    10  
    11  const (
    12  	jwtKey contextKey = iota + 1
    13  )
    14  
    15  // WithJWT creates a child context containing the given JWT.
    16  func WithJWT(ctx context.Context, t *jwt.Token) context.Context {
    17  	return context.WithValue(ctx, jwtKey, t)
    18  }
    19  
    20  // ContextJWT retrieves the JWT token from a `context` that went through our security middleware.
    21  func ContextJWT(ctx context.Context) *jwt.Token {
    22  	token, ok := ctx.Value(jwtKey).(*jwt.Token)
    23  	if !ok {
    24  		return nil
    25  	}
    26  	return token
    27  }