|
| 1 | +/* |
| 2 | + * Copyright 2024, Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Google LLC nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +package com.google.auth.credentialaccessboundary; |
| 33 | + |
| 34 | +import static com.google.auth.oauth2.OAuth2Credentials.getFromServiceLoader; |
| 35 | +import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT; |
| 36 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | + |
| 38 | +import com.google.auth.Credentials; |
| 39 | +import com.google.auth.http.HttpTransportFactory; |
| 40 | +import com.google.auth.oauth2.AccessToken; |
| 41 | +import com.google.auth.oauth2.CredentialAccessBoundary; |
| 42 | +import com.google.auth.oauth2.GoogleCredentials; |
| 43 | +import com.google.auth.oauth2.OAuth2Utils; |
| 44 | +import com.google.auth.oauth2.StsRequestHandler; |
| 45 | +import com.google.auth.oauth2.StsTokenExchangeRequest; |
| 46 | +import com.google.auth.oauth2.StsTokenExchangeResponse; |
| 47 | +import com.google.common.base.Strings; |
| 48 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 49 | +import java.io.IOException; |
| 50 | + |
| 51 | +public final class ClientSideCredentialAccessBoundaryFactory { |
| 52 | + private final GoogleCredentials sourceCredential; |
| 53 | + private final transient HttpTransportFactory transportFactory; |
| 54 | + private final String tokenExchangeEndpoint; |
| 55 | + private String accessBoundarySessionKey; |
| 56 | + private AccessToken intermediateAccessToken; |
| 57 | + |
| 58 | + private ClientSideCredentialAccessBoundaryFactory(Builder builder) { |
| 59 | + this.transportFactory = builder.transportFactory; |
| 60 | + this.sourceCredential = builder.sourceCredential; |
| 61 | + this.tokenExchangeEndpoint = builder.tokenExchangeEndpoint; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Refreshes the source credential and exchanges it for an intermediary access token using the STS |
| 66 | + * endpoint. |
| 67 | + * |
| 68 | + * <p>If the source credential is expired, it will be refreshed. A token exchange request is then |
| 69 | + * made to the STS endpoint. The resulting intermediary access token and access boundary session |
| 70 | + * key are stored. The intermediary access token's expiration time is determined as follows: |
| 71 | + * |
| 72 | + * <ol> |
| 73 | + * <li>If the STS response includes `expires_in`, that value is used. |
| 74 | + * <li>Otherwise, if the source credential has an expiration time, that value is used. |
| 75 | + * <li>Otherwise, the intermediary token will have no expiration time. |
| 76 | + * </ol> |
| 77 | + * |
| 78 | + * @throws IOException If an error occurs during credential refresh or token exchange. |
| 79 | + */ |
| 80 | + private void refreshCredentials() throws IOException { |
| 81 | + try { |
| 82 | + this.sourceCredential.refreshIfExpired(); |
| 83 | + } catch (IOException e) { |
| 84 | + throw new IOException("Unable to refresh the provided source credential.", e); |
| 85 | + } |
| 86 | + |
| 87 | + AccessToken sourceAccessToken = sourceCredential.getAccessToken(); |
| 88 | + if (sourceAccessToken == null || Strings.isNullOrEmpty(sourceAccessToken.getTokenValue())) { |
| 89 | + throw new IllegalStateException("The source credential does not have an access token."); |
| 90 | + } |
| 91 | + |
| 92 | + StsTokenExchangeRequest request = |
| 93 | + StsTokenExchangeRequest.newBuilder( |
| 94 | + sourceAccessToken.getTokenValue(), OAuth2Utils.TOKEN_TYPE_ACCESS_TOKEN) |
| 95 | + .setRequestTokenType(OAuth2Utils.TOKEN_TYPE_ACCESS_BOUNDARY_INTERMEDIARY_TOKEN) |
| 96 | + .build(); |
| 97 | + |
| 98 | + StsRequestHandler handler = |
| 99 | + StsRequestHandler.newBuilder( |
| 100 | + tokenExchangeEndpoint, request, transportFactory.create().createRequestFactory()) |
| 101 | + .build(); |
| 102 | + |
| 103 | + StsTokenExchangeResponse response = handler.exchangeToken(); |
| 104 | + this.accessBoundarySessionKey = response.getAccessBoundarySessionKey(); |
| 105 | + this.intermediateAccessToken = response.getAccessToken(); |
| 106 | + |
| 107 | + // The STS endpoint will only return the expiration time for the intermediary token |
| 108 | + // if the original access token represents a service account. |
| 109 | + // The intermediary token's expiration time will always match the source credential expiration. |
| 110 | + // When no expires_in is returned, we can copy the source credential's expiration time. |
| 111 | + if (response.getAccessToken().getExpirationTime() == null) { |
| 112 | + if (sourceAccessToken.getExpirationTime() != null) { |
| 113 | + this.intermediateAccessToken = |
| 114 | + new AccessToken( |
| 115 | + response.getAccessToken().getTokenValue(), sourceAccessToken.getExpirationTime()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void refreshCredentialsIfRequired() { |
| 121 | + // TODO(negarb): Implement refreshCredentialsIfRequired |
| 122 | + throw new UnsupportedOperationException("refreshCredentialsIfRequired is not yet implemented."); |
| 123 | + } |
| 124 | + |
| 125 | + public AccessToken generateToken(CredentialAccessBoundary accessBoundary) { |
| 126 | + // TODO(negarb/jiahuah): Implement generateToken |
| 127 | + throw new UnsupportedOperationException("generateToken is not yet implemented."); |
| 128 | + } |
| 129 | + |
| 130 | + public static Builder newBuilder() { |
| 131 | + return new Builder(); |
| 132 | + } |
| 133 | + |
| 134 | + public static class Builder { |
| 135 | + private GoogleCredentials sourceCredential; |
| 136 | + private HttpTransportFactory transportFactory; |
| 137 | + private String universeDomain; |
| 138 | + private String tokenExchangeEndpoint; |
| 139 | + |
| 140 | + private Builder() {} |
| 141 | + |
| 142 | + /** |
| 143 | + * Sets the required source credential. |
| 144 | + * |
| 145 | + * @param sourceCredential the {@code GoogleCredentials} to set |
| 146 | + * @return this {@code Builder} object |
| 147 | + */ |
| 148 | + @CanIgnoreReturnValue |
| 149 | + public Builder setSourceCredential(GoogleCredentials sourceCredential) { |
| 150 | + this.sourceCredential = sourceCredential; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Sets the HTTP transport factory. |
| 156 | + * |
| 157 | + * @param transportFactory the {@code HttpTransportFactory} to set |
| 158 | + * @return this {@code Builder} object |
| 159 | + */ |
| 160 | + @CanIgnoreReturnValue |
| 161 | + public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) { |
| 162 | + this.transportFactory = transportFactory; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Sets the optional universe domain. |
| 168 | + * |
| 169 | + * @param universeDomain the universe domain to set |
| 170 | + * @return this {@code Builder} object |
| 171 | + */ |
| 172 | + @CanIgnoreReturnValue |
| 173 | + public Builder setUniverseDomain(String universeDomain) { |
| 174 | + this.universeDomain = universeDomain; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + public ClientSideCredentialAccessBoundaryFactory build() { |
| 179 | + checkNotNull(sourceCredential, "Source credential must not be null."); |
| 180 | + |
| 181 | + // Use the default HTTP transport factory if none was provided. |
| 182 | + if (transportFactory == null) { |
| 183 | + this.transportFactory = |
| 184 | + getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY); |
| 185 | + } |
| 186 | + |
| 187 | + // Default to GDU when not supplied. |
| 188 | + if (Strings.isNullOrEmpty(universeDomain)) { |
| 189 | + this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE; |
| 190 | + } |
| 191 | + |
| 192 | + // Ensure source credential's universe domain matches. |
| 193 | + try { |
| 194 | + if (!universeDomain.equals(sourceCredential.getUniverseDomain())) { |
| 195 | + throw new IllegalArgumentException( |
| 196 | + "The client side access boundary credential's universe domain must be the same as the source " |
| 197 | + + "credential."); |
| 198 | + } |
| 199 | + } catch (IOException e) { |
| 200 | + // Throwing an IOException would be a breaking change, so wrap it here. |
| 201 | + throw new IllegalStateException( |
| 202 | + "Error occurred when attempting to retrieve source credential universe domain.", e); |
| 203 | + } |
| 204 | + |
| 205 | + this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain); |
| 206 | + return new ClientSideCredentialAccessBoundaryFactory(this); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments