• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.jwtauth;
18 
19 import io.grpc.CallCredentials;
20 import io.grpc.Grpc;
21 import io.grpc.InsecureChannelCredentials;
22 import io.grpc.ManagedChannel;
23 import io.grpc.examples.helloworld.GreeterGrpc;
24 import io.grpc.examples.helloworld.HelloReply;
25 import io.grpc.examples.helloworld.HelloRequest;
26 import java.util.concurrent.TimeUnit;
27 import java.util.logging.Logger;
28 
29 /**
30  * An authenticating client that requests a greeting from the {@link AuthServer}.
31  */
32 public class AuthClient {
33 
34   private static final Logger logger = Logger.getLogger(AuthClient.class.getName());
35 
36   private final ManagedChannel channel;
37   private final GreeterGrpc.GreeterBlockingStub blockingStub;
38   private final CallCredentials callCredentials;
39 
40   /**
41    * Construct client for accessing GreeterGrpc server.
42    */
AuthClient(CallCredentials callCredentials, String host, int port)43   AuthClient(CallCredentials callCredentials, String host, int port) {
44     this(
45         callCredentials,
46         // For this example we use plaintext to avoid needing certificates, but it is
47         // recommended to use TlsChannelCredentials.
48         Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create())
49             .build());
50   }
51 
52   /**
53    * Construct client for accessing GreeterGrpc server using the existing channel.
54    */
AuthClient(CallCredentials callCredentials, ManagedChannel channel)55   AuthClient(CallCredentials callCredentials, ManagedChannel channel) {
56     this.callCredentials = callCredentials;
57     this.channel = channel;
58     this.blockingStub = GreeterGrpc.newBlockingStub(channel);
59   }
60 
shutdown()61   public void shutdown() throws InterruptedException {
62     channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
63   }
64 
65   /**
66    * Say hello to server.
67    *
68    * @param name name to set in HelloRequest
69    * @return the message in the HelloReply from the server
70    */
greet(String name)71   public String greet(String name) {
72     logger.info("Will try to greet " + name + " ...");
73     HelloRequest request = HelloRequest.newBuilder().setName(name).build();
74 
75     // Use a stub with the given call credentials applied to invoke the RPC.
76     HelloReply response =
77         blockingStub
78             .withCallCredentials(callCredentials)
79             .sayHello(request);
80 
81     logger.info("Greeting: " + response.getMessage());
82     return response.getMessage();
83   }
84 
85   /**
86    * Greet server. If provided, the first element of {@code args} is the name to use in the greeting
87    * and the second is the client identifier to set in JWT
88    */
main(String[] args)89   public static void main(String[] args) throws Exception {
90 
91     String host = "localhost";
92     int port = 50051;
93     String user = "world";
94     String clientId = "default-client";
95 
96     if (args.length > 0) {
97       host = args[0]; // Use the arg as the server host if provided
98     }
99     if (args.length > 1) {
100       port = Integer.parseInt(args[1]); // Use the second argument as the server port if provided
101     }
102     if (args.length > 2) {
103       user = args[2]; // Use the the third argument as the name to greet if provided
104     }
105     if (args.length > 3) {
106       clientId = args[3]; // Use the fourth argument as the client identifier if provided
107     }
108 
109     CallCredentials credentials = new JwtCredential(clientId);
110     AuthClient client = new AuthClient(credentials, host, port);
111 
112     try {
113       client.greet(user);
114     } finally {
115       client.shutdown();
116     }
117   }
118 }
119