46 | 46 |
// Claims is a map of arbitrary claim data.
|
47 | 47 |
type Claims map[string]interface{}
|
48 | 48 |
|
49 | |
// NewSigner creates a new JWT token generating middleware, specifying key ID,
|
50 | |
// signing string, signing method and the claims you would like it to contain.
|
|
49 |
// NewSignerWithClaims creates a new JWT token generating middleware, specifying key ID,
|
|
50 |
// signing string, signing method and the jwt.Claims you would like it to contain.
|
51 | 51 |
// Tokens are signed with a Key ID header (kid) which is useful for determining
|
52 | 52 |
// the key to use for parsing. Particularly useful for clients.
|
53 | |
func NewSigner(kid string, key []byte, method jwt.SigningMethod, claims Claims) endpoint.Middleware {
|
|
53 |
func NewSignerWithClaims(kid string, key []byte, method jwt.SigningMethod, claims jwt.Claims) endpoint.Middleware {
|
54 | 54 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
55 | 55 |
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
|
56 | |
token := jwt.NewWithClaims(method, jwt.MapClaims(claims))
|
|
56 |
token := jwt.NewWithClaims(method, claims)
|
57 | 57 |
token.Header["kid"] = kid
|
58 | 58 |
|
59 | 59 |
// Sign and get the complete encoded token as a string using the secret
|
|
68 | 68 |
}
|
69 | 69 |
}
|
70 | 70 |
|
71 | |
// NewParser creates a new JWT token parsing middleware, specifying a
|
72 | |
// jwt.Keyfunc interface and the signing method. NewParser adds the resulting
|
73 | |
// claims to endpoint context or returns error on invalid token. Particularly
|
74 | |
// useful for servers.
|
75 | |
func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod) endpoint.Middleware {
|
|
71 |
// NewSigner creates a new JWT token generating middleware, specifying key ID,
|
|
72 |
// signing string, signing method and the claims you would like it to contain.
|
|
73 |
// It passes these values onto NewSignerWithClaims to handle the signing process.
|
|
74 |
func NewSigner(kid string, key []byte, method jwt.SigningMethod, claims Claims) endpoint.Middleware {
|
|
75 |
return NewSignerWithClaims(kid, key, method, jwt.MapClaims(claims))
|
|
76 |
}
|
|
77 |
|
|
78 |
// NewParserWithClaims creates a new JWT token parsing middleware, specifying a
|
|
79 |
// jwt.Keyfunc interface, the signing method as well as the claims to parse into.
|
|
80 |
// NewParserWithClaims adds the resulting claims to endpoint context or returns error on invalid token.
|
|
81 |
// Particularly useful for servers.
|
|
82 |
func NewParserWithClaims(keyFunc jwt.Keyfunc, method jwt.SigningMethod, claims jwt.Claims) endpoint.Middleware {
|
76 | 83 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
77 | 84 |
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
|
78 | 85 |
// tokenString is stored in the context from the transport handlers.
|
|
87 | 94 |
// of the token to identify which key to use, but the parsed token
|
88 | 95 |
// (head and claims) is provided to the callback, providing
|
89 | 96 |
// flexibility.
|
90 | |
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
97 |
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
91 | 98 |
// Don't forget to validate the alg is what you expect:
|
92 | 99 |
if token.Method != method {
|
93 | 100 |
return nil, ErrUnexpectedSigningMethod
|
|
118 | 125 |
return nil, ErrTokenInvalid
|
119 | 126 |
}
|
120 | 127 |
|
121 | |
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
122 | |
ctx = context.WithValue(ctx, JWTClaimsContextKey, Claims(claims))
|
|
128 |
if tokenClaims, ok := token.Claims.(jwt.MapClaims); ok {
|
|
129 |
ctx = context.WithValue(ctx, JWTClaimsContextKey, Claims(tokenClaims))
|
|
130 |
} else {
|
|
131 |
ctx = context.WithValue(ctx, JWTClaimsContextKey, token.Claims)
|
123 | 132 |
}
|
124 | 133 |
|
125 | 134 |
return next(ctx, request)
|
126 | 135 |
}
|
127 | 136 |
}
|
128 | 137 |
}
|
|
138 |
|
|
139 |
// NewParser creates a new JWT token parsing middleware, specifying a
|
|
140 |
// jwt.KeyFunc interface and the signing method. It will utilize NewParserWithClaims
|
|
141 |
// and fall back to implementing the jwt.MapClaims type.
|
|
142 |
func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod) endpoint.Middleware {
|
|
143 |
return NewParserWithClaims(keyFunc, method, jwt.MapClaims{})
|
|
144 |
}
|