|
46 | 46 | import com.google.api.client.util.GenericData; |
47 | 47 | import com.google.api.core.ObsoleteApi; |
48 | 48 | import com.google.auth.CredentialTypeForMetrics; |
| 49 | +import com.google.auth.Credentials; |
49 | 50 | import com.google.auth.ServiceAccountSigner; |
50 | 51 | import com.google.auth.http.HttpCredentialsAdapter; |
51 | 52 | import com.google.auth.http.HttpTransportFactory; |
@@ -531,7 +532,7 @@ public ImpersonatedCredentials createWithCustomCalendar(Calendar calendar) { |
531 | 532 | .build(); |
532 | 533 | } |
533 | 534 |
|
534 | | - private ImpersonatedCredentials(Builder builder) throws IOException { |
| 535 | + protected ImpersonatedCredentials(Builder builder) throws IOException { |
535 | 536 | super(builder); |
536 | 537 | this.sourceCredentials = builder.getSourceCredentials(); |
537 | 538 | this.targetPrincipal = builder.getTargetPrincipal(); |
@@ -580,31 +581,47 @@ public String getUniverseDomain() throws IOException { |
580 | 581 |
|
581 | 582 | @Override |
582 | 583 | public AccessToken refreshAccessToken() throws IOException { |
583 | | - if (this.sourceCredentials.getAccessToken() == null) { |
584 | | - // Apply the `CLOUD_PLATFORM_SCOPE` to access the iamcredentials endpoint |
585 | | - this.sourceCredentials = |
586 | | - this.sourceCredentials.createScoped( |
587 | | - Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE)); |
588 | | - } |
589 | | - |
590 | | - // skip for SA with SSJ flow because it uses self-signed JWT |
591 | | - // and will get refreshed at initialize request step |
592 | | - // run for other source credential types or SA with GDU assert flow |
593 | | - if (!(this.sourceCredentials instanceof ServiceAccountCredentials) |
594 | | - || (isDefaultUniverseDomain() |
595 | | - && ((ServiceAccountCredentials) this.sourceCredentials) |
596 | | - .shouldUseAssertionFlowForGdu())) { |
597 | | - try { |
598 | | - this.sourceCredentials.refreshIfExpired(); |
599 | | - } catch (IOException e) { |
600 | | - throw new IOException("Unable to refresh sourceCredentials", e); |
| 584 | + return refreshAccessToken(this.transportFactory); |
| 585 | + } |
| 586 | + |
| 587 | + AccessToken refreshAccessToken(HttpTransportFactory transportFactory) throws IOException { |
| 588 | + HttpCredentialsAdapter adapter; |
| 589 | + if (this.sourceCredentials instanceof ExternalAccountCredentials) { |
| 590 | + AccessToken intermediateAccessToken = |
| 591 | + ((ExternalAccountCredentials) this.sourceCredentials) |
| 592 | + .refreshAccessToken(transportFactory); |
| 593 | + Credentials authCredentials = |
| 594 | + intermediateAccessToken != null |
| 595 | + ? OAuth2Credentials.create(intermediateAccessToken) |
| 596 | + : this.sourceCredentials; |
| 597 | + adapter = new HttpCredentialsAdapter(authCredentials); |
| 598 | + } else { |
| 599 | + if (this.sourceCredentials.getAccessToken() == null) { |
| 600 | + // Apply the `CLOUD_PLATFORM_SCOPE` to access the iamcredentials endpoint |
| 601 | + this.sourceCredentials = |
| 602 | + this.sourceCredentials.createScoped( |
| 603 | + Collections.singletonList(OAuth2Utils.CLOUD_PLATFORM_SCOPE)); |
| 604 | + } |
| 605 | + |
| 606 | + // skip for SA with SSJ flow because it uses self-signed JWT |
| 607 | + // and will get refreshed at initialize request step |
| 608 | + // run for other source credential types or SA with GDU assert flow |
| 609 | + if (!(this.sourceCredentials instanceof ServiceAccountCredentials) |
| 610 | + || (isDefaultUniverseDomain() |
| 611 | + && ((ServiceAccountCredentials) this.sourceCredentials) |
| 612 | + .shouldUseAssertionFlowForGdu())) { |
| 613 | + try { |
| 614 | + this.sourceCredentials.refreshIfExpired(); |
| 615 | + } catch (IOException e) { |
| 616 | + throw new IOException("Unable to refresh sourceCredentials", e); |
| 617 | + } |
601 | 618 | } |
| 619 | + adapter = new HttpCredentialsAdapter(sourceCredentials); |
602 | 620 | } |
603 | 621 |
|
604 | | - HttpTransport httpTransport = this.transportFactory.create(); |
| 622 | + HttpTransport httpTransport = transportFactory.create(); |
605 | 623 | JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); |
606 | 624 |
|
607 | | - HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(sourceCredentials); |
608 | 625 | HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); |
609 | 626 |
|
610 | 627 | String endpointUrl = |
|
0 commit comments