Search in sources :

Example 21 with ApiMethodConfig

use of com.google.api.server.spi.config.model.ApiMethodConfig in project endpoints-java by cloudendpoints.

the class GoogleOAuth2Authenticator method authenticate.

@Override
public User authenticate(HttpServletRequest request) {
    Attribute attr = Attribute.from(request);
    if (attr.isEnabled(Attribute.SKIP_TOKEN_AUTH)) {
        return null;
    }
    String token = GoogleAuth.getAuthToken(request);
    if (!GoogleAuth.isOAuth2Token(token)) {
        return null;
    }
    GoogleAuth.TokenInfo tokenInfo = getTokenInfoRemote(token);
    if (tokenInfo == null) {
        return null;
    }
    ApiMethodConfig config = (ApiMethodConfig) request.getAttribute(Attribute.API_METHOD_CONFIG);
    // Check scopes.
    if (Strings.isEmptyOrWhitespace(tokenInfo.scopes)) {
        logger.warning("Access token does not contain a valid scope");
        return null;
    }
    String[] authorizedScopes = tokenInfo.scopes.split("\\s+");
    if (!config.getScopeExpression().isAuthorized(ImmutableSet.copyOf(authorizedScopes))) {
        logger.warning("Access token does not contain sufficient scopes from: " + config.getScopeExpression());
        return null;
    }
    // Check clientId.
    if (attr.isEnabled(Attribute.ENABLE_CLIENT_ID_WHITELIST) && !GoogleAuth.checkClientId(tokenInfo.clientId, config.getClientIds(), true)) {
        logger.warning("ClientId is not allowed: " + tokenInfo.clientId);
        return null;
    }
    User user = new User(tokenInfo.userId, tokenInfo.email);
    if (attr.isEnabled(Attribute.REQUIRE_APPENGINE_USER)) {
        com.google.appengine.api.users.User appEngineUser = new com.google.appengine.api.users.User(tokenInfo.email, "");
        logger.log(Level.INFO, "appEngineUser = {0}", appEngineUser);
        request.setAttribute(Attribute.AUTHENTICATED_APPENGINE_USER, appEngineUser);
    } else {
        logger.log(Level.INFO, "user = {0}", user);
    }
    return user;
}
Also used : User(com.google.api.server.spi.auth.common.User) TokenInfo(com.google.api.server.spi.auth.GoogleAuth.TokenInfo) Attribute(com.google.api.server.spi.request.Attribute) ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig)

Example 22 with ApiMethodConfig

use of com.google.api.server.spi.config.model.ApiMethodConfig in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReader method readEndpointMethod.

private void readEndpointMethod(ApiClassConfig.MethodConfigMap methodConfigMap, List<EndpointMethod> overrides) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Class<? extends Annotation> apiMethodClass = annotationTypes.get("ApiMethod");
    final EndpointMethod finalMethod = overrides.get(0);
    ApiMethodConfig methodConfig = methodConfigMap.getOrCreate(finalMethod);
    readMethodRequestParameters(finalMethod, methodConfig);
    // Process overrides in reverse order.
    for (EndpointMethod method : Lists.reverse(overrides)) {
        Annotation apiMethod = method.getMethod().getAnnotation(apiMethodClass);
        if (apiMethod != null) {
            readApiMethodInstance(new ApiMethodAnnotationConfig(methodConfig), apiMethod);
        }
    }
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Annotation(java.lang.annotation.Annotation)

Example 23 with ApiMethodConfig

use of com.google.api.server.spi.config.model.ApiMethodConfig in project endpoints-java by cloudendpoints.

the class ApiAnnotationConfigTest method testSetScopesIfSpecified_unspecified.

@Test
public void testSetScopesIfSpecified_unspecified() throws Exception {
    String[] unspecified = { Api.UNSPECIFIED_STRING_FOR_LIST };
    EndpointMethod method = getResultNoParamsMethod();
    annotationConfig.setScopesIfSpecified(unspecified);
    ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(method);
    assertEquals(toScopeExpression(Constant.API_EMAIL_SCOPE), methodConfig.getScopeExpression());
    String[] scopes = { "bleh", "more bleh" };
    annotationConfig.setScopesIfSpecified(scopes);
    annotationConfig.setScopesIfSpecified(null);
    assertEquals(toScopeExpression(scopes), config.getScopeExpression());
    annotationConfig.setScopesIfSpecified(scopes);
    annotationConfig.setScopesIfSpecified(unspecified);
    assertEquals(toScopeExpression(scopes), config.getScopeExpression());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) EndpointMethod(com.google.api.server.spi.EndpointMethod) Test(org.junit.Test)

Example 24 with ApiMethodConfig

use of com.google.api.server.spi.config.model.ApiMethodConfig in project endpoints-java by cloudendpoints.

the class ApiAnnotationConfigTest method testSetScopesIfSpecified.

@Test
public void testSetScopesIfSpecified() throws Exception {
    String[] scopes = { "foo", "bar" };
    annotationConfig.setScopesIfSpecified(scopes);
    assertEquals(toScopeExpression(scopes), config.getScopeExpression());
    ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(getResultNoParamsMethod());
    assertEquals(toScopeExpression(scopes), methodConfig.getScopeExpression());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Test(org.junit.Test)

Example 25 with ApiMethodConfig

use of com.google.api.server.spi.config.model.ApiMethodConfig in project endpoints-java by cloudendpoints.

the class ApiAnnotationConfigTest method testSetAudiencesIfSpecified.

@Test
public void testSetAudiencesIfSpecified() throws Exception {
    String[] audiences = { "foo", "bar" };
    annotationConfig.setAudiencesIfSpecified(audiences);
    assertEquals(Arrays.asList(audiences), config.getAudiences());
    ApiMethodConfig methodConfig = config.getApiClassConfig().getMethods().getOrCreate(getResultNoParamsMethod());
    assertEquals(Arrays.asList(audiences), methodConfig.getAudiences());
}
Also used : ApiMethodConfig(com.google.api.server.spi.config.model.ApiMethodConfig) Test(org.junit.Test)

Aggregations

ApiMethodConfig (com.google.api.server.spi.config.model.ApiMethodConfig)54 Test (org.junit.Test)37 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)26 EndpointMethod (com.google.api.server.spi.EndpointMethod)16 SimpleLevelOverridingInheritedApi (com.google.api.server.spi.testing.SimpleLevelOverridingInheritedApi)10 Api (com.google.api.server.spi.config.Api)8 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)8 User (com.google.api.server.spi.auth.common.User)5 PassAuthenticator (com.google.api.server.spi.testing.PassAuthenticator)5 PassPeerAuthenticator (com.google.api.server.spi.testing.PassPeerAuthenticator)5 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)4 ApiAuthConfig (com.google.api.server.spi.config.model.ApiAuthConfig)3 ApiCacheControlConfig (com.google.api.server.spi.config.model.ApiCacheControlConfig)3 ApiFrontendLimitsConfig (com.google.api.server.spi.config.model.ApiFrontendLimitsConfig)3 Attribute (com.google.api.server.spi.request.Attribute)3 Named (com.google.api.server.spi.config.Named)2 ApiSerializationConfig (com.google.api.server.spi.config.model.ApiSerializationConfig)2 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)2 FailAuthenticator (com.google.api.server.spi.testing.FailAuthenticator)2 FailPeerAuthenticator (com.google.api.server.spi.testing.FailPeerAuthenticator)2