com.intellij.ide.passwordSafe.PasswordSafeException Java Examples

The following examples show how to use com.intellij.ide.passwordSafe.PasswordSafeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MasterKeyPasswordSafe.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Set password to use (used from plugin's UI)
 *
 * @param password the password
 * @return true, if password is a correct one
 */
boolean setMasterPassword(String password) {
  byte[] savedKey = this.key.get();
  this.key.set(EncryptionUtil.genPasswordKey(password));
  String rc;
  try {
    rc = getPassword(null, MasterKeyPasswordSafe.class, testKey(password));
  }
  catch (PasswordSafeException e) {
    throw new IllegalStateException("There should be no problem with password at this point", e);
  }
  if (!TEST_PASSWORD_VALUE.equals(rc)) {
    this.key.set(savedKey);
    return false;
  }
  else {
    return true;
  }

}
 
Example #2
Source File: MasterKeyPasswordSafe.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Reset password for the password safe (clears password database). The method is used from plugin's UI.
 *
 * @param password the password to set
 * @param encrypt  if the password should be encrypted an stored is master database
 */
void resetMasterPassword(String password, boolean encrypt) {
  this.key.set(EncryptionUtil.genPasswordKey(password));
  database.clear();
  try {
    storePassword(null, MasterKeyPasswordSafe.class, testKey(password), TEST_PASSWORD_VALUE);
    if (encrypt) {
      database.setPasswordInfo(encryptPassword(password));
    }
    else {
      database.setPasswordInfo(new byte[0]);
    }
  }
  catch (PasswordSafeException e) {
    throw new IllegalStateException("There should be no problem with password at this point", e);
  }
}
 
Example #3
Source File: MasterKeyPasswordSafeTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterKey() throws PasswordSafeException {
  PasswordDatabase db = new PasswordDatabase();
  MasterKeyPasswordSafe s1 = testProvider(db);
  s1.resetMasterPassword("pass1", false);
  s1.storePassword(null, MasterKeyPasswordSafeTest.class, "TEST", "test");
  assertEquals("test", s1.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
  assertTrue(s1.changeMasterPassword("pass1", "pass2", false));
  assertEquals("test", s1.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
  MasterKeyPasswordSafe s2 = testProvider(db);
  assertFalse(s2.setMasterPassword("pass1"));
  assertTrue(s2.setMasterPassword("pass2"));
  assertEquals("test", s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
  assertTrue(s2.changeMasterPassword("pass2", "pass3", false));
  assertTrue(s2.changeMasterPassword("pass3", "pass4", false));
  assertTrue(s2.setMasterPassword("pass4"));
  assertEquals("test", s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
  s2.resetMasterPassword("fail", false);
  assertNull(s2.getPassword(null, MasterKeyPasswordSafeTest.class, "TEST"));
}
 
Example #4
Source File: PersistentConfig.java    From leetcode-editor with Apache License 2.0 5 votes vote down vote up
public String getPassword() {
    if (getConfig().getVersion() != null) {
        try {
            return PasswordSafe.getInstance().getPassword(null, this.getClass(), "leetcode-editor");
        } catch (PasswordSafeException exception) {
            MessageUtils.showAllWarnMsg("warning", "Password acquisition failed");
            return null;
        }

    }
    return null;

}
 
Example #5
Source File: BasePasswordSafeProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Nullable
public String getPassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  byte[] k = dbKey(project, requester, key);
  byte[] ct = getEncryptedPassword(k);
  return ct == null ? null : EncryptionUtil.decryptText(key(project), ct);
}
 
Example #6
Source File: MasterKeyPasswordSafe.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void removePassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  if (database.isEmpty()) {
    return;
  }
  super.removePassword(project, requester, key);
}
 
Example #7
Source File: MasterKeyPasswordSafe.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String getPassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  if (database.isEmpty()) {
    return null;
  }
  return super.getPassword(project, requester, key);
}
 
Example #8
Source File: PasswordSafeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void storePassword(@Nullable Project project, Class requester, String key, String value) throws PasswordSafeException {
  if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
    getMemoryProvider().storePassword(project, requester, key, value);
  }
  provider().storePassword(project, requester, key, value);
}
 
Example #9
Source File: PasswordSafeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void removePassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
    getMemoryProvider().removePassword(project, requester, key);
  }
  provider().removePassword(project, requester, key);
}
 
