Java源码示例:org.netbeans.api.keyring.Keyring

示例1
static void savePassword(@NonNull final FileObject fo, @NullAllowed final String password,
        @NullAllowed final String displayName) {
    
    if (password == null) {
        return;
    }
    String url = (String) fo.getAttribute(InstanceProperties.URL_ATTR);
    if (url == null) {
        return;
    }
    Keyring.save(getPasswordKey(url), password.toCharArray(), displayName);
    try {
        fo.setAttribute(InstanceProperties.PASSWORD_ATTR, null);
    } catch (IOException ex) {
        LOGGER.log(Level.INFO, null, ex);
    }
}
 
示例2
public void testPasswordInKeyring() throws Exception {
    assertNotNull(InstanceProperties.getInstanceProperties("fooservice"));

    // the instance from test layer
    assertEquals("Adminpasswd", new String(Keyring.read("j2eeserver:fooservice")));

    // new instance
    String url = TEST_URL_PREFIX + "passwordInKeyring";
    InstanceProperties.createInstanceProperties(
            url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME);
    assertEquals(TEST_PASSWORD, new String(Keyring.read("j2eeserver:" + url)));

    // all password attributes are converted to keyring
    FileObject fo = FileUtil.getConfigFile("J2EE/InstalledServers");
    for (FileObject child : fo.getChildren()) {
        assertNull(child.getAttribute(InstanceProperties.PASSWORD_ATTR));
    }
}
 
示例3
public synchronized String getAdminPassword() {
    // read old settings
    String pwd = NbPreferences.forModule(MySQLOptions.class).get("adminpwd", null); // NOI18N
    // don't store a password anymore
    NbPreferences.forModule(MySQLOptions.class).remove("adminpwd"); // NOI18N
    if (pwd != null) {
        // store using Keyring API
        Keyring.save(MySQLOptions.class.getName(), pwd.toCharArray(), NbBundle.getMessage(MySQLOptions.class, "MySQLOptions_AdminPassword")); // NOI18N
    }
    if ( isSavePassword() ) {
        LOGGER.log(Level.FINE, "Reading a Admin Password from Keyring.");
        char[] chars = Keyring.read(MySQLOptions.class.getName());
        adminPassword = chars == null ? "" : String.copyValueOf(chars);
    }
    return adminPassword;
}
 
示例4
public synchronized void setAdminPassword(String adminPassword) {
    // 'null' is a valid password, but if we save as null
    // it will actually clear the property.  So convert it to
    // an empty string.
    if ( adminPassword == null ) {
        adminPassword = "";
    }
    
    // Cache password for this session whether we save it or not.
    this.adminPassword = adminPassword;
    
    if ( isSavePassword() ) {
        LOGGER.log(Level.FINE, "Storing a Admin Password to Keyring.");
        Keyring.save(MySQLOptions.class.getName(), adminPassword.toCharArray(), NbBundle.getMessage(MySQLOptions.class, "MySQLOptions_AdminPassword")); // NOI18N
    } else {
        LOGGER.log(Level.FINE, "Removing a Admin Password from Keyring.");
        Keyring.delete(MySQLOptions.class.getName());
    }
}
 
示例5
public void setSavePassword(boolean savePassword) {
    putProperty(PROP_SAVEPWD, savePassword);
    
    // Clear the password from the persistent file if saving
    // passwords is turned off; save the password to the persistent
    // file if saving passwords is turned on
    if (adminPassword == null) {
        // nothing for save
        return ;
    }
    if ( ! savePassword ) {
        clearAdminPassword();
    } else {
        LOGGER.log(Level.FINE, "Storing a Admin Password to Keyring.");
        Keyring.save(MySQLOptions.class.getName(), adminPassword.toCharArray(), NbBundle.getMessage(MySQLOptions.class, "MySQLOptions_AdminPassword")); // NOI18N
    }
}
 
示例6
/**
 * Saves password for a key constructed from keyPrefix and key
 * @param keyPrefix key prefix for each versioning system
 * @param key will be hashed and used with keyPrefix as a key for the keyring
 * @param password password, value will be nulled. If <code>null</code> the old record will be permanently deleted
 * @param description can be null
 */
