@Test @SuppressWarnings("unchecked") public void credentialsReturnNullMetadata() throws Exception { ListMultimap<String, String> values = LinkedListMultimap.create(); values.put("Authorization", "token1"); when(credentials.getRequestMetadata(eq(expectedUri))) .thenReturn(null, Multimaps.<String, String>asMap(values), null); GoogleAuthLibraryCallCredentials callCredentials = new GoogleAuthLibraryCallCredentials(credentials); for (int i = 0; i < 3; i++) { callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier); } verify(credentials, times(3)).getRequestMetadata(eq(expectedUri)); verify(applier, times(3)).apply(headersCaptor.capture()); List<Metadata> headerList = headersCaptor.getAllValues(); assertEquals(3, headerList.size()); assertEquals(0, headerList.get(0).keys().size()); Iterable<String> authorization = headerList.get(1).getAll(AUTHORIZATION); assertArrayEquals(new String[]{"token1"}, Iterables.toArray(authorization, String.class)); assertEquals(0, headerList.get(2).keys().size()); }
@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);  }