Search in sources :

Example 46 with MacroMachine

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);
}
Also used : Locale(java.util.Locale) MacroMachine(password.pwm.util.macro.MacroMachine)

Example 47 with MacroMachine

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;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) MacroMachine(password.pwm.util.macro.MacroMachine)

Example 48 with MacroMachine

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);
}
Also used : UserInfoBean(password.pwm.ldap.UserInfoBean) LoginInfoBean(password.pwm.bean.LoginInfoBean) MacroMachine(password.pwm.util.macro.MacroMachine)

Example 49 with MacroMachine

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"));
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ArrayList(java.util.ArrayList) MacroMachine(password.pwm.util.macro.MacroMachine) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) NewUserProfile(password.pwm.config.profile.NewUserProfile)

Example 50 with MacroMachine

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;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) TokenType(password.pwm.svc.token.TokenType) MacroMachine(password.pwm.util.macro.MacroMachine) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) TimeDuration(password.pwm.util.java.TimeDuration) TokenDestinationItem(password.pwm.bean.TokenDestinationItem) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

MacroMachine (password.pwm.util.macro.MacroMachine)61 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)22 ErrorInformation (password.pwm.error.ErrorInformation)20 Locale (java.util.Locale)16 PwmOperationalException (password.pwm.error.PwmOperationalException)15 Configuration (password.pwm.config.Configuration)13 UserInfo (password.pwm.ldap.UserInfo)13 ArrayList (java.util.ArrayList)12 LinkedHashMap (java.util.LinkedHashMap)12 PwmApplication (password.pwm.PwmApplication)12 FormConfiguration (password.pwm.config.value.data.FormConfiguration)12 ChaiUser (com.novell.ldapchai.ChaiUser)10 PwmException (password.pwm.error.PwmException)10 List (java.util.List)9 EmailItemBean (password.pwm.bean.EmailItemBean)9 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)8 Map (java.util.Map)8 ActionConfiguration (password.pwm.config.value.data.ActionConfiguration)8 PwmSession (password.pwm.http.PwmSession)8 Instant (java.time.Instant)7