From 2ca700324f161f42369c65a3abd2b7b44e73d0ef Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 8 Jan 2019 09:49:16 -0800 Subject: [PATCH 1/2] Handle the legacy endpoint in the MockTokenServerTransport with a warning --- .../auth/oauth2/MockTokenServerTransport.java | 122 ++++++++++-------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/testing/auth/oauth2/MockTokenServerTransport.java b/google-api-client/src/main/java/com/google/api/client/googleapis/testing/auth/oauth2/MockTokenServerTransport.java index 0dc4710cd..d76eccbe0 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/testing/auth/oauth2/MockTokenServerTransport.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/testing/auth/oauth2/MockTokenServerTransport.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; /** * {@link Beta}
@@ -40,6 +41,12 @@ */ @Beta public class MockTokenServerTransport extends MockHttpTransport { + /** Old URL of Google's token server (for backwards compatibility) */ + private static final String LEGACY_TOKEN_SERVER_URL = + "https://accounts.google.com/o/oauth2/token"; + + private static final Logger LOGGER = Logger.getLogger(MockTokenServerTransport.class.getName()); + static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer"; static final JsonFactory JSON_FACTORY = new JacksonFactory(); final String tokenServerUrl; @@ -70,64 +77,71 @@ public void addRefreshToken(String refreshToken, String accessTokenToReturn) { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { if (url.equals(tokenServerUrl)) { - MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) { - @Override - public LowLevelHttpResponse execute() throws IOException { - String content = this.getContentAsString(); - Map query = TestUtils.parseQuery(content); - String accessToken = null; - - String foundId = query.get("client_id"); - if (foundId != null) { - if (!clients.containsKey(foundId)) { - throw new IOException("Client ID not found."); - } - String foundSecret = query.get("client_secret"); - String expectedSecret = clients.get(foundId); - if (foundSecret == null || !foundSecret.equals(expectedSecret)) { - throw new IOException("Client secret not found."); - } - String foundRefresh = query.get("refresh_token"); - if (!refreshTokens.containsKey(foundRefresh)) { - throw new IOException("Refresh Token not found."); - } - accessToken = refreshTokens.get(foundRefresh); - } else if (query.containsKey("grant_type")) { - String grantType = query.get("grant_type"); - if (!EXPECTED_GRANT_TYPE.equals(grantType)) { - throw new IOException("Unexpected Grant Type."); - } - String assertion = query.get("assertion"); - JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion); - String foundEmail = signature.getPayload().getIssuer(); - if (!serviceAccounts.containsKey(foundEmail)) { - throw new IOException("Service Account Email not found as issuer."); - } - accessToken = serviceAccounts.get(foundEmail); - String foundScopes = (String) signature.getPayload().get("scope"); - if (foundScopes == null || foundScopes.length() == 0) { - throw new IOException("Scopes not found."); - } - } else { - throw new IOException("Unknown token type."); + return buildTokenRequest(url); + } else if (url.equals(LEGACY_TOKEN_SERVER_URL)) { + LOGGER.warning("Your configured token_uri is using a legacy endpoint. You may want to " + + "redownload your credentials."); + return buildTokenRequest(url); + } + return super.buildRequest(method, url); + } + + private MockLowLevelHttpRequest buildTokenRequest(String url) { + return new MockLowLevelHttpRequest(url) { + @Override + public LowLevelHttpResponse execute() throws IOException { + String content = this.getContentAsString(); + Map query = TestUtils.parseQuery(content); + String accessToken = null; + + String foundId = query.get("client_id"); + if (foundId != null) { + if (!clients.containsKey(foundId)) { + throw new IOException("Client ID not found."); + } + String foundSecret = query.get("client_secret"); + String expectedSecret = clients.get(foundId); + if (foundSecret == null || !foundSecret.equals(expectedSecret)) { + throw new IOException("Client secret not found."); } + String foundRefresh = query.get("refresh_token"); + if (!refreshTokens.containsKey(foundRefresh)) { + throw new IOException("Refresh Token not found."); + } + accessToken = refreshTokens.get(foundRefresh); + } else if (query.containsKey("grant_type")) { + String grantType = query.get("grant_type"); + if (!EXPECTED_GRANT_TYPE.equals(grantType)) { + throw new IOException("Unexpected Grant Type."); + } + String assertion = query.get("assertion"); + JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion); + String foundEmail = signature.getPayload().getIssuer(); + if (!serviceAccounts.containsKey(foundEmail)) { + throw new IOException("Service Account Email not found as issuer."); + } + accessToken = serviceAccounts.get(foundEmail); + String foundScopes = (String) signature.getPayload().get("scope"); + if (foundScopes == null || foundScopes.length() == 0) { + throw new IOException("Scopes not found."); + } + } else { + throw new IOException("Unknown token type."); + } - // Create the JSon response - GenericJson refreshContents = new GenericJson(); - refreshContents.setFactory(JSON_FACTORY); - refreshContents.put("access_token", accessToken); - refreshContents.put("expires_in", 3600000); - refreshContents.put("token_type", "Bearer"); - String refreshText = refreshContents.toPrettyString(); + // Create the JSon response + GenericJson refreshContents = new GenericJson(); + refreshContents.setFactory(JSON_FACTORY); + refreshContents.put("access_token", accessToken); + refreshContents.put("expires_in", 3600000); + refreshContents.put("token_type", "Bearer"); + String refreshText = refreshContents.toPrettyString(); - MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() .setContentType(Json.MEDIA_TYPE) .setContent(refreshText); - return response; - } - }; - return request; - } - return super.buildRequest(method, url); + return response; + } + }; } } From e9689c6c24c48475b05a248ef5808200a34a9fcc Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 14 Jan 2019 13:10:13 -0800 Subject: [PATCH 2/2] Bump maven-surefire-plugin version for test environment fixes --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index aede76e61..09f91c121 100644 --- a/pom.xml +++ b/pom.xml @@ -346,7 +346,7 @@ maven-surefire-plugin - 2.19.1 + 3.0.0-M3 -Xmx1024m sponge_log