use of password.pwm.config.Configuration in project pwm by pwm-project.
the class LdapOtpOperator method readOtpUserConfiguration.
/**
* Read OTP secret and instantiate a OTP User Configuration object.
*/
@Override
public OTPUserRecord readOtpUserConfiguration(final UserIdentity userIdentity, final String userGUID) throws PwmUnrecoverableException {
final Configuration config = getPwmApplication().getConfig();
final LdapProfile ldapProfile = config.getLdapProfiles().get(userIdentity.getLdapProfileID());
final String ldapStorageAttribute = ldapProfile.readSettingAsString(PwmSetting.OTP_SECRET_LDAP_ATTRIBUTE);
if (ldapStorageAttribute == null || ldapStorageAttribute.length() < 1) {
final String errorMsg = "ldap storage attribute is not configured, unable to read OTP secret";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_INVALID_CONFIG, errorMsg);
throw new PwmUnrecoverableException(errorInformation);
}
OTPUserRecord otp = null;
try {
final ChaiUser theUser = pwmApplication.getProxiedChaiUser(userIdentity);
String value = theUser.readStringAttribute(ldapStorageAttribute);
if (config.readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
value = decryptAttributeValue(value);
}
if (value != null) {
otp = decomposeOtpAttribute(value);
}
} catch (ChaiOperationException e) {
final String errorMsg = "unexpected LDAP error reading responses: " + e.getMessage();
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
throw new PwmUnrecoverableException(errorInformation);
} catch (ChaiUnavailableException e) {
final String errorMsg = "unexpected LDAP 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 otp;
}
use of password.pwm.config.Configuration in project pwm by pwm-project.
the class LdapOtpOperator method writeOtpUserConfiguration.
@Override
public void writeOtpUserConfiguration(final PwmSession pwmSession, final UserIdentity userIdentity, final String userGuid, final OTPUserRecord otpConfig) throws PwmUnrecoverableException {
final Configuration config = pwmApplication.getConfig();
final LdapProfile ldapProfile = config.getLdapProfiles().get(userIdentity.getLdapProfileID());
final String ldapStorageAttribute = ldapProfile.readSettingAsString(PwmSetting.OTP_SECRET_LDAP_ATTRIBUTE);
if (ldapStorageAttribute == null || ldapStorageAttribute.length() < 1) {
final String errorMsg = "ldap storage attribute is not configured, unable to write OTP secret";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_INVALID_CONFIG, errorMsg);
throw new PwmUnrecoverableException(errorInformation);
}
String value = composeOtpAttribute(otpConfig);
if (value == null || value.length() == 0) {
final String errorMsg = "Invalid value for OTP secret, unable to store";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_INVALID_CONFIG, errorMsg);
throw new PwmUnrecoverableException(errorInformation);
}
try {
if (config.readSettingAsBoolean(PwmSetting.OTP_SECRET_ENCRYPT)) {
value = encryptAttributeValue(value);
}
final ChaiUser theUser = pwmSession == null ? pwmApplication.getProxiedChaiUser(userIdentity) : pwmSession.getSessionManager().getActor(pwmApplication, userIdentity);
theUser.writeStringAttribute(ldapStorageAttribute, value);
LOGGER.info("saved OTP secret for user to chai-ldap format");
} catch (ChaiException ex) {
final String errorMsg;
if (ex.getErrorCode() == ChaiError.NO_ACCESS) {
errorMsg = "permission error writing OTP secret to ldap attribute '" + ldapStorageAttribute + "', user does not appear to have correct permissions to save OTP secret: " + ex.getMessage();
} else {
errorMsg = "error writing OTP secret to ldap attribute '" + ldapStorageAttribute + "': " + ex.getMessage();
}
final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, errorMsg);
final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
pwmOE.initCause(ex);
throw pwmOE;
} catch (PwmOperationalException ex) {
final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_WRITING_OTP_SECRET, ex.getMessage());
final PwmUnrecoverableException pwmOE = new PwmUnrecoverableException(errorInfo);
pwmOE.initCause(ex);
throw pwmOE;
}
}
use of password.pwm.config.Configuration in project pwm by pwm-project.
the class LocalDBFactory method getInstance.
public static synchronized LocalDB getInstance(final File dbDirectory, final boolean readonly, final PwmApplication pwmApplication, final Configuration configuration) throws Exception {
final Configuration config = (configuration == null && pwmApplication != null) ? pwmApplication.getConfig() : configuration;
final long startTime = System.currentTimeMillis();
final String className;
final Map<String, String> initParameters;
if (config == null) {
className = AppProperty.LOCALDB_IMPLEMENTATION.getDefaultValue();
final String initStrings = AppProperty.LOCALDB_INIT_STRING.getDefaultValue();
initParameters = StringUtil.convertStringListToNameValuePair(Arrays.asList(initStrings.split(";;;")), "=");
} else {
className = config.readAppProperty(AppProperty.LOCALDB_IMPLEMENTATION);
final String initStrings = config.readAppProperty(AppProperty.LOCALDB_INIT_STRING);
initParameters = StringUtil.convertStringListToNameValuePair(Arrays.asList(initStrings.split(";;;")), "=");
}
final Map<LocalDBProvider.Parameter, String> parameters = pwmApplication == null ? Collections.<LocalDBProvider.Parameter, String>emptyMap() : makeParameterMap(pwmApplication.getConfig(), readonly);
final LocalDBProvider dbProvider = createInstance(className);
LOGGER.debug("initializing " + className + " localDBProvider instance");
final LocalDB localDB = new LocalDBAdaptor(dbProvider, pwmApplication);
initInstance(dbProvider, dbDirectory, initParameters, className, parameters);
final TimeDuration openTime = new TimeDuration(System.currentTimeMillis() - startTime);
if (!readonly) {
LOGGER.trace("clearing TEMP db");
localDB.truncate(LocalDB.DB.TEMP);
final LocalDBUtility localDBUtility = new LocalDBUtility(localDB);
if (localDBUtility.readImportInprogressFlag()) {
LOGGER.error("previous database import process did not complete successfully, clearing all data");
localDBUtility.prepareForImport();
localDBUtility.markImportComplete();
}
}
final StringBuilder debugText = new StringBuilder();
debugText.append("LocalDB open in ").append(openTime.asCompactString());
if (localDB.getFileLocation() != null) {
debugText.append(", db size: ").append(StringUtil.formatDiskSize(FileSystemUtility.getFileDirectorySize(localDB.getFileLocation())));
debugText.append(" at ").append(dbDirectory.toString());
final long freeSpace = FileSystemUtility.diskSpaceRemaining(localDB.getFileLocation());
if (freeSpace >= 0) {
debugText.append(", ").append(StringUtil.formatDiskSize(freeSpace)).append(" free");
}
}
LOGGER.info(debugText);
return localDB;
}
use of password.pwm.config.Configuration in project pwm by pwm-project.
the class PwmPasswordJudgeTest method testJudgePassword.
public void testJudgePassword() throws Exception {
final Configuration configuration = Mockito.mock(Configuration.class);
Mockito.when(configuration.readSettingAsEnum(PwmSetting.PASSWORD_STRENGTH_METER_TYPE, StrengthMeterType.class)).thenReturn(StrengthMeterType.PWM);
Assert.assertEquals(0, PasswordUtility.judgePasswordStrength(configuration, ""));
Assert.assertEquals(100, PasswordUtility.judgePasswordStrength(configuration, "V.{a$f.*B697e+%J9pOPn~E0CyqN~9XmR?yjOGFC(k+la?n6&^I3bwZq[miF(`0"));
final List<Integer> judgeValues = new ArrayList<>();
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, ""));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasd"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdA"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdAASDSADSAD"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdAASDSADSAD#"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdAASDSADSAD##@!#!^%&^$*"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdAASDSADSAD##@!#!^%&^$*aa"));
judgeValues.add(PasswordUtility.judgePasswordStrength(configuration, "3sadasdAASDSADSAD##@!#!^%&^$*aaaaaaaaaaaa"));
for (int i = 1; i < judgeValues.size() - 1; i++) {
int v1, v2;
v1 = judgeValues.get(i);
v2 = judgeValues.get(i - 1);
// assertTrue(v1 >= v2);
v1 = judgeValues.get(i);
v2 = judgeValues.get(i + 1);
// assertTrue(v1 <= v2);
}
}
use of password.pwm.config.Configuration in project pwm by pwm-project.
the class EmailQueueManagerTest method testConvertEmailItemToMessage.
@Test
public void testConvertEmailItemToMessage() throws MessagingException, IOException {
EmailService emailService = new EmailService();
Configuration config = Mockito.mock(Configuration.class);
Mockito.when(config.readAppProperty(AppProperty.SMTP_SUBJECT_ENCODING_CHARSET)).thenReturn("UTF8");
EmailItemBean emailItemBean = new EmailItemBean("fred@flintstones.tv, barney@flintstones.tv", "bedrock-admin@flintstones.tv", "Test Subject", "bodyPlain", "bodyHtml");
EmailServer emailServer = EmailServer.builder().javaMailProps(new Properties()).build();
List<Message> messages = EmailServerUtil.convertEmailItemToMessages(emailItemBean, config, emailServer);
Assert.assertEquals(2, messages.size());
Message message = messages.get(0);
Assert.assertEquals(new InternetAddress("fred@flintstones.tv"), message.getRecipients(Message.RecipientType.TO)[0]);
Assert.assertEquals(new InternetAddress("bedrock-admin@flintstones.tv"), message.getFrom()[0]);
Assert.assertEquals("Test Subject", message.getSubject());
String content = IOUtils.toString(message.getInputStream());
Assert.assertTrue(content.contains("bodyPlain"));
Assert.assertTrue(content.contains("bodyHtml"));
message = messages.get(1);
Assert.assertEquals(new InternetAddress("barney@flintstones.tv"), message.getRecipients(Message.RecipientType.TO)[0]);
Assert.assertEquals(new InternetAddress("bedrock-admin@flintstones.tv"), message.getFrom()[0]);
Assert.assertEquals("Test Subject", message.getSubject());
content = IOUtils.toString(message.getInputStream());
Assert.assertTrue(content.contains("bodyPlain"));
Assert.assertTrue(content.contains("bodyHtml"));
}
Aggregations