GoogleAppEngineAuthenticator implements Authenticator { @Override public User authenticate(HttpServletRequest request) throws ServiceUnavailableException { Attribute attr = Attribute.from(request); if (!EnvUtil.isRunningOnAppEngine()) { return null; } com.google.appengine.api.users.User appEngineUser = null; ApiMethodConfig config = attr.get(Attribute.API_METHOD_CONFIG); if (!attr.isEnabled(Attribute.SKIP_TOKEN_AUTH)) { appEngineUser = getOAuth2User(request, config); } if (appEngineUser == null && shouldTryCookieAuth(config)) { appEngineUser = userService.getCurrentUser(); } if (appEngineUser == null) { return null; } User user = new User(appEngineUser.getEmail()); if (attr.isEnabled(Attribute.REQUIRE_APPENGINE_USER)) { logger.atInfo().log("appEngineUser = %s", appEngineUser); attr.set(Attribute.AUTHENTICATED_APPENGINE_USER, appEngineUser); } else { logger.atInfo().log("User = %s", user); } return user; } GoogleAppEngineAuthenticator();  GoogleAppEngineAuthenticator(OAuthService oauthService, UserService userService); @Override User authenticate(HttpServletRequest request);  }
@Test public void testAuthenticateNonAppEngine() throws ServiceUnavailableException { System.clearProperty(EnvUtil.ENV_APPENGINE_RUNTIME); assertNull(authenticator.authenticate(request)); }
@Test public void testAuthenticateSkipTokenAuth() throws ServiceUnavailableException { attr.set(Attribute.SKIP_TOKEN_AUTH, true); assertNull(authenticator.authenticate(request)); }
@Test public void testAuthenticateOAuth2Fail() throws ServiceUnavailableException { authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override com.google.appengine.api.users.User getOAuth2User(HttpServletRequest request, ApiMethodConfig config) { return null; } @Override boolean shouldTryCookieAuth(ApiMethodConfig config) { return false; } }; assertNull(authenticator.authenticate(request)); }
@Test public void testAuthenticateOAuth2() throws ServiceUnavailableException { authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override com.google.appengine.api.users.User getOAuth2User(HttpServletRequest request, ApiMethodConfig config) { return APP_ENGINE_USER; } }; assertEquals(USER, authenticator.authenticate(request)); assertEquals(APP_ENGINE_USER, attr.get(Attribute.AUTHENTICATED_APPENGINE_USER)); }
@Test public void testAuthenticateSkipTokenAuthCookieAuthFail() throws ServiceUnavailableException { attr.set(Attribute.SKIP_TOKEN_AUTH, true); authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override boolean shouldTryCookieAuth(ApiMethodConfig config) { return true; } }; when(userService.getCurrentUser()).thenReturn(null); assertNull(authenticator.authenticate(request)); }
@Test public void testAuthenticateSkipTokenAuthCookieAuth() throws ServiceUnavailableException { attr.set(Attribute.SKIP_TOKEN_AUTH, true); authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override boolean shouldTryCookieAuth(ApiMethodConfig config) { return true; } }; when(userService.getCurrentUser()).thenReturn(APP_ENGINE_USER); assertEquals(USER, authenticator.authenticate(request)); assertEquals(APP_ENGINE_USER, attr.get(Attribute.AUTHENTICATED_APPENGINE_USER)); }
@Test public void testAuthenticateOAuth2CookieAuthBothFail() throws ServiceUnavailableException { authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override com.google.appengine.api.users.User getOAuth2User(HttpServletRequest request, ApiMethodConfig config) { return null; } @Override boolean shouldTryCookieAuth(ApiMethodConfig config) { return true; } }; when(userService.getCurrentUser()).thenReturn(null); assertNull(authenticator.authenticate(request)); }
@Test public void testAuthenticateOAuth2FailCookieAuth() throws ServiceUnavailableException { authenticator = new GoogleAppEngineAuthenticator(oauthService, userService) { @Override com.google.appengine.api.users.User getOAuth2User(HttpServletRequest request, ApiMethodConfig config) { return null; } @Override boolean shouldTryCookieAuth(ApiMethodConfig config) { return true; } }; when(userService.getCurrentUser()).thenReturn(APP_ENGINE_USER); assertEquals(USER, authenticator.authenticate(request)); assertEquals(APP_ENGINE_USER, attr.get(Attribute.AUTHENTICATED_APPENGINE_USER)); }