Merge pull request #617 from bcho/fix-doc/auth-jwt
auth/jwt: minor doc update
Chris Hines authored 6 years ago
GitHub committed 6 years ago
12 | 12 |
```go
|
13 | 13 |
import (
|
14 | 14 |
stdjwt "github.com/dgrijalva/jwt-go"
|
15 | |
|
|
15 |
|
16 | 16 |
"github.com/go-kit/kit/auth/jwt"
|
17 | 17 |
"github.com/go-kit/kit/endpoint"
|
18 | 18 |
)
|
|
22 | 22 |
{
|
23 | 23 |
kf := func(token *stdjwt.Token) (interface{}, error) { return []byte("SigningString"), nil }
|
24 | 24 |
exampleEndpoint = MakeExampleEndpoint(service)
|
25 | |
exampleEndpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256)(exampleEndpoint)
|
|
25 |
exampleEndpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256, jwt.StandardClaimsFactory)(exampleEndpoint)
|
26 | 26 |
}
|
27 | 27 |
}
|
28 | 28 |
```
|
|
34 | 34 |
```go
|
35 | 35 |
import (
|
36 | 36 |
stdjwt "github.com/dgrijalva/jwt-go"
|
37 | |
|
|
37 |
|
38 | 38 |
"github.com/go-kit/kit/auth/jwt"
|
39 | 39 |
"github.com/go-kit/kit/endpoint"
|
40 | 40 |
)
|
|
44 | 44 |
{
|
45 | 45 |
exampleEndpoint = grpctransport.NewClient(...).Endpoint()
|
46 | 46 |
exampleEndpoint = jwt.NewSigner(
|
47 | |
"kid-header",
|
48 | |
[]byte("SigningString"),
|
49 | |
stdjwt.SigningMethodHS256,
|
|
47 |
"kid-header",
|
|
48 |
[]byte("SigningString"),
|
|
49 |
stdjwt.SigningMethodHS256,
|
50 | 50 |
jwt.Claims{},
|
51 | 51 |
)(exampleEndpoint)
|
52 | 52 |
}
|
|
66 | 66 |
import (
|
67 | 67 |
stdjwt "github.com/dgrijalva/jwt-go"
|
68 | 68 |
|
69 | |
grpctransport "github.com/go-kit/kit/transport/grpc"
|
|
69 |
grpctransport "github.com/go-kit/kit/transport/grpc"
|
70 | 70 |
"github.com/go-kit/kit/auth/jwt"
|
71 | 71 |
"github.com/go-kit/kit/endpoint"
|
72 | 72 |
)
|
69 | 69 |
// Useful in NewParser middleware.
|
70 | 70 |
type ClaimsFactory func() jwt.Claims
|
71 | 71 |
|
72 | |
// MapClaimsFactory is a ClaimsFactory that returns
|
|
72 |
// MapClaimsFactory is a ClaimsFactory that returns
|
73 | 73 |
// an empty jwt.MapClaims.
|
74 | 74 |
func MapClaimsFactory() jwt.Claims {
|
75 | |
return jwt.MapClaims{}
|
|
75 |
return jwt.MapClaims{}
|
76 | 76 |
}
|
77 | 77 |
|
78 | |
// StandardClaimsFactory is a ClaimsFactory that returns
|
|
78 |
// StandardClaimsFactory is a ClaimsFactory that returns
|
79 | 79 |
// an empty jwt.StandardClaims.
|
80 | 80 |
func StandardClaimsFactory() jwt.Claims {
|
81 | |
return &jwt.StandardClaims{}
|
|
81 |
return &jwt.StandardClaims{}
|
82 | 82 |
}
|
83 | 83 |
|
84 | 84 |
// NewParser creates a new JWT token parsing middleware, specifying a
|
85 | 85 |
// jwt.Keyfunc interface, the signing method and the claims type to be used. NewParser
|
86 | |
// adds the resulting claims to endpoint context or returns error on invalid token.
|
|
86 |
// adds the resulting claims to endpoint context or returns error on invalid token.
|
87 | 87 |
// Particularly useful for servers.
|
88 | 88 |
func NewParser(keyFunc jwt.Keyfunc, method jwt.SigningMethod, newClaims ClaimsFactory) endpoint.Middleware {
|
89 | 89 |
return func(next endpoint.Endpoint) endpoint.Endpoint {
|