GoogleAuth { @VisibleForTesting static TokenInfo parseTokenInfo(HttpRequest request) throws IOException, ServiceUnavailableException { HttpResponse response = request.execute(); int statusCode = response.getStatusCode(); TokenInfo info = response.parseAs(TokenInfo.class); if (statusCode != 200) { String errorDescription = "Unknown error"; if (info != null && info.errorDescription != null) { errorDescription = info.errorDescription; } errorDescription += " (" + statusCode + ")"; if (statusCode >= 500) { logger.atSevere().log("Error validating access token: %s", errorDescription); throw new ServiceUnavailableException("Failed to validate access token"); } logger.atInfo().log("Invalid access token: %s", errorDescription); return null; } if (info == null || Strings.isEmptyOrWhitespace(info.email)) { logger.atWarning().log("Access token does not contain email scope"); return null; } return info; }  static String getAuthToken(HttpServletRequest request); static boolean isJwt(String token); static boolean isOAuth2Token(String token); static TokenInfo getTokenInfoRemote(String token);  }
@Test public void testParseTokenInfo_withEmail() throws Exception { HttpRequest request = constructHttpRequest(SAMPLE_CONTENT_WITH_EMAIL); TokenInfo info = GoogleAuth.parseTokenInfo(request); assertEquals("123.apps.googleusercontent.com", info.clientId); assertEquals("https: + " https: info.scopes); assertEquals("1234567", info.userId); assertEquals("dummy@gmail.com", info.email); }
@Test public void testParseTokenInfo_withoutEmail() throws Exception { HttpRequest request = constructHttpRequest(SAMPLE_CONTENT_WITHOUT_EMAIL); assertNull(GoogleAuth.parseTokenInfo(request)); }
@Test public void testParseTokenInfo_with400() throws Exception { HttpRequest request = constructHttpRequest("{\"error_description\": \"Invalid Value\"}", 400); assertNull(GoogleAuth.parseTokenInfo(request)); }
@Test(expected = ServiceUnavailableException.class) public void testParseTokenInfo_with500() throws Exception { HttpRequest request = constructHttpRequest("{\"error_description\": \"Backend Error\"}", 500); GoogleAuth.parseTokenInfo(request); }