use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.
the class ForgottenUsernameServlet method forwardToCompletePage.
private static void forwardToCompletePage(final PwmRequest pwmRequest, final UserIdentity userIdentity) throws PwmUnrecoverableException, ServletException, IOException {
final Locale locale = pwmRequest.getLocale();
final String completeMessage = pwmRequest.getConfig().readSettingAsLocalizedString(PwmSetting.FORGOTTEN_USERNAME_MESSAGE, locale);
final MacroMachine macroMachine = MacroMachine.forUser(pwmRequest.getPwmApplication(), pwmRequest.getLocale(), pwmRequest.getSessionLabel(), userIdentity);
final String expandedText = macroMachine.expandMacros(completeMessage);
pwmRequest.setAttribute(PwmRequestAttribute.CompleteText, expandedText);
pwmRequest.forwardToJsp(JspUrl.FORGOTTEN_USERNAME_COMPLETE);
}
use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.
the class ForgottenUsernameServlet method sendEmailViaMethod.
private static ErrorInformation sendEmailViaMethod(final PwmApplication pwmApplication, final SessionLabel sessionLabel, final UserInfo userInfo, final EmailItemBean emailItemBean) throws PwmUnrecoverableException {
if (emailItemBean == null) {
final String errorMsg = "emailItemBean is null";
return new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
}
final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, sessionLabel, userInfo, null);
pwmApplication.getEmailQueue().submitEmail(emailItemBean, userInfo, macroMachine);
return null;
}
use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.
the class NewUserUtils method createMacroMachineForNewUser.
static MacroMachine createMacroMachineForNewUser(final PwmApplication pwmApplication, final SessionLabel sessionLabel, final NewUserForm newUserForm, final TokenDestinationItem tokenDestinationItem) throws PwmUnrecoverableException {
final Map<String, String> formValues = newUserForm.getFormData();
final String emailAddressAttribute = pwmApplication.getConfig().getDefaultLdapProfile().readSettingAsString(PwmSetting.EMAIL_USER_MAIL_ATTRIBUTE);
final String usernameAttribute = pwmApplication.getConfig().getDefaultLdapProfile().readSettingAsString(PwmSetting.LDAP_USERNAME_ATTRIBUTE);
final LoginInfoBean stubLoginBean = new LoginInfoBean();
stubLoginBean.setUserCurrentPassword(newUserForm.getNewUserPassword());
final UserInfoBean stubUserBean = UserInfoBean.builder().userEmailAddress(formValues.get(emailAddressAttribute)).username(formValues.get(usernameAttribute)).attributes(formValues).build();
final MacroMachine.StringReplacer stringReplacer = tokenDestinationItem == null ? null : TokenUtil.makeTokenDestStringReplacer(tokenDestinationItem);
return MacroMachine.forUser(pwmApplication, sessionLabel, stubUserBean, stubLoginBean, stringReplacer);
}
use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.
the class NewUserUtils method determineUserDN.
static String determineUserDN(final PwmRequest pwmRequest, final NewUserForm formValues) throws PwmUnrecoverableException, ChaiUnavailableException {
final MacroMachine macroMachine = createMacroMachineForNewUser(pwmRequest.getPwmApplication(), pwmRequest.getSessionLabel(), formValues, null);
final NewUserProfile newUserProfile = NewUserServlet.getNewUserProfile(pwmRequest);
final List<String> configuredNames = newUserProfile.readSettingAsStringArray(PwmSetting.NEWUSER_USERNAME_DEFINITION);
final List<String> failedValues = new ArrayList<>();
final String configuredContext = newUserProfile.readSettingAsString(PwmSetting.NEWUSER_CONTEXT);
final String expandedContext = macroMachine.expandMacros(configuredContext);
if (configuredNames == null || configuredNames.isEmpty() || configuredNames.iterator().next().isEmpty()) {
final String namingAttribute = pwmRequest.getConfig().getDefaultLdapProfile().readSettingAsString(PwmSetting.LDAP_NAMING_ATTRIBUTE);
String namingValue = null;
for (final String formKey : formValues.getFormData().keySet()) {
if (formKey.equals(namingAttribute)) {
namingValue = formValues.getFormData().get(formKey);
}
}
if (namingValue == null || namingValue.isEmpty()) {
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_NEW_USER_FAILURE, "username definition not set, and naming attribute is not present in form"));
}
final String escapedName = StringUtil.escapeLdapDN(namingValue);
final String generatedDN = namingAttribute + "=" + escapedName + "," + expandedContext;
NewUserUtils.LOGGER.debug(pwmRequest, "generated dn for new user: " + generatedDN);
return generatedDN;
}
int attemptCount = 0;
final String generatedDN;
while (attemptCount < configuredNames.size()) {
final String expandedName;
{
{
final String configuredName = configuredNames.get(attemptCount);
expandedName = macroMachine.expandMacros(configuredName);
}
if (!testIfEntryNameExists(pwmRequest, expandedName)) {
NewUserUtils.LOGGER.trace(pwmRequest, "generated entry name for new user is unique: " + expandedName);
final String namingAttribute = pwmRequest.getConfig().getDefaultLdapProfile().readSettingAsString(PwmSetting.LDAP_NAMING_ATTRIBUTE);
final String escapedName = StringUtil.escapeLdapDN(expandedName);
generatedDN = namingAttribute + "=" + escapedName + "," + expandedContext;
NewUserUtils.LOGGER.debug(pwmRequest, "generated dn for new user: " + generatedDN);
return generatedDN;
} else {
failedValues.add(expandedName);
}
}
NewUserUtils.LOGGER.debug(pwmRequest, "generated entry name for new user is not unique, will try again");
attemptCount++;
}
NewUserUtils.LOGGER.error(pwmRequest, "failed to generate new user DN after " + attemptCount + " attempts, failed values: " + JsonUtil.serializeCollection(failedValues));
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_NEW_USER_FAILURE, "unable to generate a unique DN value"));
}
use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.
the class NewUserUtils method checkForTokenVerificationProgress.
static ProcessStatus checkForTokenVerificationProgress(final PwmRequest pwmRequest, final NewUserBean newUserBean, final NewUserProfile newUserProfile) throws PwmUnrecoverableException, ServletException, IOException {
final Map<String, TokenDestinationItem.Type> requiredTokenValidations = determineTokenValidationsRequired(pwmRequest, newUserBean, newUserProfile);
if (!requiredTokenValidations.isEmpty()) {
final Set<String> remainingValidations = new HashSet<>(requiredTokenValidations.keySet());
remainingValidations.removeAll(newUserBean.getCompletedTokenFields());
if (!remainingValidations.isEmpty()) {
if (StringUtil.isEmpty(newUserBean.getCurrentTokenField())) {
newUserBean.setCurrentTokenField(remainingValidations.iterator().next());
newUserBean.setTokenSent(false);
}
if (!newUserBean.isTokenSent()) {
final TokenDestinationItem tokenDestinationItem = tokenDestinationItemForCurrentValidation(pwmRequest, newUserBean, newUserProfile);
if (pwmRequest.getConfig().getTokenStorageMethod() == TokenStorageMethod.STORE_LDAP) {
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { "cannot generate new user tokens when storage type is configured as STORE_LDAP." }));
}
final Map<String, String> tokenPayloadMap = NewUserFormUtils.toTokenPayload(pwmRequest, newUserBean);
final MacroMachine macroMachine = createMacroMachineForNewUser(pwmRequest.getPwmApplication(), pwmRequest.getSessionLabel(), newUserBean.getNewUserForm(), tokenDestinationItem);
final TimeDuration tokenLifetime = figureTokenLifetime(pwmRequest.getConfig(), newUserProfile, tokenDestinationItem);
TokenUtil.initializeAndSendToken(pwmRequest, TokenUtil.TokenInitAndSendRequest.builder().userInfo(null).tokenDestinationItem(tokenDestinationItem).emailToSend(PwmSetting.EMAIL_NEWUSER_VERIFICATION).tokenType(TokenType.NEWUSER).smsToSend(PwmSetting.SMS_NEWUSER_TOKEN_TEXT).inputTokenData(tokenPayloadMap).macroMachine(macroMachine).tokenLifetime(tokenLifetime).build());
newUserBean.setTokenSent(true);
}
NewUserServlet.forwardToEnterCode(pwmRequest, newUserProfile, newUserBean);
return ProcessStatus.Halt;
}
}
return ProcessStatus.Continue;
}
Aggregations