public static void save (String keyPrefix, String key, char[] password, String description) {
    String hashedKey = getKeyringKeyHashed(keyPrefix, key);
    if (password == null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Deleting password for {0}:{1}", new String[] {keyPrefix, key}); //NOI18N
        }
        Keyring.delete(getKeyringKey(keyPrefix, key));
        Keyring.delete(hashedKey);
        NULL_HASHED_RECORDS.add(hashedKey);
    } else {
        if (description == null) {
            description = key;
        }
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Saving password for {0}:{1}", new String[] {keyPrefix, key}); //NOI18N
            if (PRINT_PASSWORDS) {
                LOG.log(Level.FINEST, "Saving password: \"{0}\"", new String(password)); //NOI18N
            }
        }
        Keyring.save(getKeyringKey(keyPrefix, key), password, description);
        Keyring.delete(hashedKey);
        NULL_HASHED_RECORDS.remove(hashedKey);
    }
}
 
示例7
private ConnectivitySettings proxyToCs(Proxy proxy, URI uri) {
    ConnectivitySettings cs = new ConnectivitySettings();
    InetSocketAddress isa = (InetSocketAddress) proxy.address();
    switch (proxy.type()) {
    case HTTP:
        setupProxy(cs, ConnectivitySettings.CONNECTION_VIA_HTTPS, isa);
        break;
    case SOCKS:
        setupProxy(cs, ConnectivitySettings.CONNECTION_VIA_SOCKS, isa);
        break;
    default:
    }
    
    String prosyUser = NetworkSettings.getAuthenticationUsername(uri);
    if (prosyUser != null && !prosyUser.isEmpty()) {
        cs.setProxyUsername(prosyUser);
        cs.setProxyPassword(Keyring.read(NetworkSettings.getKeyForAuthenticationPassword(uri)));
    }
    return cs;
}
 
示例8
private void restorePassword() {
    if (this.connectionFileName == null) {
        LOGGER.log(Level.FINE, "No connectionFileName for {0}", this);
        pwd = "";
        rpwd = false;
        return ;
    }
    final String key = this.connectionFileName;
    // If the password was saved, then it means the user checked
    // the box to say the password should be remembered.
    char[] chars = Keyring.read(key);
    if (chars != null) {
        LOGGER.log(Level.FINE, "A password read for {0}", key);
        pwd = String.valueOf(chars);
        rpwd = true;
    } else {
        LOGGER.log(Level.FINE, "No password read for {0}", key);
        pwd = "";
        rpwd = false;
    }
}
 
示例9
private void loadProjectOptions() {
    char[] keystorePasswd = Keyring.read(hash + KEY_STORE_PASSWORD);
    char[] keyPasswd = Keyring.read(hash + KEY_PASSWORD);
    if (keystorePasswd != null) {
        keystorePassword = new String(keystorePasswd);
    }
    if (keyPasswd != null) {
        keyPassword = new String(keyPasswd);
    }
    keystorePath = auxProps.get(KEY_STORE_PATH, false);
    keyAlias = auxProps.get(KEY_ALIAS, false);
    useGlobal = getBooleanValue(USE_GLOBAL, true);
    ask = getBooleanValue(ASK, true);
    apkV1 = getBooleanValue(APK_V1, true);
    apkV2 = getBooleanValue(APK_V2, true);
    apkRelease = getBooleanValue(APK_RELEASE, true);
    apkDebug = getBooleanValue(APK_DEBUG, false);
    rememberPassword = getBooleanValue(REMEMBER_PASSWORDS, true);
}
 
