|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.example.appengine; |
| 15 | + |
| 16 | +import static com.google.appengine.api.utils.SystemProperty.environment; |
| 17 | + |
| 18 | +import com.google.appengine.api.oauth.OAuthRequestException; |
| 19 | +import com.google.appengine.api.oauth.OAuthService; |
| 20 | +import com.google.appengine.api.oauth.OAuthServiceFactory; |
| 21 | +import com.google.appengine.api.oauth.OAuthServiceFailureException; |
| 22 | +import com.google.appengine.api.users.User; |
| 23 | +import com.google.appengine.api.utils.SystemProperty; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import javax.servlet.Filter; |
| 30 | +import javax.servlet.FilterChain; |
| 31 | +import javax.servlet.FilterConfig; |
| 32 | +import javax.servlet.ServletContext; |
| 33 | +import javax.servlet.ServletException; |
| 34 | +import javax.servlet.ServletRequest; |
| 35 | +import javax.servlet.ServletResponse; |
| 36 | +import javax.servlet.http.HttpServletResponse; |
| 37 | + |
| 38 | +/** |
| 39 | + * Filter to verify that request has a "Authorization: Bearer xxxx" header, |
| 40 | + * and check if xxxx is authorized to use this app. |
| 41 | + * |
| 42 | + * Note - this is to demonstrate the OAuth2 APIs, as it is possible to lockdown some |
| 43 | + * of your app's URL's using cloud console by adding service accounts to the project. |
| 44 | + */ |
| 45 | +public class Oauth2Filter implements Filter { |
| 46 | + |
| 47 | + private ServletContext context; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void init(final FilterConfig config) throws ServletException { |
| 51 | + this.context = config.getServletContext(); |
| 52 | + } |
| 53 | + |
| 54 | + // [START oauth2] |
| 55 | + @Override |
| 56 | + public void doFilter(final ServletRequest servletReq, final ServletResponse servletResp, |
| 57 | + final FilterChain chain) throws IOException, ServletException { |
| 58 | + final String scope = "https://www.googleapis.com/auth/userinfo.email"; |
| 59 | + Set<String> allowedClients = new HashSet<>(); |
| 60 | + |
| 61 | + HttpServletResponse resp = (HttpServletResponse) servletResp; |
| 62 | + |
| 63 | + OAuthService oauth = OAuthServiceFactory.getOAuthService(); |
| 64 | + |
| 65 | + allowedClients.add("407408718192.apps.googleusercontent.com"); // list of client ids to allow |
| 66 | + allowedClients.add("755878275993-j4k7emq6rlupctce1c28enpcrr50vfo1.apps.googleusercontent.com"); |
| 67 | + |
| 68 | + // Only check Oauth2 when in production, skip if run in development. |
| 69 | + SystemProperty.Environment.Value env = environment.value(); |
| 70 | + if (env == SystemProperty.Environment.Value.Production) { // APIs only work in Production |
| 71 | + try { |
| 72 | + User user = oauth.getCurrentUser(scope); |
| 73 | + String tokenAudience = oauth.getClientId(scope); |
| 74 | + |
| 75 | + // The line below is commented out for privacy. |
| 76 | +// context.log("tokenAudience: " + tokenAudience); // Account we match |
| 77 | + |
| 78 | + if (!allowedClients.contains(tokenAudience)) { |
| 79 | + throw new OAuthRequestException("audience of token '" + tokenAudience |
| 80 | + + "' is not in allowed list " + allowedClients); |
| 81 | + } |
| 82 | + } catch (OAuthRequestException ex) { |
| 83 | + resp.sendError(HttpServletResponse.SC_NOT_FOUND); // Not allowed |
| 84 | + return; |
| 85 | + } catch (OAuthServiceFailureException ex) { |
| 86 | + resp.sendError(HttpServletResponse.SC_NOT_FOUND); // some failure - reject |
| 87 | + context.log("oauth2 failure", ex); |
| 88 | + return; |
| 89 | + } |
| 90 | + } |
| 91 | + chain.doFilter(servletReq, servletResp); // continue processing |
| 92 | + } |
| 93 | + // [END oauth2] |
| 94 | + |
| 95 | + @Override |
| 96 | + public void destroy() { } |
| 97 | + |
| 98 | +} |
0 commit comments