Search in sources :

Example 26 with ChaiUser

use of com.novell.ldapchai.ChaiUser in project pwm by pwm-project.

the class GuestRegistrationServlet method handleCreateRequest.

private void handleCreateRequest(final PwmRequest pwmRequest, final GuestRegistrationBean guestRegistrationBean) throws PwmUnrecoverableException, ChaiUnavailableException, IOException, ServletException {
    final PwmSession pwmSession = pwmRequest.getPwmSession();
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final LocalSessionStateBean ssBean = pwmSession.getSessionStateBean();
    final Configuration config = pwmApplication.getConfig();
    final Locale locale = ssBean.getLocale();
    final List<FormConfiguration> guestUserForm = config.readSettingAsForm(PwmSetting.GUEST_FORM);
    try {
        // read the values from the request
        final Map<FormConfiguration, String> formValues = FormUtility.readFormValuesFromRequest(pwmRequest, guestUserForm, locale);
        // read the expiration date from the request.
        final Instant expirationDate = readExpirationFromRequest(pwmRequest);
        // see if the values meet form requirements.
        FormUtility.validateFormValues(config, formValues, locale);
        // read new user DN
        final String guestUserDN = determineUserDN(formValues, config);
        // read a chai provider to make the user
        final ChaiProvider provider = pwmSession.getSessionManager().getChaiProvider();
        // set up the user creation attributes
        final Map<String, String> createAttributes = new HashMap<>();
        for (final Map.Entry<FormConfiguration, String> entry : formValues.entrySet()) {
            final FormConfiguration formItem = entry.getKey();
            final String value = entry.getValue();
            LOGGER.debug(pwmSession, "Attribute from form: " + formItem.getName() + " = " + value);
            final String n = formItem.getName();
            final String v = formValues.get(formItem);
            if (n != null && n.length() > 0 && v != null && v.length() > 0) {
                createAttributes.put(n, v);
            }
        }
        // Write creator DN
        createAttributes.put(config.readSettingAsString(PwmSetting.GUEST_ADMIN_ATTRIBUTE), pwmSession.getUserInfo().getUserIdentity().getUserDN());
        // read the creation object classes.
        final Set<String> createObjectClasses = new HashSet<>(config.readSettingAsStringArray(PwmSetting.DEFAULT_OBJECT_CLASSES));
        provider.createEntry(guestUserDN, createObjectClasses, createAttributes);
        LOGGER.info(pwmSession, "created user object: " + guestUserDN);
        final ChaiUser theUser = provider.getEntryFactory().newChaiUser(guestUserDN);
        final UserIdentity userIdentity = new UserIdentity(guestUserDN, pwmSession.getUserInfo().getUserIdentity().getLdapProfileID());
        // write the expiration date:
        if (expirationDate != null) {
            final String expirationAttr = config.readSettingAsString(PwmSetting.GUEST_EXPIRATION_ATTRIBUTE);
            theUser.writeDateAttribute(expirationAttr, expirationDate);
        }
        final PwmPasswordPolicy passwordPolicy = PasswordUtility.readPasswordPolicyForUser(pwmApplication, pwmSession.getLabel(), userIdentity, theUser, locale);
        final PasswordData newPassword = RandomPasswordGenerator.createRandomPassword(pwmSession.getLabel(), passwordPolicy, pwmApplication);
        theUser.setPassword(newPassword.getStringValue());
        {
            // execute configured actions
            LOGGER.debug(pwmSession, "executing configured actions to user " + theUser.getEntryDN());
            final List<ActionConfiguration> actions = pwmApplication.getConfig().readSettingAsAction(PwmSetting.GUEST_WRITE_ATTRIBUTES);
            if (actions != null && !actions.isEmpty()) {
                final MacroMachine macroMachine = MacroMachine.forUser(pwmRequest, userIdentity);
                final ActionExecutor actionExecutor = new ActionExecutor.ActionExecutorSettings(pwmApplication, theUser).setExpandPwmMacros(true).setMacroMachine(macroMachine).createActionExecutor();
                actionExecutor.executeActions(actions, pwmRequest.getSessionLabel());
            }
        }
        // everything good so forward to success page.
        this.sendGuestUserEmailConfirmation(pwmRequest, userIdentity);
        pwmApplication.getStatisticsManager().incrementValue(Statistic.NEW_USERS);
        pwmRequest.getPwmResponse().forwardToSuccessPage(Message.Success_CreateGuest);
    } catch (ChaiOperationException e) {
        final ErrorInformation info = new ErrorInformation(PwmError.ERROR_NEW_USER_FAILURE, "error creating user: " + e.getMessage());
        setLastError(pwmRequest, info);
        LOGGER.warn(pwmSession, info);
        this.forwardToJSP(pwmRequest, guestRegistrationBean);
    } catch (PwmOperationalException e) {
        LOGGER.error(pwmSession, e.getErrorInformation().toDebugStr());
        setLastError(pwmRequest, e.getErrorInformation());
        this.forwardToJSP(pwmRequest, guestRegistrationBean);
    }
}
Also used : Locale(java.util.Locale) FormConfiguration(password.pwm.config.value.data.FormConfiguration) SearchConfiguration(password.pwm.ldap.search.SearchConfiguration) ActionConfiguration(password.pwm.config.value.data.ActionConfiguration) Configuration(password.pwm.config.Configuration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PwmOperationalException(password.pwm.error.PwmOperationalException) ErrorInformation(password.pwm.error.ErrorInformation) PasswordData(password.pwm.util.PasswordData) PwmPasswordPolicy(password.pwm.config.profile.PwmPasswordPolicy) FormConfiguration(password.pwm.config.value.data.FormConfiguration) List(java.util.List) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) HashSet(java.util.HashSet) ActionExecutor(password.pwm.util.operations.ActionExecutor) PwmApplication(password.pwm.PwmApplication) Instant(java.time.Instant) UserIdentity(password.pwm.bean.UserIdentity) ChaiProvider(com.novell.ldapchai.provider.ChaiProvider) ChaiUser(com.novell.ldapchai.ChaiUser) MacroMachine(password.pwm.util.macro.MacroMachine) LocalSessionStateBean(password.pwm.bean.LocalSessionStateBean) PwmSession(password.pwm.http.PwmSession) Map(java.util.Map) FormMap(password.pwm.util.FormMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with ChaiUser

use of com.novell.ldapchai.ChaiUser in project pwm by pwm-project.

the class DeleteAccountServlet method handleDeleteRequest.

@ActionHandler(action = "delete")
private ProcessStatus handleDeleteRequest(final PwmRequest pwmRequest) throws ServletException, IOException, PwmUnrecoverableException, ChaiUnavailableException {
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final DeleteAccountProfile deleteAccountProfile = getProfile(pwmRequest);
    final UserIdentity userIdentity = pwmRequest.getUserInfoIfLoggedIn();
    {
        // execute configured actions
        final List<ActionConfiguration> actions = deleteAccountProfile.readSettingAsAction(PwmSetting.DELETE_ACCOUNT_ACTIONS);
        if (actions != null && !actions.isEmpty()) {
            LOGGER.debug(pwmRequest, "executing configured actions to user " + userIdentity);
            final ActionExecutor actionExecutor = new ActionExecutor.ActionExecutorSettings(pwmApplication, userIdentity).setExpandPwmMacros(true).setMacroMachine(pwmRequest.getPwmSession().getSessionManager().getMacroMachine(pwmApplication)).createActionExecutor();
            try {
                actionExecutor.executeActions(actions, pwmRequest.getSessionLabel());
            } catch (PwmOperationalException e) {
                LOGGER.error("error during user delete action execution: " + e.getMessage());
                throw new PwmUnrecoverableException(e.getErrorInformation(), e.getCause());
            }
        }
    }
    // send notification
    sendProfileUpdateEmailNotice(pwmRequest);
    // mark the event log
    pwmApplication.getAuditManager().submit(AuditEvent.DELETE_ACCOUNT, pwmRequest.getPwmSession().getUserInfo(), pwmRequest.getPwmSession());
    final String nextUrl = deleteAccountProfile.readSettingAsString(PwmSetting.DELETE_ACCOUNT_NEXT_URL);
    if (nextUrl != null && !nextUrl.isEmpty()) {
        final MacroMachine macroMachine = pwmRequest.getPwmSession().getSessionManager().getMacroMachine(pwmApplication);
        final String macroedUrl = macroMachine.expandMacros(nextUrl);
        LOGGER.debug(pwmRequest, "settinging forward url to post-delete next url: " + macroedUrl);
        pwmRequest.getPwmSession().getSessionStateBean().setForwardURL(macroedUrl);
    }
    // perform ldap entry delete.
    if (deleteAccountProfile.readSettingAsBoolean(PwmSetting.DELETE_ACCOUNT_DELETE_USER_ENTRY)) {
        final ChaiUser chaiUser = pwmApplication.getProxiedChaiUser(pwmRequest.getUserInfoIfLoggedIn());
        try {
            chaiUser.getChaiProvider().deleteEntry(chaiUser.getEntryDN());
        } catch (ChaiException e) {
            final PwmUnrecoverableException pwmException = PwmUnrecoverableException.fromChaiException(e);
            LOGGER.error("error during user delete", pwmException);
            throw pwmException;
        }
    }
    // clear the delete bean
    pwmApplication.getSessionStateService().clearBean(pwmRequest, DeleteAccountBean.class);
    // delete finished, so logout and redirect.
    pwmRequest.getPwmSession().unauthenticateUser(pwmRequest);
    pwmRequest.sendRedirectToContinue();
    return ProcessStatus.Halt;
}
Also used : ActionExecutor(password.pwm.util.operations.ActionExecutor) PwmApplication(password.pwm.PwmApplication) ChaiUser(com.novell.ldapchai.ChaiUser) UserIdentity(password.pwm.bean.UserIdentity) DeleteAccountProfile(password.pwm.config.profile.DeleteAccountProfile) MacroMachine(password.pwm.util.macro.MacroMachine) List(java.util.List) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiException(com.novell.ldapchai.exception.ChaiException) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 28 with ChaiUser

use of com.novell.ldapchai.ChaiUser in project pwm by pwm-project.

the class GuestRegistrationServlet method handleUpdateRequest.

protected void handleUpdateRequest(final PwmRequest pwmRequest, final GuestRegistrationBean guestRegistrationBean) throws ServletException, ChaiUnavailableException, IOException, PwmUnrecoverableException {
    // Fetch the session state bean.
    final PwmSession pwmSession = pwmRequest.getPwmSession();
    final LocalSessionStateBean ssBean = pwmSession.getSessionStateBean();
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final Configuration config = pwmApplication.getConfig();
    final List<FormConfiguration> formItems = pwmApplication.getConfig().readSettingAsForm(PwmSetting.GUEST_UPDATE_FORM);
    final String expirationAttribute = config.readSettingAsString(PwmSetting.GUEST_EXPIRATION_ATTRIBUTE);
    try {
        // read the values from the request
        final Map<FormConfiguration, String> formValues = FormUtility.readFormValuesFromRequest(pwmRequest, formItems, pwmRequest.getLocale());
        // see if the values meet form requirements.
        FormUtility.validateFormValues(config, formValues, ssBean.getLocale());
        // read current values from user.
        final ChaiUser theGuest = pwmSession.getSessionManager().getActor(pwmApplication, guestRegistrationBean.getUpdateUserIdentity());
        // check unique fields against ldap
        FormUtility.validateFormValueUniqueness(pwmApplication, formValues, ssBean.getLocale(), Collections.singletonList(guestRegistrationBean.getUpdateUserIdentity()));
        final Instant expirationDate = readExpirationFromRequest(pwmRequest);
        // Update user attributes
        LdapOperationsHelper.writeFormValuesToLdap(pwmApplication, pwmSession.getSessionManager().getMacroMachine(pwmApplication), theGuest, formValues, false);
        // Write expirationDate
        if (expirationDate != null) {
            theGuest.writeDateAttribute(expirationAttribute, expirationDate);
        }
        // send email.
        final UserInfo guestUserInfoBean = UserInfoFactory.newUserInfo(pwmApplication, pwmRequest.getSessionLabel(), pwmRequest.getLocale(), guestRegistrationBean.getUpdateUserIdentity(), theGuest.getChaiProvider());
        this.sendUpdateGuestEmailConfirmation(pwmRequest, guestUserInfoBean);
        pwmApplication.getStatisticsManager().incrementValue(Statistic.UPDATED_GUESTS);
        // everything good so forward to confirmation page.
        pwmRequest.getPwmResponse().forwardToSuccessPage(Message.Success_UpdateGuest);
        return;
    } catch (PwmOperationalException e) {
        LOGGER.error(pwmSession, e.getErrorInformation().toDebugStr());
        setLastError(pwmRequest, e.getErrorInformation());
    } catch (ChaiOperationException e) {
        final ErrorInformation info = new ErrorInformation(PwmError.ERROR_UNKNOWN, "unexpected error writing to ldap: " + e.getMessage());
        LOGGER.error(pwmSession, info);
        setLastError(pwmRequest, info);
    }
    this.forwardToUpdateJSP(pwmRequest, guestRegistrationBean);
}
Also used : PwmApplication(password.pwm.PwmApplication) FormConfiguration(password.pwm.config.value.data.FormConfiguration) SearchConfiguration(password.pwm.ldap.search.SearchConfiguration) ActionConfiguration(password.pwm.config.value.data.ActionConfiguration) Configuration(password.pwm.config.Configuration) Instant(java.time.Instant) UserInfo(password.pwm.ldap.UserInfo) PwmOperationalException(password.pwm.error.PwmOperationalException) ErrorInformation(password.pwm.error.ErrorInformation) ChaiUser(com.novell.ldapchai.ChaiUser) LocalSessionStateBean(password.pwm.bean.LocalSessionStateBean) FormConfiguration(password.pwm.config.value.data.FormConfiguration) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) PwmSession(password.pwm.http.PwmSession)

