private void connectToGoogleTalk(final Map<String, String> allToks) {
    final String accessToken = allToks.get("access_token");
    final String refreshToken = allToks.get("refresh_token");

    if (StringUtils.isBlank(accessToken) || StringUtils.isBlank(refreshToken)) {
      log.warn("Not access or refresh token -- not logging in!!");
      return;
    } else {
      // Treat this the same as a credential exception? I.e. what
      // happens if the user cancels?
    }

    this.model.getSettings().setAccessToken(accessToken);
    this.model.getSettings().setRefreshToken(refreshToken);
    this.model.getSettings().setUseGoogleOAuth2(true);
    this.modelIo.write();
    Events.asyncEventBus().post(new RefreshTokenEvent(refreshToken));

    // We kick this off on another thread, as otherwise it would be
    // a Jetty thread, and we're about to kill the server. When the
    // server is killed, the connecting thread would otherwise be
    // interrupted.
    final Thread t =
        new Thread(
            new Runnable() {

              @Override
              public void run() {
                try {
                  xmppHandler.connect();
                  log.debug("Setting gtalk authorized");
                  model.getConnectivity().setGtalkAuthorized(true);
                  internalState.setNotInvited(false);
                  internalState.setModalCompleted(Modal.authorize);
                  internalState.advanceModal(null);
                } catch (final CredentialException e) {
                  log.error("Could not log in with OAUTH?", e);
                  Events.syncModal(model, Modal.authorize);
                } catch (final NotInClosedBetaException e) {
                  log.info("This user is not invited");
                  internalState.setNotInvited(true);
                  Events.syncModal(model, Modal.notInvited);
                } catch (final IOException e) {
                  log.info("We can't connect (internet connection died?).  Retry.", e);
                  Events.syncModal(model, Modal.authorize);
                }
              }
            },
            "Google-Talk-Connect-From-Oauth-Servlet-Thread");
    t.setDaemon(true);
    t.start();
  }
Example #2
0
  public void loadOAuth2UserCredentialsFile(final String filename, final Settings set) {
    if (StringUtils.isBlank(filename)) {
      log.error("No filename specified");
      throw new NullPointerException("No filename specified!");
    }
    final File file = new File(filename);
    if (!(file.exists() && file.canRead())) {
      log.error("Unable to read user credentials from {}", filename);
      throw new IllegalArgumentException("File does not exist! " + filename);
    }
    log.info("Reading user credentials from file \"{}\"", filename);
    try {
      final String json = FileUtils.readFileToString(file, "US-ASCII");
      final JSONObject obj = (JSONObject) JSONValue.parse(json);
      final String username = (String) obj.get("username");
      final String accessToken = (String) obj.get("access_token");
      final String refreshToken = (String) obj.get("refresh_token");
      // Access token is not strictly necessary, so we allow it to be
      // null.
      if (StringUtils.isBlank(username) || StringUtils.isBlank(refreshToken)) {
        log.error("Failed to parse user credentials file \"{}\"", filename);
        throw new Error("Could not load username or refresh_token");
      } else {
        set.setAccessToken(accessToken);
        set.setRefreshToken(refreshToken);
        set.setUseGoogleOAuth2(true);

        // We have to be careful here because classes simply haven't
        // registered as listeners at this point, so listeners have
        // to make sure to also check for an existing refresh token
        // in the settings.
        Events.asyncEventBus().post(new RefreshTokenEvent(refreshToken));
      }
    } catch (final IOException e) {
      log.error("Failed to read file \"{}\"", filename);
      throw new Error("Could not load oauth credentials", e);
    }
  }
Example #3
0
 public static void sync(final SyncPath path, final Object value) {
   Events.asyncEventBus().post(new SyncEvent(path, value));
 }
Example #4
0
 /**
  * Convenience method for syncing the current modal with the frontend.
  *
  * @param model The state model.
  */
 public static void syncModal(final Model model) {
   Events.asyncEventBus().post(new SyncEvent(SyncPath.MODAL, model.getModal()));
 }
