Search in sources :

Example 21 with ServiceAccountCredentials

use of com.google.auth.oauth2.ServiceAccountCredentials in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method toString_containsFields.

@Test
public void toString_containsFields() throws IOException {
    final URI tokenServer = URI.create("https://foo.com/bar");
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, tokenServer, SERVICE_ACCOUNT_USER);
    String expectedToString = String.format("ServiceAccountCredentials{clientId=%s, clientEmail=%s, privateKeyId=%s, " + "transportFactoryClassName=%s, tokenServerUri=%s, scopes=%s, serviceAccountUser=%s}", SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_ID, MockTokenServerTransportFactory.class.getName(), tokenServer, SCOPES, SERVICE_ACCOUNT_USER);
    assertEquals(expectedToString, credentials.toString());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Example 22 with ServiceAccountCredentials

use of com.google.auth.oauth2.ServiceAccountCredentials in project zhcet-web by zhcet-amu.

the class FirebaseService method initializeFirebase.

private void initializeFirebase() throws IOException {
    try {
        String messagingScope = "https://www.googleapis.com/auth/firebase.messaging";
        googleCredential = GoogleCredential.fromStream(firebaseLocator.getServiceAccountStream()).createScoped(Collections.singletonList(messagingScope));
        GoogleCredentials googleCredentials = GoogleCredentials.fromStream(firebaseLocator.getServiceAccountStream());
        projectId = ((ServiceAccountCredentials) googleCredentials).getProjectId();
        if (projectId == null)
            throw new RuntimeException("Project ID must not be null");
        FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(googleCredentials).setDatabaseUrl(getDatabaseUrl()).setStorageBucket(getStorageBucket()).build();
        FirebaseApp.initializeApp(options);
        log.info(ConsoleHelper.green("Firebase Initialized"));
    } catch (IllegalStateException ise) {
        log.info(ConsoleHelper.yellow("Firebase already initialized"));
    } catch (IllegalArgumentException e) {
        uninitialized = true;
        log.error("Firebase couldn't be initialized", e);
    }
}
Also used : GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) FirebaseOptions(com.google.firebase.FirebaseOptions)

Example 23 with ServiceAccountCredentials

use of com.google.auth.oauth2.ServiceAccountCredentials in project grpc-java by grpc.

the class GoogleAuthLibraryCallCredentialsTest method serviceAccountToJwt.

@Test
public void serviceAccountToJwt() throws Exception {
    KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    HttpTransportFactory factory = Mockito.mock(HttpTransportFactory.class);
    Mockito.when(factory.create()).thenThrow(new AssertionError());
    ServiceAccountCredentials credentials = ServiceAccountCredentials.newBuilder().setClientEmail("test-email@example.com").setPrivateKey(pair.getPrivate()).setPrivateKeyId("test-private-key-id").setHttpTransportFactory(factory).build();
    GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials);
    callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);
    assertEquals(0, runPendingRunnables());
    verify(applier).apply(headersCaptor.capture());
    Metadata headers = headersCaptor.getValue();
    String[] authorization = Iterables.toArray(headers.getAll(AUTHORIZATION), String.class);
    assertEquals(1, authorization.length);
    assertTrue(authorization[0], authorization[0].startsWith("Bearer "));
    // JWT is reasonably long. Normal tokens aren't.
    assertTrue(authorization[0], authorization[0].length() > 300);
}
Also used : KeyPair(java.security.KeyPair) Metadata(io.grpc.Metadata) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) HttpTransportFactory(com.google.auth.http.HttpTransportFactory) Test(org.junit.Test)

Example 24 with ServiceAccountCredentials

use of com.google.auth.oauth2.ServiceAccountCredentials in project grpc-java by grpc.

the class AbstractInteropTest method serviceAccountCreds.

/**
 * 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.asList(authScope));
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).setResponseSize(314159).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().setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertResponse(goldenResponse, response);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Aggregations

Test (org.junit.Test)18 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)12 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)10 URL (java.net.URL)5 Signature (java.security.Signature)5 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 Metadata (io.grpc.Metadata)3 KeyPair (java.security.KeyPair)3 GenericJson (com.google.api.client.json.GenericJson)2 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)2 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 URI (java.net.URI)2 HttpHeaders (com.google.api.client.http.HttpHeaders)1 FixedClock (com.google.api.client.testing.http.FixedClock)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 HttpTransportFactory (com.google.auth.http.HttpTransportFactory)1