Example 29 with ChaiUser

use of com.novell.ldapchai.ChaiUser in project pwm by pwm-project.

the class ActivateUserUtils method validateParamsAgainstLDAP.

static void validateParamsAgainstLDAP(final PwmRequest pwmRequest, final Map<FormConfiguration, String> formValues, final UserIdentity userIdentity) throws ChaiUnavailableException, PwmDataValidationException, PwmUnrecoverableException {
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final PwmSession pwmSession = pwmRequest.getPwmSession();
    final String searchFilter = figureLdapSearchFilter(pwmRequest);
    final ChaiProvider chaiProvider = pwmApplication.getProxyChaiProvider(userIdentity.getLdapProfileID());
    final ChaiUser chaiUser = chaiProvider.getEntryFactory().newChaiUser(userIdentity.getUserDN());
    for (final Map.Entry<FormConfiguration, String> entry : formValues.entrySet()) {
        final FormConfiguration formItem = entry.getKey();
        final String attrName = formItem.getName();
        final String tokenizedAttrName = "%" + attrName + "%";
        if (searchFilter.contains(tokenizedAttrName)) {
            LOGGER.trace(pwmSession, "skipping validation of ldap value for '" + attrName + "' because it is in search filter");
        } else {
            final String value = entry.getValue();
            try {
                if (!chaiUser.compareStringAttribute(attrName, value)) {
                    final String errorMsg = "incorrect value for '" + attrName + "'";
                    final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_ACTIVATION_VALIDATIONFAIL, errorMsg, new String[] { attrName });
                    LOGGER.debug(pwmSession.getLabel(), errorInfo.toDebugStr());
                    throw new PwmDataValidationException(errorInfo);
                }
                LOGGER.trace(pwmSession.getLabel(), "successful validation of ldap value for '" + attrName + "'");
            } catch (ChaiOperationException e) {
                LOGGER.error(pwmSession.getLabel(), "error during param validation of '" + attrName + "', error: " + e.getMessage());
                throw new PwmDataValidationException(new ErrorInformation(PwmError.ERROR_ACTIVATION_VALIDATIONFAIL, "ldap error testing value for '" + attrName + "'", new String[] { attrName }));
            }
        }
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmDataValidationException(password.pwm.error.PwmDataValidationException) PwmApplication(password.pwm.PwmApplication) ChaiProvider(com.novell.ldapchai.provider.ChaiProvider) ChaiUser(com.novell.ldapchai.ChaiUser) FormConfiguration(password.pwm.config.value.data.FormConfiguration) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) PwmSession(password.pwm.http.PwmSession) Map(java.util.Map)

