com.google.appengine.api.oauth.OAuthServiceFactory Java Examples

The following examples show how to use com.google.appengine.api.oauth.OAuthServiceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TechGalleryAuthenticator.java    From tech-gallery with Apache License 2.0 6 votes vote down vote up
@Override
public User authenticate(HttpServletRequest req) {
    OAuthService authService = OAuthServiceFactory.getOAuthService();
    com.google.appengine.api.users.User currentUser;

    try {
      currentUser = authService.getCurrentUser(Constants.EMAIL_SCOPE);
      // Check current user..
      if(currentUser != null) {
        String email = currentUser.getEmail();
        // Check domain..
        if(isValidDomain(email) || isWhiteList(email)) {
          return new User(currentUser.getUserId(), currentUser.getEmail());
        }
      }
      throw new RestrictedDomainException(i18n.t("Authorization error"));
    }
    catch(OAuthRequestException  e) {
      log.log(Level.WARNING, "Error when trying to authenticate. Message: " + e.getMessage(), e);
      return null;
    }
}
 
Example #2
Source File: HelloServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doPost(final HttpServletRequest req, final HttpServletResponse resp)
    throws IOException {

  resp.setContentType("text/plain");
  PrintWriter out = resp.getWriter();

  final String scope = "https://www.googleapis.com/auth/userinfo.email";
  OAuthService oauth = OAuthServiceFactory.getOAuthService();
  User user = null;
  try {
    user = oauth.getCurrentUser(scope);
  } catch (OAuthRequestException e) {
    getServletContext().log("Oauth error", e);
    out.print("auth error");
    return;
  }

  out.print("Hello world, welcome to Oauth2: " + user.getEmail());
}
 
Example #3
Source File: ClientSideWebAppFlowTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    initProperties();

    if (isInContainer()) {
        oAuthService = OAuthServiceFactory.getOAuthService();
    }

    client = HttpClients.createDefault();
}
 
Example #4
Source File: GoogleAppEngineAuthenticator.java    From endpoints-java with Apache License 2.0 4 votes vote down vote up
public GoogleAppEngineAuthenticator() {
  this(OAuthServiceFactory.getOAuthService(), UserServiceFactory.getUserService());
}
 
Example #5
Source File: AuthModule.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/** Provides the OAuthService instance. */
@Provides
OAuthService provideOauthService() {
  return OAuthServiceFactory.getOAuthService();
}
 
Example #6
Source File: OAuthServiceServlet.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    logHeaders(req);

    String currentScope = null;
    String lastValidScope = null;
    user = null;
    errorOnScope = null;

    oAuthService = OAuthServiceFactory.getOAuthService();
    Enumeration<String> scopes = req.getHeaders("oauth-test-scope");
    try {
        // If any scopes are invalid, exception is thrown, then error message returned via errorOnScope.
        while (scopes.hasMoreElements()) {
            currentScope = scopes.nextElement();
            user = oAuthService.getCurrentUser(currentScope);
            lastValidScope = currentScope;
            log.info("Valid scope, user: " + user.getEmail());
        }

    } catch (OAuthRequestException e) {
        errorOnScope = e.toString() + " Invalid scope: " + currentScope;
        log.info(errorOnScope);
    }

    String env = SystemProperty.environment.value().toString();
    String responseMsg = env + ",";

    String method = req.getParameter("method");

    if (method == null) {
        resp.getWriter().print(responseMsg + "Error: Must set method parameter.");
        return;
    }

    if (method.equals("env")) {
        responseMsg += env;
        resp.getWriter().print(responseMsg);
        return;
    }

    responseMsg += callMethod(method, lastValidScope);
    resp.getWriter().print(responseMsg);
}