Example #10
Source File: PasswordSafeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getPassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  if (mySettings.getProviderType().equals(PasswordSafeSettings.ProviderType.MASTER_PASSWORD)) {
    String password = getMemoryProvider().getPassword(project, requester, key);
    if (password == null) {
      password = provider().getPassword(project, requester, key);
      if (password != null) {
        // cache the password in memory as well for easier access during the session
        getMemoryProvider().storePassword(project, requester, key, password);
      }
    }
    return password;
  }
  return provider().getPassword(project, requester, key);
}
 
Example #11
Source File: DWSettingsProvider.java    From intellij-demandware with MIT License 5 votes vote down vote up
public void setPassword(String password) {
    try {
        PasswordSafe.getInstance().storePassword(null, DWSettingsProvider.class, myState.passwordKey, password != null ? password : "");
    } catch (PasswordSafeException e) {
        LOG.info("Couldn't set password for key " + myState.passwordKey, e);
    }
}
 
Example #12
Source File: DWSettingsProvider.java    From intellij-demandware with MIT License 5 votes vote down vote up
public String getPassword() {
    String password;
    try {
        password = PasswordSafe.getInstance().getPassword(null, DWSettingsProvider.class, myState.passwordKey);
    } catch (PasswordSafeException e) {
        LOG.info("Couldn't get password for key " + myState.passwordKey, e);
        password = "";
    }
    return StringUtil.notNullize(password);
}
 
Example #13
Source File: TeamServicesSecrets.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
/**
 * Old way to read passwords which is deprecated and should only be used in older version of IDEA
 *
 * @param key
 */
private static String readPasswordOldWay(final String key) {
    try {
        return PasswordSafe.getInstance().getPassword(null, TeamServicesSecrets.class, key);
    } catch (final PasswordSafeException e) {
        logger.warn("Failed to read password", e);
    } catch (Throwable t) {
        logger.warn("Failed to read password", t);
    }
    return StringUtils.EMPTY;
}
 
Example #14
Source File: TeamServicesSecrets.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
/**
 * Old way to write passwords which is deprecated and should only be used in older version of IDEA
 *
 * @param key
 * @param value
 */
private static void writePasswordOldWay(final String key, final String value) {
    try {
        PasswordSafe.getInstance().storePassword(null, TeamServicesSecrets.class, key, value);
    } catch (PasswordSafeException e) {
        logger.warn("Failed to write password", e);
    } catch (Throwable t) {
        logger.warn("Failed to write password", t);
    }
}
 
Example #15
Source File: PersistentConfig.java    From leetcode-editor with Apache License 2.0 5 votes vote down vote up
public void savePassword(String password) {
    try {
        PasswordSafe.getInstance().storePassword
                (null, this.getClass(), "leetcode-editor", password != null ? password : "");
    } catch (PasswordSafeException exception) {
        MessageUtils.showAllWarnMsg("warning", "Failed to save password");
    }
}
 