Example #5
0
  /**
   * We need to make sure to set the server port before anything is injected -- otherwise we run the
   * risk of running on a completely different port than what is passed on the command line!
   *
   * @param cmd The command line.
   * @param read The model
   */
  private void processCommandLine(final CommandLine cmd, final Model model) {

    if (cmd == null) {
      // Can be true for testing.
      log.error("No command line?");
      return;
    }
    final Settings set = model.getSettings();
    if (cmd.hasOption(Cli.OPTION_SERVER_PORT)) {
      final String serverPortStr = cmd.getOptionValue(Cli.OPTION_SERVER_PORT);
      log.debug("Using command-line proxy port: " + serverPortStr);
      final int serverPort = Integer.parseInt(serverPortStr);
      set.setServerPort(serverPort);
    } else {
      final int existing = set.getServerPort();
      if (existing < 1024) {
        log.debug("Using random give mode proxy port...");
        set.setServerPort(LanternUtils.randomPort());
      }
    }
    log.info("Running give mode proxy on port: {}", set.getServerPort());

    if (cmd.hasOption(Cli.OPTION_KEYSTORE)) {
      LanternUtils.setFallbackKeystorePath(cmd.getOptionValue(Cli.OPTION_KEYSTORE));
    }

    final String ctrlOpt = Cli.OPTION_CONTROLLER_ID;
    if (cmd.hasOption(ctrlOpt)) {
      LanternClientConstants.setControllerId(cmd.getOptionValue(ctrlOpt));
    }

    final String insOpt = Cli.OPTION_INSTANCE_ID;
    if (cmd.hasOption(insOpt)) {
      model.setInstanceId(cmd.getOptionValue(insOpt));
    }

    final String fbOpt = Cli.OPTION_AS_FALLBACK;
    if (cmd.hasOption(fbOpt)) {
      LanternUtils.setFallbackProxy(true);
    }

    final String secOpt = Cli.OPTION_OAUTH2_CLIENT_SECRETS_FILE;
    if (cmd.hasOption(secOpt)) {
      loadOAuth2ClientSecretsFile(cmd.getOptionValue(secOpt), set);
    }

    final String credOpt = Cli.OPTION_OAUTH2_USER_CREDENTIALS_FILE;
    if (cmd.hasOption(credOpt)) {
      loadOAuth2UserCredentialsFile(cmd.getOptionValue(credOpt), set);
    }

    final String ripOpt = Cli.OPTION_REPORT_IP;
    if (cmd.hasOption(ripOpt)) {
      model.setReportIp(cmd.getOptionValue(ripOpt));
    }

    // final Settings set = LanternHub.settings();

    set.setUseTrustedPeers(parseOptionDefaultTrue(cmd, Cli.OPTION_TRUSTED_PEERS));
    set.setUseAnonymousPeers(parseOptionDefaultTrue(cmd, Cli.OPTION_ANON_PEERS));
    set.setUseLaeProxies(parseOptionDefaultTrue(cmd, Cli.OPTION_LAE));
    set.setUseCentralProxies(parseOptionDefaultTrue(cmd, Cli.OPTION_CENTRAL));
    set.setUdpProxyPriority(
        cmd.getOptionValue(Cli.OPTION_UDP_PROXY_PRIORITY, "lower").toUpperCase());

    final boolean tcp = parseOptionDefaultTrue(cmd, Cli.OPTION_TCP);
    final boolean udp = parseOptionDefaultTrue(cmd, Cli.OPTION_UDP);
    IceConfig.setTcp(tcp);
    IceConfig.setUdp(udp);
    set.setTcp(tcp);
    set.setUdp(udp);

    /*
    if (cmd.hasOption(OPTION_USER)) {
        set.setUserId(cmd.getOptionValue(OPTION_USER));
    }
    if (cmd.hasOption(OPTION_PASS)) {
        set.(cmd.getOptionValue(OPTION_PASS));
    }
    */

    if (cmd.hasOption(Cli.OPTION_ACCESS_TOK)) {
      set.setAccessToken(cmd.getOptionValue(Cli.OPTION_ACCESS_TOK));
    }

    if (cmd.hasOption(Cli.OPTION_REFRESH_TOK)) {
      final String refresh = cmd.getOptionValue(Cli.OPTION_REFRESH_TOK);
      set.setRefreshToken(refresh);
      Events.asyncEventBus().post(new RefreshTokenEvent(refresh));
    }
    // option to disable use of keychains in local privacy
    if (cmd.hasOption(Cli.OPTION_DISABLE_KEYCHAIN)) {
      log.info("Disabling use of system keychains");
      set.setKeychainEnabled(false);
    } else {
      set.setKeychainEnabled(true);
    }

    if (cmd.hasOption(Cli.OPTION_PASSWORD_FILE)) {
      loadLocalPasswordFile(cmd.getOptionValue(Cli.OPTION_PASSWORD_FILE));
    }

    if (cmd.hasOption(Cli.OPTION_PUBLIC_API)) {
      set.setBindToLocalhost(false);
    }

    log.info("Running API on port: {}", StaticSettings.getApiPort());
    if (cmd.hasOption(Cli.OPTION_LAUNCHD)) {
      log.debug("Running from launchd or launchd set on command line");
      model.setLaunchd(true);
    } else {
      model.setLaunchd(false);
    }

    if (cmd.hasOption(Cli.OPTION_GIVE)) {
      model.getSettings().setMode(Mode.give);
    } else if (cmd.hasOption(Cli.OPTION_GET)) {
      model.getSettings().setMode(Mode.get);
    }
  }