1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | # package auth/jwt `package auth/jwt` provides a set of interfaces for service authorization through [JSON Web Tokens](https://jwt.io/). ## Usage NewParser takes a key function and an expected signing method and returns an `endpoint.Middleware`. The middleware will parse a token passed into the context via the `jwt.JWTTokenContextKey`. If the token is valid, any claims will be added to the context via the `jwt.JWTClaimsContextKey`. ```go import ( stdjwt "github.com/dgrijalva/jwt-go" "github.com/go-kit/kit/auth/jwt" "github.com/go-kit/kit/endpoint" ) func main() { var exampleEndpoint endpoint.Endpoint { kf := func(token *stdjwt.Token) (interface{}, error) { return []byte("SigningString"), nil } exampleEndpoint = MakeExampleEndpoint(service) exampleEndpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256)(exampleEndpoint) } } ``` NewSigner takes a JWT key ID header, the signing key, signing method, and a claims object. It returns an `endpoint.Middleware`. The middleware will build the token string and add it to the context via the `jwt.JWTTokenContextKey`. ```go import ( stdjwt "github.com/dgrijalva/jwt-go" "github.com/go-kit/kit/auth/jwt" "github.com/go-kit/kit/endpoint" ) func main() { var exampleEndpoint endpoint.Endpoint { exampleEndpoint = grpctransport.NewClient(...).Endpoint() exampleEndpoint = jwt.NewSigner( "kid-header", []byte("SigningString"), stdjwt.SigningMethodHS256, jwt.Claims{}, )(exampleEndpoint) } } ``` In order for the parser and the signer to work, the authorization headers need to be passed between the request and the context. `ToHTTPContext()`, `FromHTTPContext()`, `ToGRPCContext()`, and `FromGRPCContext()` are given as helpers to do this. These functions implement the correlating transport's RequestFunc interface and can be passed as ClientBefore or ServerBefore options. Example of use in a client: ```go import ( stdjwt "github.com/dgrijalva/jwt-go" grpctransport "github.com/go-kit/kit/transport/grpc" "github.com/go-kit/kit/auth/jwt" "github.com/go-kit/kit/endpoint" ) func main() { options := []httptransport.ClientOption{} var exampleEndpoint endpoint.Endpoint { exampleEndpoint = grpctransport.NewClient(..., grpctransport.ClientBefore(jwt.FromGRPCContext())).Endpoint() exampleEndpoint = jwt.NewSigner( "kid-header", []byte("SigningString"), stdjwt.SigningMethodHS256, jwt.Claims{}, )(exampleEndpoint) } } ``` Example of use in a server: ```go import ( "context" "github.com/go-kit/kit/auth/jwt" "github.com/go-kit/kit/log" grpctransport "github.com/go-kit/kit/transport/grpc" ) func MakeGRPCServer(ctx context.Context, endpoints Endpoints, logger log.Logger) pb.ExampleServer { options := []grpctransport.ServerOption{grpctransport.ServerErrorLogger(logger)} return &grpcServer{ createUser: grpctransport.NewServer( ctx, endpoints.CreateUserEndpoint, DecodeGRPCCreateUserRequest, EncodeGRPCCreateUserResponse, append(options, grpctransport.ServerBefore(jwt.ToGRPCContext()))..., ), getUser: grpctransport.NewServer( ctx, endpoints.GetUserEndpoint, DecodeGRPCGetUserRequest, EncodeGRPCGetUserResponse, options..., ), } } ``` |
History of auth / jwt @d2f2902b-79c4-43cd-8c09-341e33fc6017/v0.5.0
0
»»
- Improvements based off PR comments Brian Kassouf 7 years ago
- typo in README Brian Kassouf 7 years ago
- s/impliment/implement Brian Kassouf 7 years ago
- Update README.md Brian Kassouf (commit: GitHub) 7 years ago
- Update README.md Brian Kassouf (commit: GitHub) 7 years ago
- Add README.md Brian Kassouf 7 years ago
- Remove unused keyset struct Brian Kassouf 7 years ago
- go back to the keyfunc method of passing keys to support signers who don't control the KID header Brian Kassouf 7 years ago
- Don't export the type of the context keys to avoid collisions Brian Kassouf 7 years ago
- Refactor extractTokenFromAuthHeader function Brian Kassouf 7 years ago
- Added tests for the type of error returned Brian Kassouf 7 years ago
- Updated tests to use the new KeySet object Brian Kassouf 7 years ago
- Refactor away from passing a function to passing a struct with multiple options for signing keys Brian Kassouf 7 years ago
- let's use a shorter invalid key Brian Kassouf 7 years ago
- add more tests around token parsing Brian Kassouf 7 years ago
- add tests for http transports Brian Kassouf 7 years ago
- Simplify the passing of tokens by removing the metadata requirement Brian Kassouf 7 years ago
- added a FromHTTPContext function Brian Kassouf 7 years ago
- Update import paths for to go-kit in test files Brian Kassouf (commit: Brian Kassouf) 7 years ago
- Update import paths for to go-kit in test files Brian Kassouf (commit: Brian Kassouf) 7 years ago
- Fix typo in comments Brian Kassouf (commit: Brian Kassouf) 7 years ago
- added more test cases to the transport tests Brian Kassouf 7 years ago
- Move jwtToken strings into a const variable & rename metadata key to 'authorization' Brian Kassouf 7 years ago
- move jwt auth into it's own package and rename some functions Brian Kassouf 7 years ago
0
»»