Skip to content

Commit

Permalink
Handle the legacy endpoint in the MockTokenServerTransport (#1232)
Browse files Browse the repository at this point in the history
* Handle the legacy endpoint in the MockTokenServerTransport with a warning

* Bump maven-surefire-plugin version for test environment fixes
  • Loading branch information
chingor13 authored Jan 14, 2019
1 parent 8881848 commit 5fd84d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
* {@link Beta} <br/>
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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", 3600);
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", 3600);
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;
}
};
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<version>3.0.0-M3</version>
<configuration>
<argLine>-Xmx1024m</argLine>
<reportNameSuffix>sponge_log</reportNameSuffix>
Expand Down

0 comments on commit 5fd84d4

Please sign in to comment.