@Test public void serviceAccountToJwt() throws Exception { KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); @SuppressWarnings("deprecation") ServiceAccountCredentials credentials = new ServiceAccountCredentials( null, "email@example.com", pair.getPrivate(), null, null) { @Override public AccessToken refreshAccessToken() { throw new AssertionError(); } }; 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 ")); assertTrue(authorization[0], authorization[0].length() > 300); }
@Override public void applyRequestMetadata( RequestInfo info, Executor appExecutor, final MetadataApplier applier) { SecurityLevel security = info.getSecurityLevel(); if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) { applier.fail(Status.UNAUTHENTICATED .withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security)); return; } String authority = checkNotNull(info.getAuthority(), "authority"); final URI uri; try { uri = serviceUri(authority, info.getMethodDescriptor()); } catch (StatusException e) { applier.fail(e.getStatus()); return; } creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() { @Override public void onSuccess(Map<String, List<String>> metadata) { Metadata headers; try { synchronized (GoogleAuthLibraryCallCredentials.this) { if (lastMetadata == null || lastMetadata != metadata) { lastHeaders = toHeaders(metadata); lastMetadata = metadata; } headers = lastHeaders; } } catch (Throwable t) { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed to convert credential metadata") .withCause(t)); return; } applier.apply(headers); } @Override public void onFailure(Throwable e) { if (e instanceof IOException) { applier.fail(Status.UNAVAILABLE .withDescription("Credentials failed to obtain metadata") .withCause(e)); } else { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed computing credential metadata") .withCause(e)); } } }); }
GoogleAuthLibraryCallCredentials extends CallCredentials2 { @Override public void applyRequestMetadata( RequestInfo info, Executor appExecutor, final MetadataApplier applier) { SecurityLevel security = info.getSecurityLevel(); if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) { applier.fail(Status.UNAUTHENTICATED .withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security)); return; } String authority = checkNotNull(info.getAuthority(), "authority"); final URI uri; try { uri = serviceUri(authority, info.getMethodDescriptor()); } catch (StatusException e) { applier.fail(e.getStatus()); return; } creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() { @Override public void onSuccess(Map<String, List<String>> metadata) { Metadata headers; try { synchronized (GoogleAuthLibraryCallCredentials.this) { if (lastMetadata == null || lastMetadata != metadata) { lastHeaders = toHeaders(metadata); lastMetadata = metadata; } headers = lastHeaders; } } catch (Throwable t) { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed to convert credential metadata") .withCause(t)); return; } applier.apply(headers); } @Override public void onFailure(Throwable e) { if (e instanceof IOException) { applier.fail(Status.UNAVAILABLE .withDescription("Credentials failed to obtain metadata") .withCause(e)); } else { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed computing credential metadata") .withCause(e)); } } }); } }
GoogleAuthLibraryCallCredentials extends CallCredentials2 { @Override public void applyRequestMetadata( RequestInfo info, Executor appExecutor, final MetadataApplier applier) { SecurityLevel security = info.getSecurityLevel(); if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) { applier.fail(Status.UNAUTHENTICATED .withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security)); return; } String authority = checkNotNull(info.getAuthority(), "authority"); final URI uri; try { uri = serviceUri(authority, info.getMethodDescriptor()); } catch (StatusException e) { applier.fail(e.getStatus()); return; } creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() { @Override public void onSuccess(Map<String, List<String>> metadata) { Metadata headers; try { synchronized (GoogleAuthLibraryCallCredentials.this) { if (lastMetadata == null || lastMetadata != metadata) { lastHeaders = toHeaders(metadata); lastMetadata = metadata; } headers = lastHeaders; } } catch (Throwable t) { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed to convert credential metadata") .withCause(t)); return; } applier.apply(headers); } @Override public void onFailure(Throwable e) { if (e instanceof IOException) { applier.fail(Status.UNAVAILABLE .withDescription("Credentials failed to obtain metadata") .withCause(e)); } else { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed computing credential metadata") .withCause(e)); } } }); } GoogleAuthLibraryCallCredentials(Credentials creds); @VisibleForTesting  GoogleAuthLibraryCallCredentials(Credentials creds, JwtHelper jwtHelper); }
GoogleAuthLibraryCallCredentials extends CallCredentials2 { @Override public void applyRequestMetadata( RequestInfo info, Executor appExecutor, final MetadataApplier applier) { SecurityLevel security = info.getSecurityLevel(); if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) { applier.fail(Status.UNAUTHENTICATED .withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security)); return; } String authority = checkNotNull(info.getAuthority(), "authority"); final URI uri; try { uri = serviceUri(authority, info.getMethodDescriptor()); } catch (StatusException e) { applier.fail(e.getStatus()); return; } creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() { @Override public void onSuccess(Map<String, List<String>> metadata) { Metadata headers; try { synchronized (GoogleAuthLibraryCallCredentials.this) { if (lastMetadata == null || lastMetadata != metadata) { lastHeaders = toHeaders(metadata); lastMetadata = metadata; } headers = lastHeaders; } } catch (Throwable t) { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed to convert credential metadata") .withCause(t)); return; } applier.apply(headers); } @Override public void onFailure(Throwable e) { if (e instanceof IOException) { applier.fail(Status.UNAVAILABLE .withDescription("Credentials failed to obtain metadata") .withCause(e)); } else { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed computing credential metadata") .withCause(e)); } } }); } GoogleAuthLibraryCallCredentials(Credentials creds); @VisibleForTesting  GoogleAuthLibraryCallCredentials(Credentials creds, JwtHelper jwtHelper); @Override void thisUsesUnstableApi(); @Override void applyRequestMetadata(
      RequestInfo info, Executor appExecutor, final MetadataApplier applier); }
GoogleAuthLibraryCallCredentials extends CallCredentials2 { @Override public void applyRequestMetadata( RequestInfo info, Executor appExecutor, final MetadataApplier applier) { SecurityLevel security = info.getSecurityLevel(); if (requirePrivacy && security != SecurityLevel.PRIVACY_AND_INTEGRITY) { applier.fail(Status.UNAUTHENTICATED .withDescription("Credentials require channel with PRIVACY_AND_INTEGRITY security level. " + "Observed security level: " + security)); return; } String authority = checkNotNull(info.getAuthority(), "authority"); final URI uri; try { uri = serviceUri(authority, info.getMethodDescriptor()); } catch (StatusException e) { applier.fail(e.getStatus()); return; } creds.getRequestMetadata(uri, appExecutor, new RequestMetadataCallback() { @Override public void onSuccess(Map<String, List<String>> metadata) { Metadata headers; try { synchronized (GoogleAuthLibraryCallCredentials.this) { if (lastMetadata == null || lastMetadata != metadata) { lastHeaders = toHeaders(metadata); lastMetadata = metadata; } headers = lastHeaders; } } catch (Throwable t) { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed to convert credential metadata") .withCause(t)); return; } applier.apply(headers); } @Override public void onFailure(Throwable e) { if (e instanceof IOException) { applier.fail(Status.UNAVAILABLE .withDescription("Credentials failed to obtain metadata") .withCause(e)); } else { applier.fail(Status.UNAUTHENTICATED .withDescription("Failed computing credential metadata") .withCause(e)); } } }); } GoogleAuthLibraryCallCredentials(Credentials creds); @VisibleForTesting  GoogleAuthLibraryCallCredentials(Credentials creds, JwtHelper jwtHelper); @Override void thisUsesUnstableApi(); @Override void applyRequestMetadata(
      RequestInfo info, Executor appExecutor, final MetadataApplier applier);  }