private AppsCodeUserDetails getUserDetails(String teamId, String username, String token) throws Exception { String email = emailCache.getIfPresent(new UserId(teamId, username)); if (email != null && !email.isEmpty()) { logger.info(String .format("Returning user details from cache. username: %s, email: %s", username, email)); return AppsCodeUserDetails.fromUsernameAndEmail(username, email); } ConduitGrpc.ConduitBlockingStub conduitBlockingStub = ConduitGrpc.newBlockingStub(channel). withCallCredentials( MoreCallCredentials.from(new TokenCredential(teamId, token))); ConduitWhoAmIResponse response = conduitBlockingStub.whoAmI(VoidRequest.newBuilder().build()); emailCache.put(new UserId(teamId, response.getUser().getUserName()), response.getUser().getPrimaryEmail()); return AppsCodeUserDetails.fromUsernameAndEmail(response.getUser().getUserName(), response.getUser().getPrimaryEmail()); }
private void fetchAccessToken() { ManagedChannel channel = ManagedChannelBuilder.forTarget(HOSTNAME).build(); try { mApi = EmbeddedAssistantGrpc.newStub(channel) .withCallCredentials(MoreCallCredentials.from( Credentials_.fromResource(getApplicationContext(), R.raw.credentials) )); } catch (IOException|JSONException e) { Log.e(TAG, "error creating assistant service:", e); } for (Listener listener : mListeners) { listener. onCredentioalSuccess(); } }
/** Test JWT-based auth. */ public void jwtTokenCreds(InputStream serviceAccountJson) throws Exception { final SimpleRequest request = SimpleRequest.newBuilder() .setResponseType(PayloadType.COMPRESSABLE) .setResponseSize(314159) .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[271828]))) .setFillUsername(true) .build(); ServiceAccountCredentials credentials = (ServiceAccountCredentials) GoogleCredentials.fromStream(serviceAccountJson); TestServiceGrpc.TestServiceBlockingStub stub = blockingStub .withCallCredentials(MoreCallCredentials.from(credentials)); SimpleResponse response = stub.unaryCall(request); assertEquals(credentials.getClientEmail(), response.getUsername()); assertEquals(314159, response.getPayload().getBody().size()); }
/** Sends a unary rpc with raw oauth2 access token credentials. */ public void oauth2AuthToken(String jsonKey, InputStream credentialsStream, String authScope) throws Exception { GoogleCredentials utilCredentials = GoogleCredentials.fromStream(credentialsStream); utilCredentials = utilCredentials.createScoped(Arrays.<String>asList(authScope)); AccessToken accessToken = utilCredentials.refreshAccessToken(); OAuth2Credentials credentials = OAuth2Credentials.create(accessToken); TestServiceGrpc.TestServiceBlockingStub stub = blockingStub .withCallCredentials(MoreCallCredentials.from(credentials)); final SimpleRequest request = SimpleRequest.newBuilder() .setFillUsername(true) .setFillOauthScope(true) .build(); final SimpleResponse response = stub.unaryCall(request); assertFalse(response.getUsername().isEmpty()); assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername())); assertFalse(response.getOauthScope().isEmpty()); assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.contains(response.getOauthScope())); }
/** * Initializes the Assistant. */ public void connect() { mAssistantThread = new HandlerThread("assistantThread"); mAssistantThread.start(); mAssistantHandler = new Handler(mAssistantThread.getLooper()); ManagedChannel channel = ManagedChannelBuilder.forTarget(ASSISTANT_API_ENDPOINT).build(); mAssistantService = EmbeddedAssistantGrpc.newStub(channel) .withCallCredentials(MoreCallCredentials.from(mUserCredentials)); }
/** * Get CallCredentials from OAuthCredentials * * @param oAuthCredentials the credentials from the AuthenticationHelper * @return the CallCredentials for the GRPC requests */ private CallCredentials getCallCredentials(OAuthCredentials oAuthCredentials) { AccessToken accessToken = new AccessToken( oAuthCredentials.getAccessToken(), new Date(oAuthCredentials.getExpirationTime()) ); OAuth2Credentials oAuth2Credentials = new OAuth2Credentials(accessToken); // Create an instance of {@link io.grpc.CallCredentials} return MoreCallCredentials.from(oAuth2Credentials); }
/** * Create a new {@link CallCredentials} object. * * @throws IOException in case the call credentials can't be constructed. */ public static CallCredentials newCallCredentials(AuthAndTLSOptions options) throws IOException { Credentials creds = newCredentials(options); if (creds != null) { return MoreCallCredentials.from(creds); } return null; }
@VisibleForTesting public static CallCredentials newCallCredentials( @Nullable InputStream credentialsFile, List<String> authScope) throws IOException { Credentials creds = newCredentials(credentialsFile, authScope); if (creds != null) { return MoreCallCredentials.from(creds); } return null; }
/** Sends a large unary rpc with service account credentials. */ public void serviceAccountCreds(String jsonKey, InputStream credentialsStream, String authScope) throws Exception { // cast to ServiceAccountCredentials to double-check the right type of object was created. GoogleCredentials credentials = ServiceAccountCredentials.class.cast(GoogleCredentials.fromStream(credentialsStream)); credentials = credentials.createScoped(Arrays.<String>asList(authScope)); TestServiceGrpc.TestServiceBlockingStub stub = blockingStub .withCallCredentials(MoreCallCredentials.from(credentials)); final SimpleRequest request = SimpleRequest.newBuilder() .setFillUsername(true) .setFillOauthScope(true) .setResponseSize(314159) .setResponseType(PayloadType.COMPRESSABLE) .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[271828]))) .build(); final SimpleResponse response = stub.unaryCall(request); assertFalse(response.getUsername().isEmpty()); assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername())); assertFalse(response.getOauthScope().isEmpty()); assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.contains(response.getOauthScope())); final SimpleResponse goldenResponse = SimpleResponse.newBuilder() .setOauthScope(response.getOauthScope()) .setUsername(response.getUsername()) .setPayload(Payload.newBuilder() .setType(PayloadType.COMPRESSABLE) .setBody(ByteString.copyFrom(new byte[314159]))) .build(); assertEquals(goldenResponse, response); }
/** Sends a large unary rpc with compute engine credentials. */ public void computeEngineCreds(String serviceAccount, String oauthScope) throws Exception { ComputeEngineCredentials credentials = ComputeEngineCredentials.create(); TestServiceGrpc.TestServiceBlockingStub stub = blockingStub .withCallCredentials(MoreCallCredentials.from(credentials)); final SimpleRequest request = SimpleRequest.newBuilder() .setFillUsername(true) .setFillOauthScope(true) .setResponseSize(314159) .setResponseType(PayloadType.COMPRESSABLE) .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[271828]))) .build(); final SimpleResponse response = stub.unaryCall(request); assertEquals(serviceAccount, response.getUsername()); assertFalse(response.getOauthScope().isEmpty()); assertTrue("Received oauth scope: " + response.getOauthScope(), oauthScope.contains(response.getOauthScope())); final SimpleResponse goldenResponse = SimpleResponse.newBuilder() .setOauthScope(response.getOauthScope()) .setUsername(response.getUsername()) .setPayload(Payload.newBuilder() .setType(PayloadType.COMPRESSABLE) .setBody(ByteString.copyFrom(new byte[314159]))) .build(); assertEquals(goldenResponse, response); }