/** Marshall the given parameter object, and output to a SdkJsonGenerator */
  public void marshall(AccountSettings accountSettings, StructuredJsonGenerator jsonGenerator) {

    if (accountSettings == null) {
      throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }

    try {
      jsonGenerator.writeStartObject();

      if (accountSettings.getAwsAccountNumber() != null) {
        jsonGenerator
            .writeFieldName("awsAccountNumber")
            .writeValue(accountSettings.getAwsAccountNumber());
      }

      java.util.Map<String, Integer> unmeteredDevicesMap = accountSettings.getUnmeteredDevices();
      if (unmeteredDevicesMap != null) {
        jsonGenerator.writeFieldName("unmeteredDevices");
        jsonGenerator.writeStartObject();

        for (Map.Entry<String, Integer> unmeteredDevicesMapValue : unmeteredDevicesMap.entrySet()) {
          if (unmeteredDevicesMapValue.getValue() != null) {
            jsonGenerator.writeFieldName(unmeteredDevicesMapValue.getKey());

            jsonGenerator.writeValue(unmeteredDevicesMapValue.getValue());
          }
        }
        jsonGenerator.writeEndObject();
      }

      java.util.Map<String, Integer> unmeteredRemoteAccessDevicesMap =
          accountSettings.getUnmeteredRemoteAccessDevices();
      if (unmeteredRemoteAccessDevicesMap != null) {
        jsonGenerator.writeFieldName("unmeteredRemoteAccessDevices");
        jsonGenerator.writeStartObject();

        for (Map.Entry<String, Integer> unmeteredRemoteAccessDevicesMapValue :
            unmeteredRemoteAccessDevicesMap.entrySet()) {
          if (unmeteredRemoteAccessDevicesMapValue.getValue() != null) {
            jsonGenerator.writeFieldName(unmeteredRemoteAccessDevicesMapValue.getKey());

            jsonGenerator.writeValue(unmeteredRemoteAccessDevicesMapValue.getValue());
          }
        }
        jsonGenerator.writeEndObject();
      }

      jsonGenerator.writeEndObject();
    } catch (Throwable t) {
      throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t);
    }
  }
Ejemplo n.º 2
0
 /**
  * @param secret
  * @return
  */
 public String decrypt(String secret) {
   try {
     Cipher cipher = Cryptos.getAES(Cipher.ENCRYPT_MODE, keyHash, settings.cryptoKeySize());
     return Codecs.toUTF8(cipher.doFinal(Codecs.decodeBase64(secret, false)));
   } catch (BadPaddingException | IllegalBlockSizeException ex) {
     throw AppeException.wrap(ex);
   }
 }
Ejemplo n.º 3
0
 /**
  * Generate password that match policy
  *
  * @return
  */
 public String randomPassword() {
   return PRNG.nextBInteger(settings.passwordMinLength()).abs().toString();
 }
Ejemplo n.º 4
0
 /**
  * return a hash of a given secret if any.
  *
  * @param secret
  * @return
  */
 public String hash(String secret) {
   return Codecs.encodeBase64(
       Digests.sha2(settings.cryptoHashSalt(), Codecs.toBytes(secret)), false);
 }
Ejemplo n.º 5
0
 /** @param settings */
 public CredentialsCrypto(AccountSettings settings) {
   this.settings = settings;
   this.keyHash = Digests.sha2(Codecs.toBytes(settings.cryptoKeySeed()));
 }
