Question

Trying to set up google glass quickstart project on apache tomcat 7. It works fine when i am trying to launch it: http://localhost:8080 when i am at my home network. But it does not work when i am in corporate network.

I have set up proxy configs in the context.xml and in the code as well before it tries to exchange access token. I am getting a connection refused exception in the AuthServlet class that is in the source code. I have modified the source code to define system variables for proxy settings here as well.

When i try to connect to any url on the web after setting the proxy, i am able to connect to the internet, but the exchange of token request fails, not sure why this is happening. Could someone please point me in the right direction?

Below is the code that shows proxy set up:

public class AuthServlet extends HttpServlet {
  private static final Logger LOG = Logger.getLogger(AuthServlet.class.getSimpleName());

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
      System.out.println("inside authservlet");
      System.setProperty("http.proxyHost", "proxy server");
      System.setProperty("http.proxyPort", "proxy server port");
      Authenticator.setDefault(
              new Authenticator() {
                  public PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(
                              "username", "password".toCharArray());
                  }
              }
              );
      System.setProperty("http.proxyUser", "username");
      System.setProperty("http.proxyPassword", "password");
      System.out.println("finished proxy set up");
      try {
        URL myURL = new URL("http://google.com");
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
      System.out.println("trying to connect to a URL");
    // If something went wrong, log the error message.
    if (req.getParameter("error") != null) {
      LOG.severe("Something went wrong during auth: " + req.getParameter("error"));
      res.setContentType("text/plain");
      res.getWriter().write("Something went wrong during auth. Please check your log for details");
      return;
    }

    // If we have a code, finish the OAuth 2.0 dance
    if (req.getParameter("code") != null) {
      System.out.println("Got a code. Attempting to exchange for access token.");

      AuthorizationCodeFlow flow = AuthUtil.newAuthorizationCodeFlow();
      TokenResponse tokenResponse =
          flow.newTokenRequest(req.getParameter("code"))
              .setRedirectUri(WebUtil.buildUrl(req, "/oauth2callback")).execute();
      //Error is in the above line.

      // Extract the Google User ID from the ID token in the auth response
      String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId();

Below are the logs with the exception

inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
Got a code. Attempting to exchange for access token.
Feb 18, 2014 1:04:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [oauth2callback] in context with path [] threw exception
java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
    at com.google.glassware.AuthServlet.doGet(AuthServlet.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.google.glassware.ReauthFilter.doFilter(ReauthFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.google.glassware.AuthFilter.doFilter(AuthFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Was it helpful?

Solution

The problem is that you're setting up an HTTP proxy connection, but the OAuth exchange uses an HTTPS connection.

At the very least, you'll need to setup proxy information for HTTPS with something like

systemProperties.setProperty( "https.proxyHost", "proxy server" );
systemProperties.setProperty( "https.proxyPort", "secure proxy port" );

and you should be sure to test it with a connection to https://google.com/

That is, however, the least that you may need to do. The reality is that it can get much MUCH more complicated. HTTPS really isn't made to work through a proxy, and a proxy fundamentally undermines much of the entire security behind it. There are many other things you may need to setup so that you deliberately undermine the security controls, and you really shouldn't do that. (But if you have to, it looks like How do a send an HTTPS request through a proxy in Java? has several pointers for things you will need to do.)

But this is only the start of your problems, to be honest. Although it means you'll be able to send things out through the proxy, the Mirror API uses web-hooks as callbacks for several of its functions, and these callbacks will also be over HTTPS. So your proxy will also need to be able to handle incoming HTTPS connections and route them to your server.

You may want to look into testing things out on App Engine, since these issues will be handled for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top