|
| 1 | +package com.google.auth.credentialaccessboundary; |
| 2 | + |
| 3 | +import static com.google.auth.oauth2.OAuth2Credentials.getFromServiceLoader; |
| 4 | +import static com.google.common.base.MoreObjects.firstNonNull; |
| 5 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 6 | + |
| 7 | +import com.google.auth.Credentials; |
| 8 | +import com.google.auth.http.HttpTransportFactory; |
| 9 | +import com.google.auth.oauth2.AccessToken; |
| 10 | +import com.google.auth.oauth2.GoogleCredentials; |
| 11 | +import com.google.auth.oauth2.OAuth2Utils; |
| 12 | +import com.google.auth.oauth2.StsRequestHandler; |
| 13 | +import com.google.auth.oauth2.StsTokenExchangeRequest; |
| 14 | +import com.google.auth.oauth2.StsTokenExchangeResponse; |
| 15 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +public final class ClientSideCredentialAccessBoundaryFactory { |
| 19 | + private final GoogleCredentials sourceCredential; |
| 20 | + private final transient HttpTransportFactory transportFactory; |
| 21 | + private final String tokenExchangeEndpoint; |
| 22 | + private String acceessBoundarySessionKey; |
| 23 | + private AccessToken intermediaryAccessToken; |
| 24 | + |
| 25 | + private ClientSideCredentialAccessBoundaryFactory(Builder builder) { |
| 26 | + this.transportFactory = |
| 27 | + firstNonNull( |
| 28 | + builder.transportFactory, |
| 29 | + getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY)); |
| 30 | + this.sourceCredential = checkNotNull(builder.sourceCredential); |
| 31 | + |
| 32 | + // Default to GDU when not supplied. |
| 33 | + String universeDomain; |
| 34 | + if (builder.universeDomain == null || builder.universeDomain.trim().isEmpty()) { |
| 35 | + universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE; |
| 36 | + } else { |
| 37 | + universeDomain = builder.universeDomain; |
| 38 | + } |
| 39 | + |
| 40 | + // Ensure source credential's universe domain matches. |
| 41 | + try { |
| 42 | + if (!universeDomain.equals(sourceCredential.getUniverseDomain())) { |
| 43 | + throw new IllegalArgumentException( |
| 44 | + "The client side access boundary credential's universe domain must be the same as the source " |
| 45 | + + "credential."); |
| 46 | + } |
| 47 | + } catch (IOException e) { |
| 48 | + // Throwing an IOException would be a breaking change, so wrap it here. |
| 49 | + throw new IllegalStateException( |
| 50 | + "Error occurred when attempting to retrieve source credential universe domain.", e); |
| 51 | + } |
| 52 | + String TOKEN_EXCHANGE_URL_FORMAT = "https://sts.{universe_domain}/v1/token"; |
| 53 | + this.tokenExchangeEndpoint = |
| 54 | + TOKEN_EXCHANGE_URL_FORMAT.replace("{universe_domain}", universeDomain); |
| 55 | + } |
| 56 | + |
| 57 | + public void fetchCredentials() throws IOException { |
| 58 | + try { |
| 59 | + this.sourceCredential.refreshIfExpired(); |
| 60 | + } catch (IOException e) { |
| 61 | + throw new IOException("Unable to refresh the provided source credential.", e); |
| 62 | + } |
| 63 | + |
| 64 | + AccessToken sourceAccessToken = sourceCredential.getAccessToken(); |
| 65 | + if (sourceAccessToken == null || sourceAccessToken.getTokenValue() == null) { |
| 66 | + throw new IOException("The source credential does not have an access token."); |
| 67 | + } |
| 68 | + |
| 69 | + StsTokenExchangeRequest request = |
| 70 | + StsTokenExchangeRequest.newBuilder( |
| 71 | + sourceAccessToken.getTokenValue(), OAuth2Utils.TOKEN_TYPE_ACCESS_TOKEN) |
| 72 | + .setRequestTokenType(OAuth2Utils.TOKEN_TYPE_ACCESS_BOUNDARY_INTERMEDIARY_TOKEN) |
| 73 | + .build(); |
| 74 | + |
| 75 | + StsRequestHandler handler = |
| 76 | + StsRequestHandler.newBuilder( |
| 77 | + tokenExchangeEndpoint, request, transportFactory.create().createRequestFactory()) |
| 78 | + .build(); |
| 79 | + |
| 80 | + StsTokenExchangeResponse response = handler.exchangeToken(); |
| 81 | + this.acceessBoundarySessionKey = response.getAccessBoundarySessionKey(); |
| 82 | + this.intermediaryAccessToken = response.getAccessToken(); |
| 83 | + |
| 84 | + // The STS endpoint will only return the expiration time for the intermediary token |
| 85 | + // if the original access token represents a service account. |
| 86 | + // The intermediary token's expiration time will always match the source credential expiration. |
| 87 | + // When no expires_in is returned, we can copy the source credential's expiration time. |
| 88 | + if (response.getAccessToken().getExpirationTime() == null) { |
| 89 | + if (sourceAccessToken.getExpirationTime() != null) { |
| 90 | + this.intermediaryAccessToken = |
| 91 | + new AccessToken( |
| 92 | + response.getAccessToken().getTokenValue(), sourceAccessToken.getExpirationTime()); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static Builder newBuilder() { |
| 98 | + return new Builder(); |
| 99 | + } |
| 100 | + |
| 101 | + public static class Builder { |
| 102 | + private GoogleCredentials sourceCredential; |
| 103 | + private HttpTransportFactory transportFactory; |
| 104 | + private String universeDomain; |
| 105 | + |
| 106 | + private Builder() {} |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the required source credential used to acquire the intermediary credential. |
| 110 | + * |
| 111 | + * @param sourceCredential the {@code GoogleCredentials} to set |
| 112 | + * @return this {@code Builder} object |
| 113 | + */ |
| 114 | + public Builder setSourceCredential(GoogleCredentials sourceCredential) { |
| 115 | + this.sourceCredential = sourceCredential; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the HTTP transport factory. |
| 121 | + * |
| 122 | + * @param transportFactory the {@code HttpTransportFactory} to set |
| 123 | + * @return this {@code Builder} object |
| 124 | + */ |
| 125 | + @CanIgnoreReturnValue |
| 126 | + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { |
| 127 | + this.transportFactory = transportFactory; |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Sets the optional universe domain. |
| 133 | + * |
| 134 | + * @param universeDomain the universe domain to set |
| 135 | + * @return this {@code Builder} object |
| 136 | + */ |
| 137 | + @CanIgnoreReturnValue |
| 138 | + public Builder setUniverseDomain(String universeDomain) { |
| 139 | + this.universeDomain = universeDomain; |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public ClientSideCredentialAccessBoundaryFactory build() { |
| 144 | + return new ClientSideCredentialAccessBoundaryFactory(this); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments