Search in sources :

Example 1 with AuthScopeExpression

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

the class DiscoveryGenerator method writeApiMethod.

private void writeApiMethod(ApiConfig config, String servicePath, RestDescription doc, ApiMethodConfig methodConfig, SchemaRepository schemaRepo, AuthScopeRepository scopeRepo) {
    List<String> parts = DOT_SPLITTER.splitToList(methodConfig.getFullMethodName());
    Map<String, RestMethod> methods = getMethodMapFromDoc(doc, parts);
    Map<String, JsonSchema> parameters = convertMethodParameters(methodConfig);
    AuthScopeExpression scopeExpression = methodConfig.getScopeExpression();
    RestMethod method = new RestMethod().setDescription(methodConfig.getDescription()).setHttpMethod(methodConfig.getHttpMethod()).setId(methodConfig.getFullMethodName()).setPath(methodConfig.getCanonicalPath().substring(servicePath.length())).setScopes(AuthScopeExpressions.encodeMutable(scopeExpression));
    scopeRepo.add(scopeExpression);
    List<String> parameterOrder = computeParameterOrder(methodConfig);
    if (!parameterOrder.isEmpty()) {
        method.setParameterOrder(parameterOrder);
    }
    if (!parameters.isEmpty()) {
        method.setParameters(parameters);
    }
    ApiParameterConfig requestParamConfig = getAndCheckMethodRequestResource(methodConfig);
    if (requestParamConfig != null) {
        TypeToken<?> requestType = requestParamConfig.getSchemaBaseType();
        Schema schema = schemaRepo.getOrAdd(requestType, config);
        method.setRequest(new Request().set$ref(schema.name()).setParameterName("resource"));
    }
    if (methodConfig.hasResourceInResponse()) {
        TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), config);
        Schema schema = schemaRepo.getOrAdd(returnType, config);
        method.setResponse(new Response().set$ref(schema.name()));
    }
    methods.put(parts.get(parts.size() - 1), method);
}
Also used : Response(com.google.api.services.discovery.model.RestMethod.Response) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) AuthScopeExpression(com.google.api.server.spi.config.scope.AuthScopeExpression) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Schema(com.google.api.server.spi.config.model.Schema) JsonSchema(com.google.api.services.discovery.model.JsonSchema) Request(com.google.api.services.discovery.model.RestMethod.Request) RestMethod(com.google.api.services.discovery.model.RestMethod)

Example 2 with AuthScopeExpression

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

the class GoogleAppEngineAuthenticator method getOAuth2User.

@VisibleForTesting
com.google.appengine.api.users.User getOAuth2User(HttpServletRequest request, ApiMethodConfig config) throws ServiceUnavailableException {
    String token = GoogleAuth.getAuthToken(request);
    if (!GoogleAuth.isOAuth2Token(token)) {
        return null;
    }
    AuthScopeExpression scopeExpression = config.getScopeExpression();
    String[] allScopes = scopeExpression.getAllScopes();
    String clientId = null;
    if (EnvUtil.isRunningOnAppEngineProd()) {
        try {
            String[] authorizedScopes = oauthService.getAuthorizedScopes(allScopes);
            boolean authorized = false;
            if (authorizedScopes != null) {
                // Authorize against the scopes based on the scope expression.
                authorized = scopeExpression.isAuthorized(ImmutableSet.copyOf(authorizedScopes));
            }
            if (!authorized) {
                logger.atWarning().log("Access token does not contain sufficient scopes from: %s", scopeExpression);
                return null;
            }
            clientId = oauthService.getClientId(allScopes);
        } catch (OAuthRequestException e) {
            logger.atWarning().withCause(e).log("Failed to get client id for '%s'", scopeExpression);
            return null;
        }
    } else {
        // Dev env.
        clientId = getOAuth2ClientIdDev(token);
    }
    // Check client id.
    if ((Attribute.from(request).isEnabled(Attribute.ENABLE_CLIENT_ID_WHITELIST) && !GoogleAuth.checkClientId(clientId, config.getClientIds(), true))) {
        logger.atWarning().log("ClientId is not allowed: %s", clientId);
        return null;
    }
    try {
        com.google.appengine.api.users.User appEngineUser = oauthService.getCurrentUser(allScopes);
        return appEngineUser;
    } catch (OAuthRequestException e) {
        logger.atWarning().withCause(e).log("Failed to get user for '%s'", scopeExpression);
    }
    return null;
}
Also used : OAuthRequestException(com.google.appengine.api.oauth.OAuthRequestException) AuthScopeExpression(com.google.api.server.spi.config.scope.AuthScopeExpression) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with AuthScopeExpression

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

the class ApiClassConfigTest method testDefaultsOverriddenWithLocal.

@Test
public void testDefaultsOverriddenWithLocal() {
    config.setResource("bleh");
    config.setAuthLevel(AuthLevel.REQUIRED);
    AuthScopeExpression scopes = AuthScopeExpressions.interpret("scope1", "scope2");
    config.setScopeExpression(scopes);
    List<String> audiences = Lists.newArrayList("audience1", "audience2");
    config.setAudiences(audiences);
    List<String> clientIds = Lists.newArrayList("ci1", "ci2");
    config.setClientIds(clientIds);
    config.setUseDatastore(true);
    assertEquals("bleh", config.getResource());
    assertEquals(AuthLevel.REQUIRED, config.getAuthLevel());
    assertEquals(scopes, config.getScopeExpression());
    assertEquals(audiences, config.getAudiences());
    assertEquals(clientIds, config.getClientIds());
    assertTrue(config.getUseDatastore());
}
Also used : AuthScopeExpression(com.google.api.server.spi.config.scope.AuthScopeExpression) Test(org.junit.Test)

Aggregations

AuthScopeExpression (com.google.api.server.spi.config.scope.AuthScopeExpression)3 ApiParameterConfig (com.google.api.server.spi.config.model.ApiParameterConfig)1 Schema (com.google.api.server.spi.config.model.Schema)1 JsonSchema (com.google.api.services.discovery.model.JsonSchema)1 RestMethod (com.google.api.services.discovery.model.RestMethod)1 Request (com.google.api.services.discovery.model.RestMethod.Request)1 Response (com.google.api.services.discovery.model.RestMethod.Response)1 OAuthRequestException (com.google.appengine.api.oauth.OAuthRequestException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Test (org.junit.Test)1