Search in sources :

Example 1 with FailToStoreCredentialException

use of net.viperfish.journal.framework.errors.FailToStoreCredentialException in project vsDiaryWriter by shilongdai.

the class ChangePasswordOperation method execute.

@Override
public void execute() {
    List<Journal> buffer = db().getAll();
    // set the new password
    try {
        auth().setPassword(pass);
        // clear all
        indexer().clear();
        db().clear();
        // re-encrypt all entries
        for (Journal i : buffer) {
            i.setId(null);
            Journal added = db().addEntry(i);
            indexer().add(added);
        }
    } catch (FailToStoreCredentialException e) {
        File userHome = new File(System.getProperty("user.home"));
        File export = new File(userHome, "export.txt");
        OperationErrorException err = new OperationErrorException("Failed to save the new password:" + e.getMessage() + " Exporting all entries to " + export.getAbsolutePath());
        new ExportJournalOperation(export.getAbsolutePath()).execute();
        throw err;
    } catch (FailToSyncEntryException e) {
        File userHome = new File(System.getProperty("user.home"));
        File export = new File(userHome, "export.txt");
        OperationErrorException err = new OperationErrorException("Failed to re-add entries with the new password:" + e.getMessage() + " Exporting all entries to " + export.getAbsolutePath());
        new ExportJournalOperation(export.getAbsolutePath()).execute();
        throw err;
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) Journal(net.viperfish.journal.framework.Journal) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) File(java.io.File)

Example 2 with FailToStoreCredentialException

use of net.viperfish.journal.framework.errors.FailToStoreCredentialException in project vsDiaryWriter by shilongdai.

the class OpenBSDBCryptAuthManager method setPassword.

/**
 * sets a password
 *
 * This method sets the password of the user in this
 * {@link AuthenticationManager}. The password is hashed with BCrypt using
 * the format in OpenBSD with a cost factor of 16. The final output will be
 * written to the password file as an ASCII string. This method will ready
 * this authenticator for {@link OpenBSDBCryptAuthManager#verify(String)}
 * and {@link OpenBSDBCryptAuthManager#getPassword()}.
 *
 * @param pass
 *            the password to set
 *
 * @throws FailToStoreCredentialException
 *             if failed to write the hashed password to the password file
 */
@Override
public synchronized void setPassword(String pass) throws FailToStoreCredentialException {
    byte[] salt = new byte[16];
    rand.nextBytes(salt);
    current = OpenBSDBCrypt.generate(pass.toCharArray(), salt, 16);
    try {
        passwdFile.write(current, StandardCharsets.US_ASCII);
    } catch (IOException e) {
        FailToStoreCredentialException fc = new FailToStoreCredentialException("Cannot store the hashed password:" + e.getMessage());
        fc.initCause(e);
        current = null;
        throw fc;
    }
    this.password = pass;
}
Also used : FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) IOException(java.io.IOException)

Example 3 with FailToStoreCredentialException

use of net.viperfish.journal.framework.errors.FailToStoreCredentialException in project vsDiaryWriter by shilongdai.

the class SCryptAuthManager method setPassword.

/**
 * sets the password
 *
 * This method sets the password of the user. The password is hashed with
 * SCrypt. The hashed password will be stored with the salt in the format of
 * the {@link PasswordFile} in the password file. A successful invocation of
 * the method prepares the authenticator for
 * {@link SCryptAuthManager#verify(String)} and
 * {@link SCryptAuthManager#getPassword()}.
 *
 * @param pass
 *            the password to set
 *
 * @throws FailToStoreCredentialException
 *             if cannot write the generated hash to password file
 */
@Override
public synchronized void setPassword(String pass) {
    byte[] salt = new byte[8];
    rand.nextBytes(salt);
    byte[] crypt = SCrypt.generate(pass.getBytes(StandardCharsets.UTF_16), salt, 262144, 10, 3, 256);
    passCont = new PasswordFile(crypt, salt);
    try {
        passwdFile.write(passCont.toString(), StandardCharsets.US_ASCII);
    } catch (IOException e) {
        FailToStoreCredentialException fs = new FailToStoreCredentialException("Cannot store password to passwd file:" + e.getMessage());
        fs.initCause(e);
        passCont = null;
        throw new RuntimeException(fs);
    }
    this.password = pass;
}
Also used : FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) IOException(java.io.IOException)

Aggregations

FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)3 IOException (java.io.IOException)2 File (java.io.File)1 Journal (net.viperfish.journal.framework.Journal)1 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)1 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)1