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;
}
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);
}
}
}
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());
}
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());
}
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());
}
Aggregations