private ICredentials getAuthorizationInfo(URI link, String realm) throws CredentialsException {
    ISecurePreferences securePreferences = getSecurePreferences();

    /* Check if Bundle is Stopped */
    if (securePreferences == null) return null;

    /* Return from Equinox Security Storage */
    if (securePreferences.nodeExists(SECURE_FEED_NODE)) { // Global Feed Node
      ISecurePreferences allFeedsPreferences = securePreferences.node(SECURE_FEED_NODE);
      if (allFeedsPreferences.nodeExists(
          EncodingUtils.encodeSlashes(link.toString()))) { // Feed Node
        ISecurePreferences feedPreferences =
            allFeedsPreferences.node(EncodingUtils.encodeSlashes(link.toString()));
        if (feedPreferences.nodeExists(
            EncodingUtils.encodeSlashes(realm != null ? realm : REALM))) { // Realm Node
          ISecurePreferences realmPreferences =
              feedPreferences.node(EncodingUtils.encodeSlashes(realm != null ? realm : REALM));

          try {
            String username = realmPreferences.get(USERNAME, null);
            String password = realmPreferences.get(PASSWORD, null);
            String domain = realmPreferences.get(DOMAIN, null);

            if (username != null && password != null)
              return new Credentials(username, password, domain);
          } catch (StorageException e) {
            throw new CredentialsException(
                Activator.getDefault().createErrorStatus(e.getMessage(), e));
          }
        }
      }
    }

    return null;
  }
  /** Tests edge conditions: null or empty arguments */
  @Test
  public void testEdge() {
    assertNull(EncodingUtils.encodeBase64(null));
    assertNull(EncodingUtils.decodeBase64(null));

    String encoded = EncodingUtils.encodeBase64(decoded4);
    assertNotNull(encoded);
    assertEquals(encoded4, encoded);

    byte[] decoded = EncodingUtils.decodeBase64(encoded);
    compareArrays(decoded4, decoded);
  }
  private void internalSetAuthCredentials(
      ICredentials credentials, URI link, String realm, boolean persist)
      throws CredentialsException {

    /* Store Credentials in In-Memory Store */
    if (!persist) {
      fInMemoryStore.put(toCacheKey(link, realm), credentials);
    }

    /* Store Credentials in secure Storage */
    else {
      ISecurePreferences securePreferences = getSecurePreferences();

      /* Check if Bundle is Stopped */
      if (securePreferences == null) return;

      /* Store in Equinox Security Storage */
      ISecurePreferences allFeedsPreferences = securePreferences.node(SECURE_FEED_NODE);
      ISecurePreferences feedPreferences =
          allFeedsPreferences.node(EncodingUtils.encodeSlashes(link.toString()));
      ISecurePreferences realmPreference =
          feedPreferences.node(EncodingUtils.encodeSlashes(realm != null ? realm : REALM));

      IPreferenceScope globalScope = Owl.getPreferenceService().getGlobalScope();

      /* OS Password is only supported on Windows and Mac */
      boolean useOSPassword = globalScope.getBoolean(DefaultPreferences.USE_OS_PASSWORD);
      if (!Platform.OS_WIN32.equals(Platform.getOS())
          && !Platform.OS_MACOSX.equals(Platform.getOS())) useOSPassword = false;

      boolean encryptPW =
          useOSPassword || globalScope.getBoolean(DefaultPreferences.USE_MASTER_PASSWORD);
      try {
        if (credentials.getUsername() != null)
          realmPreference.put(USERNAME, credentials.getUsername(), encryptPW);

        if (credentials.getPassword() != null)
          realmPreference.put(PASSWORD, credentials.getPassword(), encryptPW);

        if (credentials.getDomain() != null)
          realmPreference.put(DOMAIN, credentials.getDomain(), encryptPW);

        realmPreference.flush(); // Flush to disk early
      } catch (StorageException e) {
        throw new CredentialsException(Activator.getDefault().createErrorStatus(e.getMessage(), e));
      } catch (IOException e) {
        throw new CredentialsException(Activator.getDefault().createErrorStatus(e.getMessage(), e));
      }
    }

    /* Uncache */
    removeUnprotected(link, realm);
  }
  /** Tests round trip using large random sequences */
  @Test
  public void testRandom() {
    Random generator = new Random(System.currentTimeMillis());

    for (int i = 0; i < RANDOM_ITERATIONS; i++) {
      // length of array is random in [100, 1000)
      int length = 100 + generator.nextInt(900);
      byte[] bytes = new byte[length];
      generator.nextBytes(bytes);

      // round trip
      String encoded = EncodingUtils.encodeBase64(bytes);
      byte[] decoded = EncodingUtils.decodeBase64(encoded);
      compareArrays(bytes, decoded);
    }
  }
