Search in sources :

Example 1 with OAuthServiceFailureException

use of com.google.appengine.api.oauth.OAuthServiceFailureException in project java-docs-samples by GoogleCloudPlatform.

the class Oauth2Filter method doFilter.

// [START oauth2]
@Override
public void doFilter(final ServletRequest servletReq, final ServletResponse servletResp, final FilterChain chain) throws IOException, ServletException {
    final String scope = "https://www.googleapis.com/auth/userinfo.email";
    ImmutableSet<String> allowedClients = new ImmutableSet.Builder<String>().add("407408718192.apps.googleusercontent.com").add("755878275993-j4k7emq6rlupctce1c28enpcrr50vfo1.apps.googleusercontent.com").build();
    HttpServletResponse resp = (HttpServletResponse) servletResp;
    OAuthService oauth = OAuthServiceFactory.getOAuthService();
    // Only check Oauth2 when in production, skip if run in development.
    SystemProperty.Environment.Value env = environment.value();
    if (env == SystemProperty.Environment.Value.Production) {
        // APIs only work in Production
        try {
            String tokenAudience = oauth.getClientId(scope);
            if (!allowedClients.contains(tokenAudience)) {
                throw new OAuthRequestException("audience of token '" + tokenAudience + "' is not in allowed list " + allowedClients);
            }
        } catch (OAuthRequestException ex) {
            // Not allowed
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (OAuthServiceFailureException ex) {
            // some failure - reject
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            context.log("oauth2 failure", ex);
            return;
        }
    }
    // continue processing
    chain.doFilter(servletReq, servletResp);
}
Also used : OAuthService(com.google.appengine.api.oauth.OAuthService) OAuthRequestException(com.google.appengine.api.oauth.OAuthRequestException) ImmutableSet(com.google.common.collect.ImmutableSet) OAuthServiceFailureException(com.google.appengine.api.oauth.OAuthServiceFailureException) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

OAuthRequestException (com.google.appengine.api.oauth.OAuthRequestException)1 OAuthService (com.google.appengine.api.oauth.OAuthService)1 OAuthServiceFailureException (com.google.appengine.api.oauth.OAuthServiceFailureException)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1