示例10
public void storeSettings() {
    if (rememberPasswd.isSelected()) {
        Keyring.save(hash + KEY_STORE_PASSWORD, keystorePassword.getPassword(), "NBANDROID Project Keystore Password");
        Keyring.save(hash + KEY_PASSWORD, keyPassword.getPassword(), "NBANDROID Project Keystore Key Password");
    } else {
        Keyring.delete(hash + KEY_STORE_PASSWORD);
        Keyring.delete(hash + KEY_PASSWORD);
    }
    NbPreferences.forModule(KeystoreSelector.class).put(hash + KEY_STORE_PATH, path.getText());
    NbPreferences.forModule(KeystoreSelector.class).put(hash + KEY_ALIAS, alias.getText());
    NbPreferences.forModule(KeystoreSelector.class).putBoolean(hash + APK_V1, v1.isSelected());
    NbPreferences.forModule(KeystoreSelector.class).putBoolean(hash + APK_V2, v2.isSelected());
    NbPreferences.forModule(KeystoreSelector.class).putBoolean(hash + APK_RELEASE, release.isSelected());
    NbPreferences.forModule(KeystoreSelector.class).putBoolean(hash + APK_DEBUG, debug.isSelected());
    NbPreferences.forModule(KeystoreSelector.class).putBoolean(hash + REMEMBER_PASSWORDS, rememberPasswd.isSelected());
}
 
示例11
@NonNull
@Override
Authentification create(@NonNull final Map<String,String> props) {
    String user = props.get(PLAT_PROP_AUTH_USER);
    if (user == null) {
        throw new IllegalStateException("No user"); //NOI18N
    }
    char[] passwd = Keyring.read(RemotePlatformProvider.createPropertyName(
        props.get(RemotePlatform.PLAT_PROP_ANT_NAME),
        Password.PLAT_PROP_AUTH_PASSWD));
    final String passwdStr;
    if (passwd == null) {
        LOG.log(
            Level.WARNING,
            "No password for: {0} for platform: {1}",   //NOI18N
            new Object[]{
                user,
                props.get(RemotePlatform.PLAT_PROP_ANT_NAME)
            });
        passwdStr = ""; //NOI18N
    } else {
        passwdStr = String.valueOf(passwd);
    }
    return new Password(
        user,
        passwdStr);
}
 
示例12
@NonNull
@Override
Authentification create(@NonNull final Map<String,String> props) {
    String user = props.get(PLAT_PROP_AUTH_USER);
    if (user == null) {
        throw new IllegalStateException("No user"); //NOI18N
    }
    final String keyStore = props.get(Key.PLAT_PROP_AUTH_KEYSTORE);
    if (keyStore == null) {
        throw new IllegalStateException("No key store");    //NOI18N
    }
    final char[] passPhrase = Keyring.read(RemotePlatformProvider.createPropertyName(
        props.get(RemotePlatform.PLAT_PROP_ANT_NAME),
        Key.PLAT_PROP_AUTH_PASSPHRASE));
    final String passPhraseStr;
    if (passPhrase == null) {
        LOG.log(
            Level.WARNING,
            "No passprase for: {0} for platform: {1}",   //NOI18N
            new Object[]{
                user,
                props.get(RemotePlatform.PLAT_PROP_ANT_NAME)
            });
        passPhraseStr = "";
    } else {
        passPhraseStr = String.valueOf(passPhrase);
    }
    return new Key(
        user,
        new File(keyStore),
        passPhraseStr);
}
 
示例13
@NonNull
@Override
void store(@NonNull final Map<String,String> props) {
    super.store(props);
    Keyring.save(
        RemotePlatformProvider.createPropertyName(
            props.get(RemotePlatform.PLAT_PROP_ANT_NAME),
            PLAT_PROP_AUTH_PASSWD),
        getPassword().toCharArray(),
        null);
}
 
示例14
@Override
void store(@NonNull final Map<String,String> props) {
    super.store(props);
    props.put(PLAT_PROP_AUTH_KEYSTORE, getKeyStore().getAbsolutePath());
    Keyring.save(
        RemotePlatformProvider.createPropertyName(
            props.get(RemotePlatform.PLAT_PROP_ANT_NAME),
            PLAT_PROP_AUTH_PASSPHRASE),
        getPassPhrase().toCharArray(),
        null);
}
 
示例15
public static char[] getAuthenticationPassword () {
    String old = getPreferences().get(PROXY_AUTHENTICATION_PASSWORD, null);
    if (old != null) {
        getPreferences().remove(PROXY_AUTHENTICATION_PASSWORD);
        setAuthenticationPassword(old.toCharArray());
    }
    char[] pwd = Keyring.read(PROXY_AUTHENTICATION_PASSWORD);
    return pwd != null ? pwd : new char[0];
}
 
