com.intellij.ide.passwordSafe.PasswordSafe Java Examples

The following examples show how to use com.intellij.ide.passwordSafe.PasswordSafe. 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: IdeaLightweightExtension.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
private void initializeApplication(Application application) {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    when(application.getPicoContainer()).thenReturn(pico);

    MessageBus bus = new SingleThreadedMessageBus(null);
    when(application.getMessageBus()).thenReturn(bus);

    // Service setup.  See ServiceManager
    pico.registerComponent(service(PasswordSafe.class, new MockPasswordSafe()));
    pico.registerComponent(service(VcsContextFactory.class, new MockVcsContextFactory()));

    VirtualFileManager vfm = mock(VirtualFileManager.class);
    when(application.getComponent(VirtualFileManager.class)).thenReturn(vfm);

    AccessToken readToken = mock(AccessToken.class);
    when(application.acquireReadActionLock()).thenReturn(readToken);

    ApplicationInfo appInfo = mock(ApplicationInfo.class);
    when(appInfo.getApiVersion()).thenReturn("IC-182.1.1");
    registerApplicationService(ApplicationInfo.class, appInfo);
}
 
Example #2
Source File: ApplicationPasswordRegistry.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve the stored password.  If it was not stored, then the promise's
 * resolved value will be null.
 *
 * @param config source
 * @return promise with a null value (if no password stored) or the password.
 */
@NotNull
public final Promise<OneTimeString> get(@NotNull final ServerConfig config) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetching password for config " + config.getServerName());
    }
    final CredentialAttributes attr = getCredentialAttributes(config, true);
    return PasswordSafe.getInstance().getAsync(attr)
            .then((c) -> c == null ? null : c.getPassword())
            // should use onProcessed, but that's a higher API version.
            .processed((p) -> {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Fetched password for config " + config.getServerName());
                }
            })
            .rejected((t) -> LOG.warn("Password fetch generated an error", t));
}
 
Example #3
Source File: AuthDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean decideOnShowRememberPasswordOption(@javax.annotation.Nullable String password, boolean rememberByDefault) {
  final PasswordSafeImpl passwordSafe = (PasswordSafeImpl)PasswordSafe.getInstance();
  // if password saving is disabled, don't show the checkbox.
  if (passwordSafe.getSettings().getProviderType().equals(PasswordSafeSettings.ProviderType.DO_NOT_STORE)) {
    return false;
  }
  // if password is prefilled, it is expected to continue remembering it.
  if (!StringUtil.isEmptyOrSpaces(password)) {
    return true;
  }
  return rememberByDefault;
}
 
Example #4
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 #5
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 #6
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 #7
Source File: ApplicationPasswordRegistry.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the password associated with the configuration.
 *
 * @param config server configuration for the password.
 */
public final void remove(@NotNull ServerConfig config) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Removing password for config " + config.getServerName());
    }
    CredentialAttributes attr = getCredentialAttributes(config, false);
    PasswordSafe.getInstance().set(attr, new Credentials(config.getUsername(), (String) null));
}
 
Example #8
Source File: ApplicationPasswordRegistry.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the given password in the registry, associated with the config.  After being
 * called, the password will be blanked out.
 */
public final void store(@NotNull ServerConfig config, @NotNull char[] password, boolean inMemoryOnly) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Storing password for config " + config.getServerName());
    }
    final CredentialAttributes attr = getCredentialAttributes(config, inMemoryOnly);
    PasswordSafe.getInstance().set(attr, new Credentials(config.getUsername(), password));
    Arrays.fill(password, (char) 0);
}
 
Example #9
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 #10
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 #11
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 #12
Source File: ReviewDataProvider.java    From review-board-idea-plugin with Apache License 2.0 4 votes vote down vote up
private static String getPassword(String username) {
    CredentialAttributes attributes = new CredentialAttributes(REVIEWBOARD_PASSWORD, username,
            ReviewDataProvider.class, false);
    return PasswordSafe.getInstance().getPassword(attributes);
}
 
Example #13
Source File: ReviewDataProvider.java    From review-board-idea-plugin with Apache License 2.0 4 votes vote down vote up
private static void savePassword(String username, String password) {
    Credentials saveCredentials = new Credentials(username, password);
    PasswordSafe.getInstance().set(new CredentialAttributes(REVIEWBOARD_PASSWORD, username,
            ReviewDataProvider.class, false), saveCredentials);
}
 
Example #14
Source File: CrucibleSettings.java    From Crucible4IDEA with MIT License 4 votes vote down vote up
public void savePassword(String pass) {
  PasswordSafe.getInstance().set(new CredentialAttributes(CRUCIBLE_SETTINGS_PASSWORD_KEY), new Credentials(USERNAME, pass));
}
 
Example #15
Source File: CrucibleSettings.java    From Crucible4IDEA with MIT License 4 votes vote down vote up
@Nullable
public String getPassword() {
  final Credentials credentials = PasswordSafe.getInstance().get(new CredentialAttributes(CRUCIBLE_SETTINGS_PASSWORD_KEY));

  return credentials != null ? credentials.getPasswordAsString() : null;
}
 
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: PasswordSafeConfigurable.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * The constructor
 *
 * @param settings the password safe settings
 */
@Inject
public PasswordSafeConfigurable(@Nonnull PasswordSafeSettings settings, @Nonnull PasswordSafe passwordSafe) {
  mySettings = settings;
  myPasswordSafe = passwordSafe;
}