Example #5
0
 private static void saveServers(Collection<SonarServer> servers) {
   IEclipsePreferences rootNode = new InstanceScope().getNode(NODE);
   try {
     Preferences serversNode = rootNode.node(PREF_SERVERS);
     for (SonarServer server : servers) {
       String encodedUrl = EncodingUtils.encodeSlashes(server.getUrl());
       serversNode.node(encodedUrl).putBoolean("auth", server.hasCredentials());
     }
     serversNode.flush();
   } catch (BackingStoreException e) {
     LoggerFactory.getLogger(SecurityManager.class).error(e.getMessage(), e);
   }
 }
  /*
   * @see
   * org.rssowl.core.connection.ICredentialsProvider#deleteAuthCredentials(java
   * .net.URI, java.lang.String)
   */
  public synchronized void deleteAuthCredentials(URI link, String realm)
      throws CredentialsException {

    /* Delete from In-Memory Store if present */
    fInMemoryStore.remove(toCacheKey(link, realm));

    /* Delete from Cache */
    removeUnprotected(link, realm);

    /* Check if Bundle is Stopped */
    ISecurePreferences securePreferences = getSecurePreferences();
    if (securePreferences == null) return;

    /* Remove from Equinox Security Storage */
    if (securePreferences.nodeExists(SECURE_FEED_NODE)) { // Global Feed Node
      ISecurePreferences allFeedsPreferences = securePreferences.node(SECURE_FEED_NODE);
      if (allFeedsPreferences.nodeExists(
          EncodingUtils.encodeSlashes(link.toString()))) { // Feed Node
        ISecurePreferences feedPreferences =
            allFeedsPreferences.node(EncodingUtils.encodeSlashes(link.toString()));
        if (feedPreferences.nodeExists(
            EncodingUtils.encodeSlashes(realm != null ? realm : REALM))) { // Realm Node
          ISecurePreferences realmPreferences =
              feedPreferences.node(EncodingUtils.encodeSlashes(realm != null ? realm : REALM));
          realmPreferences.clear();
          realmPreferences.removeNode();
          try {
            feedPreferences.flush();
          } catch (IOException e) {
            throw new CredentialsException(
                Activator.getDefault().createErrorStatus(e.getMessage(), e));
          }
        }
      }
    }
  }
  /** Tests encoding using hand-calculated examples. */
  @Test
  public void testHandCoded() {
    String encoded = EncodingUtils.encodeBase64(decoded1.getBytes());
    assertEquals(encoded1, encoded);

    byte[] bytes = EncodingUtils.decodeBase64(encoded2);
    String decoded = new String(bytes);
    assertEquals(decoded2, decoded);

    String testZeroes = EncodingUtils.encodeBase64(decoded3);
    assertEquals(encoded3, testZeroes);
    byte[] roundtripBytes = EncodingUtils.decodeBase64(testZeroes);
    compareArrays(decoded3, roundtripBytes);

    byte[] bytesInvalidChars = EncodingUtils.decodeBase64(encoded5);
    String decodedInvalidChars = new String(bytesInvalidChars);
    assertEquals(decoded5, decodedInvalidChars);

    String shortSample = EncodingUtils.encodeBase64(decoded6.getBytes());
    assertEquals(encoded6, shortSample);
    assertEquals(decoded6, new String(EncodingUtils.decodeBase64(shortSample)));
  }
Example #8
0
 private static Collection<SonarServer> loadServers() {
   IEclipsePreferences rootNode = new InstanceScope().getNode(NODE);
   List<SonarServer> servers = Lists.newArrayList();
   try {
     rootNode.sync();
     if (rootNode.nodeExists(PREF_SERVERS)) {
       Preferences serversNode = rootNode.node(PREF_SERVERS);
       for (String encodedUrl : serversNode.childrenNames()) {
         Preferences serverNode = serversNode.node(encodedUrl);
         String url = EncodingUtils.decodeSlashes(encodedUrl);
         boolean auth = serverNode.getBoolean("auth", false);
         servers.add(new SonarServer(url, auth));
       }
     } else {
       // Defaults
       return Arrays.asList(
           new SonarServer("http://localhost:9000"),
           new SonarServer("http://nemo.sonarsource.org"));
     }
   } catch (BackingStoreException e) {
     LoggerFactory.getLogger(SecurityManager.class).error(e.getMessage(), e);
   }
   return servers;
 }