Skip to content

Commit 5fd84d4

Browse files
authored
Handle the legacy endpoint in the MockTokenServerTransport (#1232)
* Handle the legacy endpoint in the MockTokenServerTransport with a warning * Bump maven-surefire-plugin version for test environment fixes
1 parent 8881848 commit 5fd84d4

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/testing/auth/oauth2/MockTokenServerTransport.java

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.util.HashMap;
3333
import java.util.Map;
34+
import java.util.logging.Logger;
3435

3536
/**
3637
* {@link Beta} <br/>
@@ -40,6 +41,12 @@
4041
*/
4142
@Beta
4243
public class MockTokenServerTransport extends MockHttpTransport {
44+
/** Old URL of Google's token server (for backwards compatibility) */
45+
private static final String LEGACY_TOKEN_SERVER_URL =
46+
"https://accounts.google.com/o/oauth2/token";
47+
48+
private static final Logger LOGGER = Logger.getLogger(MockTokenServerTransport.class.getName());
49+
4350
static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
4451
static final JsonFactory JSON_FACTORY = new JacksonFactory();
4552
final String tokenServerUrl;
@@ -70,64 +77,71 @@ public void addRefreshToken(String refreshToken, String accessTokenToReturn) {
7077
@Override
7178
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
7279
if (url.equals(tokenServerUrl)) {
73-
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {
74-
@Override
75-
public LowLevelHttpResponse execute() throws IOException {
76-
String content = this.getContentAsString();
77-
Map<String, String> query = TestUtils.parseQuery(content);
78-
String accessToken = null;
79-
80-
String foundId = query.get("client_id");
81-
if (foundId != null) {
82-
if (!clients.containsKey(foundId)) {
83-
throw new IOException("Client ID not found.");
84-
}
85-
String foundSecret = query.get("client_secret");
86-
String expectedSecret = clients.get(foundId);
87-
if (foundSecret == null || !foundSecret.equals(expectedSecret)) {
88-
throw new IOException("Client secret not found.");
89-
}
90-
String foundRefresh = query.get("refresh_token");
91-
if (!refreshTokens.containsKey(foundRefresh)) {
92-
throw new IOException("Refresh Token not found.");
93-
}
94-
accessToken = refreshTokens.get(foundRefresh);
95-
} else if (query.containsKey("grant_type")) {
96-
String grantType = query.get("grant_type");
97-
if (!EXPECTED_GRANT_TYPE.equals(grantType)) {
98-
throw new IOException("Unexpected Grant Type.");
99-
}
100-
String assertion = query.get("assertion");
101-
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
102-
String foundEmail = signature.getPayload().getIssuer();
103-
if (!serviceAccounts.containsKey(foundEmail)) {
104-
throw new IOException("Service Account Email not found as issuer.");
105-
}
106-
accessToken = serviceAccounts.get(foundEmail);
107-
String foundScopes = (String) signature.getPayload().get("scope");
108-
if (foundScopes == null || foundScopes.length() == 0) {
109-
throw new IOException("Scopes not found.");
110-
}
111-
} else {
112-
throw new IOException("Unknown token type.");
80+
return buildTokenRequest(url);
81+
} else if (url.equals(LEGACY_TOKEN_SERVER_URL)) {
82+
LOGGER.warning("Your configured token_uri is using a legacy endpoint. You may want to "
83+
+ "redownload your credentials.");
84+
return buildTokenRequest(url);
85+
}
86+
return super.buildRequest(method, url);
87+
}
88+
89+
private MockLowLevelHttpRequest buildTokenRequest(String url) {
90+
return new MockLowLevelHttpRequest(url) {
91+
@Override
92+
public LowLevelHttpResponse execute() throws IOException {
93+
String content = this.getContentAsString();
94+
Map<String, String> query = TestUtils.parseQuery(content);
95+
String accessToken = null;
96+
97+
String foundId = query.get("client_id");
98+
if (foundId != null) {
99+
if (!clients.containsKey(foundId)) {
100+
throw new IOException("Client ID not found.");
101+
}
102+
String foundSecret = query.get("client_secret");
103+
String expectedSecret = clients.get(foundId);
104+
if (foundSecret == null || !foundSecret.equals(expectedSecret)) {
105+
throw new IOException("Client secret not found.");
113106
}
107+
String foundRefresh = query.get("refresh_token");
108+
if (!refreshTokens.containsKey(foundRefresh)) {
109+
throw new IOException("Refresh Token not found.");
110+
}
111+
accessToken = refreshTokens.get(foundRefresh);
112+
} else if (query.containsKey("grant_type")) {
113+
String grantType = query.get("grant_type");
114+
if (!EXPECTED_GRANT_TYPE.equals(grantType)) {
115+
throw new IOException("Unexpected Grant Type.");
116+
}
117+
String assertion = query.get("assertion");
118+
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
119+
String foundEmail = signature.getPayload().getIssuer();
120+
if (!serviceAccounts.containsKey(foundEmail)) {
121+
throw new IOException("Service Account Email not found as issuer.");
122+
}
123+
accessToken = serviceAccounts.get(foundEmail);
124+
String foundScopes = (String) signature.getPayload().get("scope");
125+
if (foundScopes == null || foundScopes.length() == 0) {
126+
throw new IOException("Scopes not found.");
127+
}
128+
} else {
129+
throw new IOException("Unknown token type.");
130+
}
114131

115-
// Create the JSon response
116-
GenericJson refreshContents = new GenericJson();
117-
refreshContents.setFactory(JSON_FACTORY);
118-
refreshContents.put("access_token", accessToken);
119-
refreshContents.put("expires_in", 3600);
120-
refreshContents.put("token_type", "Bearer");
121-
String refreshText = refreshContents.toPrettyString();
132+
// Create the JSon response
133+
GenericJson refreshContents = new GenericJson();
134+
refreshContents.setFactory(JSON_FACTORY);
135+
refreshContents.put("access_token", accessToken);
136+
refreshContents.put("expires_in", 3600);
137+
refreshContents.put("token_type", "Bearer");
138+
String refreshText = refreshContents.toPrettyString();
122139

123-
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse()
140+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse()
124141
.setContentType(Json.MEDIA_TYPE)
125142
.setContent(refreshText);
126-
return response;
127-
}
128-
};
129-
return request;
130-
}
131-
return super.buildRequest(method, url);
143+
return response;
144+
}
145+
};
132146
}
133147
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
</plugin>
347347
<plugin>
348348
<artifactId>maven-surefire-plugin</artifactId>
349-
<version>2.19.1</version>
349+
<version>3.0.0-M3</version>
350350
<configuration>
351351
<argLine>-Xmx1024m</argLine>
352352
<reportNameSuffix>sponge_log</reportNameSuffix>

0 commit comments

Comments
 (0)