EndpointsAuthenticator implements Authenticator { @Override public User authenticate(HttpServletRequest request) throws ServiceUnavailableException { Attribute attr = Attribute.from(request); User user = jwtAuthenticator.authenticate(request); if (user == null) { if (EnvUtil.isRunningOnAppEngine() && attr.isEnabled(Attribute.REQUIRE_APPENGINE_USER)) { user = appEngineAuthenticator.authenticate(request); } else { user = oauth2Authenticator.authenticate(request); } } return user; } EndpointsAuthenticator(); @VisibleForTesting  EndpointsAuthenticator(GoogleJwtAuthenticator jwtAuthenticator,
      GoogleAppEngineAuthenticator appEngineAuthenticator,
      GoogleOAuth2Authenticator oauth2Authenticator); @Override User authenticate(HttpServletRequest request);  }
@Test public void testAuthenticate_jwt() throws ServiceUnavailableException { when(jwtAuthenticator.authenticate(request)).thenReturn(USER); assertEquals(USER, authenticator.authenticate(request)); }
@Test public void testAuthenticate_appEngine() throws ServiceUnavailableException { when(jwtAuthenticator.authenticate(request)).thenReturn(null); when(appEngineAuthenticator.authenticate(request)).thenReturn(USER); System.setProperty(EnvUtil.ENV_APPENGINE_RUNTIME, "Production"); attr.set(Attribute.REQUIRE_APPENGINE_USER, true); assertEquals(USER, authenticator.authenticate(request)); }
@Test public void testAuthenticate_oauth2NonAppEngine() throws ServiceUnavailableException { when(jwtAuthenticator.authenticate(request)).thenReturn(null); when(oauth2Authenticator.authenticate(request)).thenReturn(USER); System.setProperty(EnvUtil.ENV_APPENGINE_RUNTIME, ""); attr.set(Attribute.REQUIRE_APPENGINE_USER, true); assertEquals(USER, authenticator.authenticate(request)); }
@Test public void testAuthenticate_oAuth2NotRequireAppEngineUser() throws ServiceUnavailableException { when(jwtAuthenticator.authenticate(request)).thenReturn(null); when(oauth2Authenticator.authenticate(request)).thenReturn(USER); System.setProperty(EnvUtil.ENV_APPENGINE_RUNTIME, "Production"); attr.set(Attribute.REQUIRE_APPENGINE_USER, false); assertEquals(USER, authenticator.authenticate(request)); }