/** Return channel with interceptor for returning credentials. */ private Channel newChannel() throws IOException { checkState(publisherChannel != null, "PubsubGrpcClient has been closed"); ClientAuthInterceptor interceptor = new ClientAuthInterceptor(credentials, Executors.newSingleThreadExecutor()); return ClientInterceptors.intercept(publisherChannel, interceptor); }
/** * Generate a JWT-specific service URI. The URI is simply an identifier with enough information * for a service to know that the JWT was intended for it. The URI will commonly be verified with * a simple string equality check. */ private URI serviceUri(Channel channel, MethodDescriptor<?, ?> method) throws StatusException { String authority = channel.authority(); if (authority == null) { throw Status.UNAUTHENTICATED.withDescription("Channel has no authority").asException(); } // Always use HTTPS, by definition. final String scheme = "https"; final int defaultPort = 443; String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); URI uri; try { uri = new URI(scheme, authority, path, null, null); } catch (URISyntaxException e) { throw Status.UNAUTHENTICATED.withDescription("Unable to construct service URI for auth") .withCause(e).asException(); } // The default port must not be present. Alternative ports should be present. if (uri.getPort() == defaultPort) { uri = removePort(uri); } return uri; }
/** * Create a new gRPC channel to the Google Genomics API, using the provided credentials for auth. * * @param creds The credential. * @param fields Which fields to return in the partial response, or null for none. * @return The ManagedChannel. * @throws SSLException */ public static ManagedChannel fromCreds(GoogleCredentials creds, String fields) throws SSLException { List<ClientInterceptor> interceptors = new ArrayList(); interceptors.add(new ClientAuthInterceptor(creds.createScoped(Arrays.asList(GENOMICS_SCOPE)), Executors.newSingleThreadExecutor())); if (!Strings.isNullOrEmpty(fields)) { Metadata headers = new Metadata(); Metadata.Key<String> partialResponseHeader = Metadata.Key.of(PARTIAL_RESPONSE_HEADER, Metadata.ASCII_STRING_MARSHALLER); headers.put(partialResponseHeader, fields); interceptors.add(MetadataUtils.newAttachHeadersInterceptor(headers)); } return getGenomicsManagedChannel(interceptors); }
new ClientAuthInterceptor(credentials, MoreExecutors.directExecutor());
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */ public static Channel getChannel(CredentialsProvider credentialsProvider) throws IOException { ManagedChannel channelImpl = NettyChannelBuilder.forAddress(ENDPOINT, 443) .negotiationType(NegotiationType.TLS) // Maximum Pub/Sub message size is 10MB. .maxInboundMessageSize(10 * 1024 * 1024) .build(); final ClientAuthInterceptor interceptor = new ClientAuthInterceptor( credentialsProvider.getCredentials(), Executors.newCachedThreadPool()); return ClientInterceptors.intercept(channelImpl, interceptor); }