示例16
/** Creates new form AuthenticatorPanel */
public NbAuthenticatorPanel(String realmName) {
    this.realmName = realmName;
    initComponents();
    prefs = NbPreferences.forModule(NbAuthenticatorPanel.class).node("authentication"); // NOI18N
    keyringKey = "authentication." + realmName; // NOI18N
    String username = prefs.get(realmName, null);
    if (username != null) {
        userName.setText(username);
        char[] pwd = Keyring.read(keyringKey);
        if (pwd != null) {
            password.setText(new String(pwd));
        }
    }
}
 
示例17
public @Override boolean enabled() {
    if (Boolean.getBoolean("netbeans.keyring.no.master")) {
        LOG.fine("master password encryption disabled");
        return false;
    }
    if (GraphicsEnvironment.isHeadless()) {
        LOG.fine("disabling master password encryption in headless mode");
        return false;
    }
    try {
        KEY_FACTORY = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        encrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        decrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        Preferences prefs = NbPreferences.forModule(Keyring.class);
        Utils.goMinusR(prefs);
        String saltKey = "salt"; // NOI18N
        byte[] salt = prefs.getByteArray(saltKey, null);
        if (salt == null) {
            salt = new byte[36];
            new SecureRandom().nextBytes(salt);
            prefs.putByteArray(saltKey, salt);
        }
        PARAM_SPEC = new PBEParameterSpec(salt, 20);
        LOG.warning("Falling back to master password encryption; " +
                "add -J-Dorg.netbeans.modules.keyring.level=0 to netbeans.conf to see why native keyrings could not be loaded");
        return true;
    } catch (Exception x) {
        LOG.log(Level.INFO, "Cannot initialize security using " + ENCRYPTION_ALGORITHM, x);
        return false;
    }
}
 
示例18
@Override
public String put(String key, String value) {
    if (PayaraModule.PASSWORD_ATTR.equals(key)) {
        String serverName = get(PayaraModule.DISPLAY_NAME_ATTR);
        String userName = get(PayaraModule.USERNAME_ATTR);
        if (serverName != null && userName != null) {
            Keyring.save(PayaraInstance.passwordKey(
                    serverName, userName), value.toCharArray(),
                    "Payara administrator user password");
        }
    }
    synchronized(delegate) {return delegate.put(key, value);}
}
 
示例19
/**
 * Retrieve password stored in {@see Keyring}.
 * <p/>
 * @param serverName Name of server to add into password key.
 * @param userName User name of account user who's password will be read.
 * @return 
 */
public static String getPasswordFromKeyring(final String serverName,
        final String userName) {
    char[] passwordChars = Keyring.read(PayaraInstance.passwordKey(serverName, userName));
    String value = passwordChars != null
            ? new String(passwordChars)
            : DEFAULT_ADMIN_PASSWORD;
    return value;
}
 
示例20
@Override
public String put(String key, String value) {
    if (GlassfishModule.PASSWORD_ATTR.equals(key)) {
        String serverName = get(GlassfishModule.DISPLAY_NAME_ATTR);
        String userName = get(GlassfishModule.USERNAME_ATTR);
        if (serverName != null && userName != null) {
            Keyring.save(GlassfishInstance.passwordKey(
                    serverName, userName), value.toCharArray(),
                    "GlassFish administrator user password");
        }
    }
    synchronized(delegate) {return delegate.put(key, value);}
}
 
示例21
/**
 * Retrieve password stored in {@see Keyring}.
 * <p/>
 * @param serverName Name of server to add into password key.
 * @param userName User name of account user who's password will be read.
 * @return 
 */
public static String getPasswordFromKeyring(final String serverName,
        final String userName) {
    char[] passwordChars = Keyring.read(
            GlassfishInstance.passwordKey(serverName, userName));
    String value = passwordChars != null
            ? new String(passwordChars)
            : DEFAULT_ADMIN_PASSWORD;
    return value;
}
 