Example 30 with ChaiUser

use of com.novell.ldapchai.ChaiUser in project pwm by pwm-project.

the class RestChallengesServer method doSetChallengeDataJson.

@RestMethodHandler(method = HttpMethod.POST, consumes = HttpContentType.json, produces = HttpContentType.json)
public RestResultBean doSetChallengeDataJson(final RestRequest restRequest) throws IOException, PwmUnrecoverableException {
    final JsonChallengesData jsonInput = RestUtility.deserializeJsonBody(restRequest, JsonChallengesData.class);
    final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername(restRequest, jsonInput.getUsername());
    try {
        final ChaiUser chaiUser;
        final String userGUID;
        final String csIdentifer;
        final UserIdentity userIdentity;
        final CrService crService = restRequest.getPwmApplication().getCrService();
        userIdentity = targetUserIdentity.getUserIdentity();
        chaiUser = targetUserIdentity.getChaiUser();
        userGUID = LdapOperationsHelper.readLdapGuidValue(restRequest.getPwmApplication(), restRequest.getSessionLabel(), userIdentity, false);
        final ChallengeProfile challengeProfile = crService.readUserChallengeProfile(restRequest.getSessionLabel(), userIdentity, chaiUser, PwmPasswordPolicy.defaultPolicy(), restRequest.getLocale());
        csIdentifer = challengeProfile.getChallengeSet().getIdentifier();
        final ResponseInfoBean responseInfoBean = jsonInput.toResponseInfoBean(restRequest.getLocale(), csIdentifer);
        crService.writeResponses(userIdentity, chaiUser, userGUID, responseInfoBean);
        // update statistics
        StatisticsManager.incrementStat(restRequest.getPwmApplication(), Statistic.REST_CHALLENGES);
        return RestResultBean.forSuccessMessage(restRequest, Message.Success_SetupResponse);
    } catch (Exception e) {
        final String errorMsg = "unexpected error reading json input: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ChaiUser(com.novell.ldapchai.ChaiUser) UserIdentity(password.pwm.bean.UserIdentity) ChallengeProfile(password.pwm.config.profile.ChallengeProfile) ResponseInfoBean(password.pwm.bean.ResponseInfoBean) CrService(password.pwm.util.operations.CrService) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiException(com.novell.ldapchai.exception.ChaiException) PwmOperationalException(password.pwm.error.PwmOperationalException) IOException(java.io.IOException) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Aggregations

ChaiUser (com.novell.ldapchai.ChaiUser)69 ErrorInformation (password.pwm.error.ErrorInformation)38 UserIdentity (password.pwm.bean.UserIdentity)30 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)27 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)25 PwmOperationalException (password.pwm.error.PwmOperationalException)23 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)21 ChaiException (com.novell.ldapchai.exception.ChaiException)18 ChaiProvider (com.novell.ldapchai.provider.ChaiProvider)18 PwmApplication (password.pwm.PwmApplication)16 PwmSession (password.pwm.http.PwmSession)12 UserInfo (password.pwm.ldap.UserInfo)12 Instant (java.time.Instant)10 FormConfiguration (password.pwm.config.value.data.FormConfiguration)10 PasswordData (password.pwm.util.PasswordData)10 MacroMachine (password.pwm.util.macro.MacroMachine)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 LdapProfile (password.pwm.config.profile.LdapProfile)9 Locale (java.util.Locale)8