Search in sources :

Example 6 with DatabaseAccessor

use of password.pwm.util.db.DatabaseAccessor in project pwm by pwm-project.

the class DbOtpOperator method readOtpUserConfiguration.

@Override
public OTPUserRecord readOtpUserConfiguration(final UserIdentity theUser, final String userGUID) throws PwmUnrecoverableException {
    LOGGER.trace(String.format("Enter: readOtpUserConfiguration(%s, %s)", theUser, userGUID));
    if (userGUID == null || userGUID.length() < 1) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_GUID, "cannot save otp to db, user does not have a GUID"));
    }
    OTPUserRecord otpConfig = null;
    try {
        final DatabaseAccessor databaseAccessor = pwmApplication.getDatabaseAccessor();
        String value = databaseAccessor.get(DatabaseTable.OTP, userGUID);
        if (value != null && value.length() > 0) {
            if (getPwmApplication().getConfig().readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
                value = decryptAttributeValue(value);
            }
            if (value != null) {
                otpConfig = decomposeOtpAttribute(value);
            }
            if (otpConfig != null) {
                LOGGER.debug("found user OTP secret in db: " + otpConfig.toString());
            }
        }
    } catch (LocalDBException e) {
        final String errorMsg = "unexpected LocalDB error reading responses: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        throw new PwmUnrecoverableException(errorInformation);
    } catch (PwmOperationalException e) {
        final String errorMsg = "unexpected error reading responses: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        throw new PwmUnrecoverableException(errorInformation);
    }
    return otpConfig;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) DatabaseAccessor(password.pwm.util.db.DatabaseAccessor) LocalDBException(password.pwm.util.localdb.LocalDBException) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 7 with DatabaseAccessor

use of password.pwm.util.db.DatabaseAccessor in project pwm by pwm-project.

the class DbOtpOperator method writeOtpUserConfiguration.

@Override
public void writeOtpUserConfiguration(final PwmSession pwmSession, final UserIdentity theUser, final String userGUID, final OTPUserRecord otpConfig) throws PwmUnrecoverableException {
    if (userGUID == null || userGUID.length() < 1) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_GUID, "cannot save OTP secret to remote database, user " + theUser + " does not have a guid"));
    }
    LOGGER.trace("attempting to save OTP secret for " + theUser + " in remote database (key=" + userGUID + ")");
    try {
        String value = composeOtpAttribute(otpConfig);
        if (getPwmApplication().getConfig().readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
            LOGGER.debug("Encrypting OTP secret for storage");
            value = encryptAttributeValue(value);
        }
        final DatabaseAccessor databaseAccessor = pwmApplication.getDatabaseAccessor();
        databaseAccessor.put(DatabaseTable.OTP, userGUID, value);
        LOGGER.info("saved OTP secret for " + theUser + " in remote database (key=" + userGUID + ")");
    } catch (PwmOperationalException ex) {
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, "unexpected error saving otp to db: " + ex.getMessage());
        final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
        pwmOE.initCause(ex);
        throw pwmOE;
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) DatabaseAccessor(password.pwm.util.db.DatabaseAccessor) PwmOperationalException(password.pwm.error.PwmOperationalException)

Aggregations

DatabaseAccessor (password.pwm.util.db.DatabaseAccessor)7 ErrorInformation (password.pwm.error.ErrorInformation)6 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)6 PwmOperationalException (password.pwm.error.PwmOperationalException)3 DatabaseException (password.pwm.util.db.DatabaseException)3 ChaiResponseSet (com.novell.ldapchai.cr.ChaiResponseSet)2 ResponseSet (com.novell.ldapchai.cr.ResponseSet)1 ChaiException (com.novell.ldapchai.exception.ChaiException)1 ChaiValidationException (com.novell.ldapchai.exception.ChaiValidationException)1 PwmApplication (password.pwm.PwmApplication)1 PwmEnvironment (password.pwm.PwmEnvironment)1 PwmException (password.pwm.error.PwmException)1 LocalDBException (password.pwm.util.localdb.LocalDBException)1