Esempio n. 1
0
  /**
   * Generates X-GOOGLE-TOKEN response by communication with http://www.google.com (algorithm from
   * MGTalk/NetworkThread.java)
   *
   * @param userName
   * @param passwd
   * @return
   */
  public String responseXGoogleToken() {
    try {
      String firstUrl =
          "https://www.google.com:443/accounts/ClientAuth?Email="
              + Strconv.unicodeToUTF(account.getUserName())
              + "%40"
              + account.getServer()
              + "&Passwd="
              + Strconv.unicodeToUTF(account.getPassword())
              + "&PersistentCookie=false&source=bombusqd";

      // log.addMessage("Connecting to www.google.com");
      // #if Android
      // #            URL url = new URL(firstUrl);
      // #            HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
      // #            InputStream is = c.getInputStream();
      // #else
      HttpsConnection c = (HttpsConnection) Connector.open(firstUrl);
      InputStream is = c.openInputStream();
      // #endif

      String sid = readLine(is);
      if (!sid.startsWith("SID=")) {
        throw new SecurityException(SR.get(SR.MS_LOGIN_FAILED));
      }

      String lsid = readLine(is);

      String secondUrl =
          "https://www.google.com:443/accounts/IssueAuthToken?"
              + sid
              + "&"
              + lsid
              + "&service=mail&Session=true";
      is.close();
      // #if Android
      // #            url = new URL(secondUrl);
      // #            c = (HttpsURLConnection) url.openConnection();
      // #            is = c.getInputStream();
      // #else
      c.close();
      c = null;
      c = (HttpsConnection) Connector.open(secondUrl);
      is = c.openInputStream();
      // #endif
      String token = "\0" + Strconv.unicodeToUTF(account.getUserName()) + "\0" + readLine(is);
      is.close();
      // #if !Android
      c.close();
      // #endif
      return Strconv.toBase64(token);

    } catch (javax.microedition.pki.CertificateException e) {
      throw new SecurityException(e.getMessage());
    } catch (SecurityException e) {
      throw e;
    } catch (Exception e) {
      // #ifdef DEBUG
      // #             e.printStackTrace();
      // #endif
      // listener.loginFailed("Google token error");
    }
    return null;
  }