Example #16
Source File: PasswordSafePromptDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Ask password possibly asking password database first. The method could be invoked from any thread. If UI needs to be shown,
 * the method invokes {@link UIUtil#invokeAndWaitIfNeeded(Runnable)}
 * @param project       the context project
 * @param title         the dialog title
 * @param message       the message describing a resource for which password is asked
 * @param requestor     the password requestor
 * @param key           the password key
 * @param resetPassword if true, the old password is removed from database and new password will be asked.
 * @param error         the error text to show in the dialog
 * @param promptLabel   the prompt label text
 * @param checkboxLabel the checkbox text   @return null if dialog was cancelled or password (stored in database or a entered by user)
 */
@Nullable
private static String askPassword(final Project project,
                                  final String title,
                                  final String message,
                                  @Nonnull final Class<?> requestor,
                                  final String key,
                                  boolean resetPassword,
                                  final String error,
                                  final String promptLabel,
                                  final String checkboxLabel) {
  final PasswordSafeImpl ps = (PasswordSafeImpl)PasswordSafe.getInstance();
  try {
    if (resetPassword) {
      ps.removePassword(project, requestor, key);
    }
    else {
      String pw = ps.getPassword(project, requestor, key);
      if (pw != null) {
        return pw;
      }
    }
  }
  catch (PasswordSafeException ex) {
    // ignore exception on get/reset phase
    if (LOG.isDebugEnabled()) {
      LOG.debug("Failed to retrieve or reset password", ex);
    }
  }
  final Ref<String> ref = Ref.create();
  ApplicationManager.getApplication().invokeAndWait(new Runnable() {
    public void run() {
      PasswordSafeSettings.ProviderType type = ps.getSettings().getProviderType();
      final PasswordPromptComponent component = new PasswordPromptComponent(type, message, false, promptLabel, checkboxLabel);
      PasswordSafePromptDialog d = new PasswordSafePromptDialog(project, title, component);

      d.setErrorText(error);
      if (d.showAndGet()) {
        ref.set(new String(component.getPassword()));
        try {
          if (component.isRememberSelected()) {
            ps.storePassword(project, requestor, key, ref.get());
          }
          else if (!type.equals(PasswordSafeSettings.ProviderType.DO_NOT_STORE)) {
            ps.getMemoryProvider().storePassword(project, requestor, key, ref.get());
          }
        }
        catch (PasswordSafeException e) {
          Messages.showErrorDialog(project, e.getMessage(), "Failed to Store Password");
          if (LOG.isDebugEnabled()) {
            LOG.debug("Failed to store password", e);
          }
        }
      }
    }
  }, ModalityState.any());
  return ref.get();
}
 
Example #17
Source File: MasterPasswordDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Ask password from user and set it to password safe instance
 *
 * @param project the current project
 * @param safe    the password safe
 * @throws PasswordSafeException if the master password is not provided.
 */
public static void askPassword(@Nullable Project project, MasterKeyPasswordSafe safe) throws PasswordSafeException {
  // trying empty password: people who have set up empty password, don't want to get disturbed by the prompt.
  if (safe.setMasterPassword("")) {
    return;
  }

  String error = null;
  retries:
  for (int count = 0; count < NUMBER_OF_RETRIES; count++) {
    MasterPasswordDialog d = new MasterPasswordDialog(project, safe);
    if (error != null) {
      d.setErrorText(error);
    }
    d.show();
    switch (d.getExitCode()) {
      case OK_EXIT_CODE:
        boolean rc;
        String pw = new String(d.myMasterPasswordPasswordField.getPassword());
        if (d.myEncryptMasterPasswordWithCheckBox.isSelected()) {
          rc = safe.changeMasterPassword(pw, pw, true);
        }
        else {
          rc = safe.setMasterPassword(pw);
        }
        if (rc) {
          return;
        }
        else {
          error = "Invalid master password, please retry or reset.";
          continue retries;
        }
      case CANCEL_EXIT_CODE:
        throw new MasterPasswordUnavailableException("The master password request were cancelled.");
      case CHANGE_USER_CODE:
        if (!ChangeMasterKeyDialog.changePassword(project, safe)) {
          throw new MasterPasswordUnavailableException("The master password request were cancelled.");
        }
        return;
      case RESET_USER_CODE:
        if (!ResetPasswordDialog.resetPassword(project, safe)) {
          throw new MasterPasswordUnavailableException("The master password request were cancelled.");
        }
        return;
    }
  }
}
 
Example #18
Source File: BasePasswordSafeProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void removePassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  byte[] k = dbKey(project, requester, key);
  removeEncryptedPassword(k);
}
 
Example #19
Source File: BasePasswordSafeProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void storePassword(@Nullable Project project, Class requester, String key, String value) throws PasswordSafeException {
  byte[] k = dbKey(project, requester, key);
  byte[] ct = EncryptionUtil.encryptText(key(project), value);
  storeEncryptedPassword(k, ct);
}
 
Example #20
Source File: NilProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String getPassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  // nothing is stored
  return null;
}
 
Example #21
Source File: NilProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void removePassword(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  // do nothing
}
 
Example #22
Source File: NilProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void storePassword(@Nullable Project project, Class requester, String key, String value) throws PasswordSafeException {
  // just forget about password
}
 
Example #23
Source File: BasePasswordSafeProvider.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Get secret key for the provider
 *
 * @param project the project to use
 * @return the secret key to use
 */
protected abstract byte[] key(@Nullable Project project) throws PasswordSafeException;
 
Example #24
Source File: BasePasswordSafeProvider.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Get database key
 *
 * @param project
 * @param requester the requester class
 * @param key       the key to use
 * @return the key to use for map
 */
private byte[] dbKey(@Nullable Project project, Class requester, String key) throws PasswordSafeException {
  return EncryptionUtil.dbKey(key(project), requester, key);
}