示例22
private synchronized void clearInstanceStorage(final String url) {
    FileObject instanceFO = getInstanceFileObject(url);
    if (instanceFO != null) {
        try {
            instanceFO.delete();
        } catch (IOException ioe) {
            LOGGER.log(Level.INFO, null, ioe);
        }
    }

    Keyring.delete(getPasswordKey(url));
}
 
示例23
@CheckForNull
static String readPassword(@NonNull final String url) {
    char[] passwordChars = Keyring.read(getPasswordKey(url));
    if (passwordChars != null) {
        String password = String.valueOf(passwordChars);
        Arrays.fill(passwordChars, ' ');
        return password;
    }
    return null;
}
 
示例24
static void savePassword(@NonNull final String url, @NullAllowed final String password,
        @NullAllowed final String displayName) {

    if (password == null) {
        return;
    }
    Keyring.save(getPasswordKey(url), password.toCharArray(), displayName);
}
 
示例25
public void testKeyringCleanup() throws Exception {
    String url = TEST_URL_PREFIX + "keyringCleanup";
    InstanceProperties.createInstanceProperties(
            url, TEST_USERNAME, TEST_PASSWORD, TEST_DISPLAY_NAME);
    assertEquals(TEST_PASSWORD, new String(Keyring.read("j2eeserver:" + url)));

    ServerRegistry.getInstance().removeServerInstance(url);
    assertNull(Keyring.read("j2eeserver:" + url));
}
 
示例26
private void store(AmazonInstance ai) {
    InstanceProperties props = InstancePropertiesManager.getInstance().createProperties(AMAZON_IP_NAMESPACE);
    
    Keyring.save(PREFIX+KEY_ID+"."+ai.getName(), ai.getKeyId().toCharArray(), "Amazon Access Key ID"); // NOI18N
    Keyring.save(PREFIX+KEY+"."+ai.getName(), ai.getKey().toCharArray(), "Amazon Secret Access Key"); // NOI18N
    
    props.putString("name", ai.getName()); // NOI18N
    props.putString("region", ai.getRegionURL()); // NOI18N
    props.putString("code", ai.getRegionCode()); // NOI18N
}
 
示例27
private String readPasswordFromKeyring() {
    // new password key
    char[] newPassword = Keyring.read(passwordKey);
    if (newPassword != null) {
        return new String(newPassword);
    }
    // deprecated password key
    newPassword = Keyring.read(deprecatedPasswordKey);
    if (newPassword != null) {
        return new String(newPassword);
    }
    return null;
}
 
示例28
protected void savePassword(final String password, final String type) {
    if (password == null) {
        // password not set at all, do nothing
        //  (e.g. when user opens Remote Configurations and clicks OK but not all configs were displayed [= passwords were not fetched from keyring])
        return;
    }
    if (StringUtils.hasText(password)) {
        Keyring.save(passwordKey, password.toCharArray(),
                NbBundle.getMessage(RemoteConfiguration.class, "MSG_PasswordFor", getDisplayName(), type));
        // remove old password key
        Keyring.delete(deprecatedPasswordKey);
    } else {
        deletePassword();
    }
}
 
示例29
public String getPassword() {
    if (passwordKey == null) {
        return null;
    }
    char[] chars = Keyring.read(passwordKey);
    if (chars == null) {
        return null;
    }
    return new String(chars);
}
 
示例30
public static synchronized void removeFromTrustStore(String url) throws GeneralSecurityException, IOException {
    FileObject root = FileUtil.getConfigRoot();
    FileObject ts = root.getFileObject(TRUST_STORE_PATH);
    if (ts == null) {
        return;
    }

    char[] password = Keyring.read(TRUST_PASSWORD_KEY);

    KeyStore keystore = KeyStore.getInstance("JKS"); // NOI18N
    InputStream is = new BufferedInputStream(ts.getInputStream());
    try {
        keystore.load(is, password);
    } catch (IOException ex) {
        LOGGER.log(Level.INFO, null, ex);
        return;
    } finally {
        is.close();
    }

    keystore.deleteEntry(url);

    OutputStream out = new BufferedOutputStream(ts.getOutputStream());
    try {
        keystore.store(out, password);
    } finally {
        out.close();
    }
}