Ejemplo n.º 6
0
  public void testAccountMethods() throws Exception {
    User original = twitter1.verifyCredentials();
    assertNotNull(DataObjectFactory.getRawJSON(original));
    assertEquals(original, DataObjectFactory.createUser(DataObjectFactory.getRawJSON(original)));
    if (original.getScreenName().endsWith("new") || original.getName().endsWith("new")) {
      original =
          twitter1.updateProfile(
              "twit4j",
              "http://yusuke.homeip.net/twitter4j/",
              "location:",
              "Hi there, I do test a lot!new");
    }
    String newName, newURL, newLocation, newDescription;
    String neu = "new";
    newName = original.getName() + neu;
    newURL = original.getURL() + neu;
    newLocation = new Date().toString();
    newDescription = original.getDescription() + neu;

    User altered = twitter1.updateProfile(newName, newURL, newLocation, newDescription);
    assertNotNull(DataObjectFactory.getRawJSON(altered));
    assertEquals(original, DataObjectFactory.createUser(DataObjectFactory.getRawJSON(original)));
    assertEquals(altered, DataObjectFactory.createUser(DataObjectFactory.getRawJSON(altered)));
    twitter1.updateProfile(
        original.getName(),
        original.getURL().toString(),
        original.getLocation(),
        original.getDescription());
    assertEquals(newName, altered.getName());
    assertEquals(newURL, altered.getURL().toString());
    assertEquals(newLocation, altered.getLocation());
    assertEquals(newDescription, altered.getDescription());

    User eu;
    eu = twitter1.updateProfileColors("f00", "f0f", "0ff", "0f0", "f0f");
    assertNotNull(DataObjectFactory.getRawJSON(eu));
    assertEquals(eu, DataObjectFactory.createUser(DataObjectFactory.getRawJSON(eu)));
    assertEquals("f00", eu.getProfileBackgroundColor());
    assertEquals("f0f", eu.getProfileTextColor());
    assertEquals("0ff", eu.getProfileLinkColor());
    assertEquals("0f0", eu.getProfileSidebarFillColor());
    assertEquals("f0f", eu.getProfileSidebarBorderColor());
    assertTrue(eu.isProfileUseBackgroundImage());
    assertFalse(eu.isShowAllInlineMedia());
    assertTrue(0 <= eu.getListedCount());
    eu = twitter1.updateProfileColors("87bc44", "9ae4e8", "000000", "0000ff", "e0ff92");
    assertNotNull(DataObjectFactory.getRawJSON(eu));
    assertEquals(eu, DataObjectFactory.createUser(DataObjectFactory.getRawJSON(eu)));
    assertEquals("87bc44", eu.getProfileBackgroundColor());
    assertEquals("9ae4e8", eu.getProfileTextColor());
    assertEquals("000000", eu.getProfileLinkColor());
    assertEquals("0000ff", eu.getProfileSidebarFillColor());
    assertEquals("e0ff92", eu.getProfileSidebarBorderColor());

    AccountSettings settings = twitter1.getAccountSettings();
    assertTrue(settings.isSleepTimeEnabled());
    assertTrue(settings.isGeoEnabled());
    assertEquals("en", settings.getLanguage());
    assertEquals("Rome", settings.getTimeZone().getName());
    assertTrue(settings.isAlwaysUseHttps());
    assertFalse(settings.isDiscoverableByEmail());
    Location[] locations = settings.getTrendLocations();
    assertTrue(0 < locations.length);

    AccountSettings intermSettings =
        twitter1.updateAccountSettings(1 /* GLOBAL */, true, "23", "08", "Helsinki", "it");
    assertTrue(intermSettings.isSleepTimeEnabled());
    assertEquals(intermSettings.getSleepStartTime(), "23");
    assertEquals(intermSettings.getSleepEndTime(), "8");
    assertTrue(intermSettings.isGeoEnabled());
    assertEquals("it", intermSettings.getLanguage());
    assertTrue(intermSettings.isAlwaysUseHttps());
    assertFalse(intermSettings.isDiscoverableByEmail());
    assertEquals("Helsinki", intermSettings.getTimeZone().getName());
    Location[] intermLocations = intermSettings.getTrendLocations();
    assertTrue(0 < intermLocations.length);

    AccountSettings lastSettings =
        twitter1.updateAccountSettings(
            settings.getTrendLocations()[0].getWoeid(),
            settings.isSleepTimeEnabled(),
            settings.getSleepStartTime(),
            settings.getSleepStartTime(),
            settings.getTimeZone().getName(),
            settings.getLanguage());
    assertEquals(settings.getLanguage(), lastSettings.getLanguage());
    assertEquals(settings.isSleepTimeEnabled(), lastSettings.isSleepTimeEnabled());
    assertEquals(settings.getTimeZone().getName(), lastSettings.getTimeZone().getName());
    assertEquals(settings.getSleepEndTime(), lastSettings.getSleepEndTime());
  }
  void addAccount() {

    try {
      String accountName = editAccountName.getText().toString();
      String accountType = serverInfo.getAccountType();
      Properties propSettings = serverInfo.getAccountParamsAsProperties();

      AccountSettings settings = null;

      if (accountType.equals(org.exfio.csyncdroid.Constants.ACCOUNT_TYPE_FXACCOUNT)) {
        settings = new FxAccountAccountSettings();
      } else if (accountType.equals(org.exfio.csyncdroid.Constants.ACCOUNT_TYPE_LEGACYV5)) {
        settings = new LegacyV5AccountSettings();
      } else {
        settings = new ExfioPeerAccountSettings();

        // FIXME - Complete migration of this code from QueryServerDialogFrament

        /*
        WeaveClient weaveClient = WeaveClientFactory.getInstance(adParams);

        // Check underlying weave account is initialised
        if ( !weaveClient.isInitialised() ) {
        	throw new WeaveException(String.format("Weave account '%s@%s' not initialised.", adParams.user, adParams.accountServer));
        }

        ExfioPeerV1 auth = new ExfioPeerV1(weaveClient);

        // Check exfio peer account is initialised
        if ( !auth.isInitialised() ) {
        	throw new WeaveException(String.format("eXfio Peer account '%s@%s' not initialised.", adParams.user, adParams.accountServer));
        }

        //Initialise sqldroid jdbc provider
        Class.forName("org.sqldroid.SQLDroidDriver");

        String clientName = OSUtils.getPrettyName();

        //Build unique account guid that is also valid filename
        guid = WeaveAccount.generateAccountGuid(accountServer, username);

        //Create database file if it does not already exist

        android.database.sqlite.SQLiteDatabase database = null;
        try {
        	database = getContext().openOrCreateDatabase(guid, Context.MODE_PRIVATE, null);
        } catch(Exception e) {
        	Log.e(TAG, e.getMessage());
        } finally {
        	if ( database != null ) {
        		database.close();
        	}
        }
        String databasePath = getContext().getDatabasePath(guid).getAbsolutePath();

        // Request authorisation from existing client
        Log.i(TAG, String.format("Requesting client auth for client '%s'", clientName));

        auth.requestClientAuth(clientName, password, databasePath);
        String authCode = auth.getAuthCode();

        Log.i(TAG, String.format("Client auth request pending with auth code '%s'", authCode));
        */
      }

      Account account = null;
      try {
        account = settings.createAccount(getActivity(), accountName, propSettings);
      } catch (WeaveException e) {
        Toast.makeText(
                getActivity(),
                "Couldn't create account (account with this name already existing?)",
                Toast.LENGTH_LONG)
            .show();
        return;
      }

      // Register addressbook
      if (serverInfo.getAddressBook().isEnabled()) {
        ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
        ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
      } else {
        ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0);
      }

      // TODO - support multiple calendars

      // Register calendar
      if (serverInfo.getCalendar().isEnabled()) {
        LocalCalendar.create(account, getActivity().getContentResolver(), serverInfo.getCalendar());
        ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1);
        ContentResolver.setSyncAutomatically(account, CalendarContract.AUTHORITY, true);
      } else {
        ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 0);
      }

      getActivity().finish();

    } catch (Exception e) {
      Log.getInstance().error(String.format("Error creating account - %s", e.getMessage()));
      Toast.makeText(getActivity(), "Couldn't create account", Toast.LENGTH_LONG).show();
    }
  }