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());
}
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);
}
}
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);
}
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);
}
Aggregations