This project contains PR #49 classes to provide a shim JWT support in Vert.x gRPC.
It is not recommended to use this project without understanding what classes will be patched.
The poc-grpc-vertx-server (shim branch) project contains a PoC which demonstrates how this shim can be used.
The #callHandler
method can be used to setup handlers for methods which require authentication or which are public and don't require authentication.
JWTAuthOptions jwtConfig = new JWTAuthOptions()
.setKeyStore(new KeyStoreOptions().setPath(keystorePath.toString())
.setPassword(keystorePassword)
.setType("jceks"));
JWTAuth jwtAuth = JWTAuth.create(vertx, jwtConfig);
JWTGrpcServer jwtServer = JWTGrpcServer.create(vertx, jwtAuth);
jwtServer.callHandler(GreeterGrpc.getSayHelloMethod(), true, request -> {
User user = request.user();
request.handler(hello -> {
log.info("Server got hello request with name {} from {}", hello.getName(), user.subject());
GrpcServerResponse<HelloRequest, HelloReply> response = request.response();
HelloReply reply = HelloReply.newBuilder()
.setMessage("Reply with " + hello.getName())
.build();
response.end(reply);
});
});
server = vertx.createHttpServer(new HttpServerOptions().setPort(0)
.setHost("localhost")).requestHandler(jwtServer);
The client will send credentials to the server if those have been provided by setting a JWT via #withCredentials
.
String token = …
JWTGrpcClient client = JWTGrpcClient.create(vertx).withCredentials(new TokenCredentials(token));
this.socket = SocketAddress.inetSocketAddress(port, host);
Future<HelloReply> reply = client.request(socket, GreeterGrpc.getSayHelloMethod()).compose(request -> {
request.end(HelloRequest
.newBuilder()
.setName(name)
.build());
return request.response()
.compose(GrpcClientResponse::last);
});