Skip to content

Commit 6b04cc1

Browse files
committed
Move ClientSideCredentialAccessBoundaryFactory to its own module and some minor refactoring.
1 parent d9574da commit 6b04cc1

File tree

6 files changed

+97
-36
lines changed

6 files changed

+97
-36
lines changed

cab-token-generator/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.google.auth</groupId>
8+
<artifactId>google-auth-library-parent</artifactId>
9+
<version>1.29.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>cab-token-generator</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>22</maven.compiler.source>
16+
<maven.compiler.target>22</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.google.auth</groupId>
22+
<artifactId>google-auth-library-oauth2-http</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
</project>

oauth2_http/java/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactory.java renamed to cab-token-generator/src/main/java/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactory.java

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1+
/*
2+
* Copyright 2021 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+
132
package com.google.auth.credentialaccessboundary;
233

334
import static com.google.auth.oauth2.OAuth2Credentials.getFromServiceLoader;
4-
import static com.google.common.base.MoreObjects.firstNonNull;
35+
import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT;
536
import static com.google.common.base.Preconditions.checkNotNull;
637

738
import com.google.auth.Credentials;
@@ -12,6 +43,7 @@
1243
import com.google.auth.oauth2.StsRequestHandler;
1344
import com.google.auth.oauth2.StsTokenExchangeRequest;
1445
import com.google.auth.oauth2.StsTokenExchangeResponse;
46+
import com.google.common.base.Strings;
1547
import com.google.errorprone.annotations.CanIgnoreReturnValue;
1648
import java.io.IOException;
1749

@@ -23,46 +55,20 @@ public final class ClientSideCredentialAccessBoundaryFactory {
2355
private AccessToken intermediaryAccessToken;
2456

2557
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);
58+
this.transportFactory = builder.transportFactory;
59+
this.sourceCredential = builder.sourceCredential;
60+
this.tokenExchangeEndpoint = builder.tokenExchangeEndpoint;
5561
}
5662

57-
public void fetchCredentials() throws IOException {
63+
private void fetchCredentials() throws IOException {
5864
try {
5965
this.sourceCredential.refreshIfExpired();
6066
} catch (IOException e) {
6167
throw new IOException("Unable to refresh the provided source credential.", e);
6268
}
6369

6470
AccessToken sourceAccessToken = sourceCredential.getAccessToken();
65-
if (sourceAccessToken == null || sourceAccessToken.getTokenValue() == null) {
71+
if (sourceAccessToken == null || Strings.isNullOrEmpty(sourceAccessToken.getTokenValue())) {
6672
throw new IOException("The source credential does not have an access token.");
6773
}
6874

@@ -102,6 +108,7 @@ public static class Builder {
102108
private GoogleCredentials sourceCredential;
103109
private HttpTransportFactory transportFactory;
104110
private String universeDomain;
111+
private String tokenExchangeEndpoint;
105112

106113
private Builder() {}
107114

@@ -141,6 +148,33 @@ public Builder setUniverseDomain(String universeDomain) {
141148
}
142149

143150
public ClientSideCredentialAccessBoundaryFactory build() {
151+
checkNotNull(sourceCredential, "Source credential must not be null.");
152+
153+
// Use the default HTTP transport factory if none was provided.
154+
if (transportFactory == null) {
155+
this.transportFactory =
156+
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY);
157+
}
158+
159+
// Default to GDU when not supplied.
160+
if (Strings.isNullOrEmpty(universeDomain)) {
161+
this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE;
162+
}
163+
164+
// Ensure source credential's universe domain matches.
165+
try {
166+
if (!universeDomain.equals(sourceCredential.getUniverseDomain())) {
167+
throw new IllegalArgumentException(
168+
"The client side access boundary credential's universe domain must be the same as the source "
169+
+ "credential.");
170+
}
171+
} catch (IOException e) {
172+
// Throwing an IOException would be a breaking change, so wrap it here.
173+
throw new IllegalStateException(
174+
"Error occurred when attempting to retrieve source credential universe domain.", e);
175+
}
176+
177+
this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain);
144178
return new ClientSideCredentialAccessBoundaryFactory(this);
145179
}
146180
}

oauth2_http/java/com/google/auth/oauth2/DownscopedCredentials.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package com.google.auth.oauth2;
3333

34+
import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT;
3435
import static com.google.common.base.MoreObjects.firstNonNull;
3536
import static com.google.common.base.Preconditions.checkNotNull;
3637

@@ -88,7 +89,6 @@
8889
*/
8990
public final class DownscopedCredentials extends OAuth2Credentials {
9091

91-
private final String TOKEN_EXCHANGE_URL_FORMAT = "https://sts.{universe_domain}/v1/token";
9292
private final GoogleCredentials sourceCredential;
9393
private final CredentialAccessBoundary credentialAccessBoundary;
9494
private final String universeDomain;
@@ -125,8 +125,7 @@ private DownscopedCredentials(Builder builder) {
125125
throw new IllegalStateException(
126126
"Error occurred when attempting to retrieve source credential universe domain.", e);
127127
}
128-
this.tokenExchangeEndpoint =
129-
TOKEN_EXCHANGE_URL_FORMAT.replace("{universe_domain}", universeDomain);
128+
this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain);
130129
}
131130

132131
@Override

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class OAuth2Utils {
8989
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s:generateAccessToken";
9090
static final String SIGN_BLOB_ENDPOINT_FORMAT =
9191
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s:signBlob";
92+
public static final String TOKEN_EXCHANGE_URL_FORMAT = "https://sts.%s/v1/token";
9293

9394
static final URI TOKEN_SERVER_URI = URI.create("https://oauth2.googleapis.com/token");
9495

oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package com.google.auth.oauth2;
3333

3434
import static com.google.auth.Credentials.GOOGLE_DEFAULT_UNIVERSE;
35+
import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT;
3536
import static org.junit.Assert.assertEquals;
3637
import static org.junit.Assert.assertNotNull;
3738
import static org.junit.Assert.fail;
@@ -50,7 +51,6 @@
5051
@RunWith(JUnit4.class)
5152
public class DownscopedCredentialsTest {
5253

53-
private final String TOKEN_EXCHANGE_URL_FORMAT = "https://sts.%s/v1/token";
5454
private static final String SA_PRIVATE_KEY_PKCS8 =
5555
"-----BEGIN PRIVATE KEY-----\n"
5656
+ "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX0PQoe1igW12i"

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<module>oauth2_http</module>
5959
<module>appengine</module>
6060
<module>bom</module>
61+
<module>cab-token-generator</module>
6162
</modules>
6263

6364
<scm>

0 commit comments

Comments
 (0)