use of password.pwm.error.ErrorInformation in project pwm by pwm-project.
the class LocalDbOtpOperator method writeOtpUserConfiguration.
@Override
public void writeOtpUserConfiguration(final PwmSession pwmSession, final UserIdentity theUser, final String userGUID, final OTPUserRecord otpConfig) throws PwmUnrecoverableException {
LOGGER.trace(pwmSession, String.format("Enter: writeOtpUserConfiguration(%s, %s, %s)", theUser, userGUID, otpConfig));
if (userGUID == null || userGUID.length() < 1) {
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_GUID, "cannot save otp to localDB, user does not have a pwmGUID"));
}
if (localDB == null || localDB.status() != LocalDB.Status.OPEN) {
final String errorMsg = "LocalDB is not available, unable to write user otp";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, errorMsg);
throw new PwmUnrecoverableException(errorInformation);
}
try {
final Configuration config = this.getPwmApplication().getConfig();
String value = composeOtpAttribute(otpConfig);
if (config.readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
LOGGER.debug(pwmSession, "Encrypting OTP secret for storage");
value = encryptAttributeValue(value);
}
localDB.put(LocalDB.DB.OTP_SECRET, userGUID, value);
LOGGER.info(pwmSession, "saved OTP secret for user in LocalDB");
} catch (LocalDBException ex) {
final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, "unexpected LocalDB error saving otp to localDB: " + ex.getMessage());
final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
pwmOE.initCause(ex);
throw pwmOE;
} catch (PwmOperationalException ex) {
final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, "unexpected error saving otp to localDB: " + ex.getMessage());
final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
pwmOE.initCause(ex);
throw pwmOE;
}
}
use of password.pwm.error.ErrorInformation in project pwm by pwm-project.
the class AbstractJDBCLocalDB method size.
public int size(final LocalDB.DB db) throws LocalDBException {
preCheck(false);
final StringBuilder sb = new StringBuilder();
sb.append("SELECT COUNT(" + KEY_COLUMN + ") FROM ").append(db.toString());
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
lock.readLock().lock();
try {
statement = dbConnection.prepareStatement(sb.toString());
resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1);
}
} catch (final SQLException ex) {
throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
} finally {
close(statement);
close(resultSet);
}
} finally {
lock.readLock().unlock();
}
return 0;
}
use of password.pwm.error.ErrorInformation in project pwm by pwm-project.
the class AbstractJDBCLocalDB method iterator.
public LocalDB.LocalDBIterator<String> iterator(final LocalDB.DB db) throws LocalDBException {
try {
if (dbIterators.size() > ITERATOR_LIMIT) {
throw new LocalDBException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "over " + ITERATOR_LIMIT + " iterators are outstanding, maximum limit exceeded"));
}
final LocalDB.LocalDBIterator iterator = new DbIterator(db);
dbIterators.add(iterator);
LOGGER.trace(this.getClass().getSimpleName() + " issued iterator for " + db.toString() + ", outstanding iterators: " + dbIterators.size());
return iterator;
} catch (final Exception e) {
throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, e.getMessage()));
}
}
use of password.pwm.error.ErrorInformation in project pwm by pwm-project.
the class AbstractJDBCLocalDB method executeUpdateStatement.
private void executeUpdateStatement(final String sqlText, final String... values) throws LocalDBException {
lock.writeLock().lock();
try {
PreparedStatement statement = null;
try {
statement = dbConnection.prepareStatement(sqlText);
for (int i = 0; i < values.length; i++) {
statement.setString(i + 1, values[i]);
}
statement.executeUpdate();
dbConnection.commit();
} catch (final SQLException ex) {
throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
} finally {
close(statement);
}
} finally {
lock.writeLock().unlock();
}
}
use of password.pwm.error.ErrorInformation in project pwm by pwm-project.
the class AbstractJDBCLocalDB method truncate.
public void truncate(final LocalDB.DB db) throws LocalDBException {
preCheck(true);
final Instant startTime = Instant.now();
final StringBuilder sqlText = new StringBuilder();
sqlText.append("DROP TABLE ").append(db.toString());
PreparedStatement statement = null;
try {
lock.writeLock().lock();
try {
final Set<LocalDB.LocalDBIterator<String>> copiedIterators = new HashSet<>();
copiedIterators.addAll(dbIterators);
for (final LocalDB.LocalDBIterator dbIterator : copiedIterators) {
dbIterator.close();
}
statement = dbConnection.prepareStatement(sqlText.toString());
statement.executeUpdate();
dbConnection.commit();
LOGGER.debug("truncated table " + db.toString() + " (" + TimeDuration.fromCurrent(startTime).asCompactString() + ")");
initTable(dbConnection, db);
} catch (final SQLException ex) {
throw new LocalDBException(new ErrorInformation(PwmError.ERROR_LOCALDB_UNAVAILABLE, ex.getMessage()));
} finally {
close(statement);
}
} finally {
lock.writeLock().unlock();
}
}
Aggregations