Search in sources :

Example 6 with MacroMachine

use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.

the class RemoteVerificationMethod method sendRemoteRequest.

private void sendRemoteRequest(final Map<String, String> userResponses) throws PwmUnrecoverableException {
    lastResponse = null;
    final Map<String, String> headers = new LinkedHashMap<>();
    headers.put(HttpHeader.Content_Type.getHttpName(), HttpContentType.json.getHeaderValue());
    headers.put(HttpHeader.Accept_Language.getHttpName(), locale.toLanguageTag());
    final RemoteVerificationRequestBean remoteVerificationRequestBean = new RemoteVerificationRequestBean();
    remoteVerificationRequestBean.setResponseSessionID(this.remoteSessionID);
    final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, PwmConstants.DEFAULT_LOCALE, SessionLabel.SYSTEM_LABEL, userInfo.getUserIdentity());
    remoteVerificationRequestBean.setUserInfo(PublicUserInfoBean.fromUserInfoBean(userInfo, pwmApplication.getConfig(), locale, macroMachine));
    remoteVerificationRequestBean.setUserResponses(userResponses);
    final PwmHttpClientRequest pwmHttpClientRequest = new PwmHttpClientRequest(HttpMethod.POST, url, JsonUtil.serialize(remoteVerificationRequestBean), headers);
    try {
        final PwmHttpClientResponse response = pwmHttpClient.makeRequest(pwmHttpClientRequest);
        final String responseBodyStr = response.getBody();
        this.lastResponse = JsonUtil.deserialize(responseBodyStr, RemoteVerificationResponseBean.class);
    } catch (PwmException e) {
        LOGGER.error(sessionLabel, e.getErrorInformation());
        throw new PwmUnrecoverableException(e.getErrorInformation());
    } catch (Exception e) {
        final String errorMsg = "error reading remote responses web service response: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_SERVICE_NOT_AVAILABLE, errorMsg);
        LOGGER.error(sessionLabel, errorInformation);
        throw new PwmUnrecoverableException(errorInformation);
    }
}
Also used : RemoteVerificationRequestBean(password.pwm.bean.RemoteVerificationRequestBean) PwmException(password.pwm.error.PwmException) RemoteVerificationResponseBean(password.pwm.bean.RemoteVerificationResponseBean) ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) MacroMachine(password.pwm.util.macro.MacroMachine) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with MacroMachine

use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.

the class HelpdeskServlet method processExecuteActionRequest.

@ActionHandler(action = "executeAction")
private ProcessStatus processExecuteActionRequest(final PwmRequest pwmRequest) throws ChaiUnavailableException, PwmUnrecoverableException, IOException, ServletException {
    final HelpdeskProfile helpdeskProfile = getHelpdeskProfile(pwmRequest);
    final String userKey = pwmRequest.readBodyAsJsonStringMap(PwmHttpRequestWrapper.Flag.BypassValidation).get(PwmConstants.PARAM_USERKEY);
    if (userKey == null || userKey.length() < 1) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_MISSING_PARAMETER, PwmConstants.PARAM_USERKEY + " parameter is missing");
        setLastError(pwmRequest, errorInformation);
        pwmRequest.respondWithError(errorInformation, false);
        return ProcessStatus.Halt;
    }
    final UserIdentity userIdentity = UserIdentity.fromKey(userKey, pwmRequest.getPwmApplication());
    LOGGER.debug(pwmRequest, "received executeAction request for user " + userIdentity.toString());
    final List<ActionConfiguration> actionConfigurations = helpdeskProfile.readSettingAsAction(PwmSetting.HELPDESK_ACTIONS);
    final String requestedName = pwmRequest.readParameterAsString("name");
    ActionConfiguration action = null;
    for (final ActionConfiguration loopAction : actionConfigurations) {
        if (requestedName != null && requestedName.equals(loopAction.getName())) {
            action = loopAction;
            break;
        }
    }
    if (action == null) {
        final String errorMsg = "request to execute unknown action: " + requestedName;
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        LOGGER.debug(pwmRequest, errorInformation.toDebugStr());
        final RestResultBean restResultBean = RestResultBean.fromError(errorInformation, pwmRequest);
        pwmRequest.outputJsonResult(restResultBean);
        return ProcessStatus.Halt;
    }
    // check if user should be seen by actor
    HelpdeskServletUtil.checkIfUserIdentityViewable(pwmRequest, helpdeskProfile, userIdentity);
    final boolean useProxy = helpdeskProfile.readSettingAsBoolean(PwmSetting.HELPDESK_USE_PROXY);
    try {
        final PwmSession pwmSession = pwmRequest.getPwmSession();
        final ChaiUser chaiUser = useProxy ? pwmRequest.getPwmApplication().getProxiedChaiUser(userIdentity) : pwmRequest.getPwmSession().getSessionManager().getActor(pwmRequest.getPwmApplication(), userIdentity);
        final MacroMachine macroMachine = MacroMachine.forUser(pwmRequest, userIdentity);
        final ActionExecutor actionExecutor = new ActionExecutor.ActionExecutorSettings(pwmRequest.getPwmApplication(), chaiUser).setExpandPwmMacros(true).setMacroMachine(macroMachine).createActionExecutor();
        actionExecutor.executeAction(action, pwmRequest.getSessionLabel());
        // mark the event log
        {
            final HelpdeskAuditRecord auditRecord = new AuditRecordFactory(pwmRequest).createHelpdeskAuditRecord(AuditEvent.HELPDESK_ACTION, pwmSession.getUserInfo().getUserIdentity(), action.getName(), userIdentity, pwmSession.getSessionStateBean().getSrcAddress(), pwmSession.getSessionStateBean().getSrcHostname());
            pwmRequest.getPwmApplication().getAuditManager().submit(auditRecord);
        }
        final RestResultBean restResultBean = RestResultBean.forSuccessMessage(pwmRequest.getLocale(), pwmRequest.getConfig(), Message.Success_Action, action.getName());
        pwmRequest.outputJsonResult(restResultBean);
        return ProcessStatus.Halt;
    } catch (PwmOperationalException e) {
        LOGGER.error(pwmRequest, e.getErrorInformation().toDebugStr());
        final RestResultBean restResultBean = RestResultBean.fromError(e.getErrorInformation(), pwmRequest);
        pwmRequest.outputJsonResult(restResultBean);
        return ProcessStatus.Halt;
    }
}
Also used : ActionExecutor(password.pwm.util.operations.ActionExecutor) UserIdentity(password.pwm.bean.UserIdentity) HelpdeskProfile(password.pwm.config.profile.HelpdeskProfile) HelpdeskAuditRecord(password.pwm.svc.event.HelpdeskAuditRecord) PwmOperationalException(password.pwm.error.PwmOperationalException) ErrorInformation(password.pwm.error.ErrorInformation) AuditRecordFactory(password.pwm.svc.event.AuditRecordFactory) ChaiUser(com.novell.ldapchai.ChaiUser) MacroMachine(password.pwm.util.macro.MacroMachine) ActionConfiguration(password.pwm.config.value.data.ActionConfiguration) PwmSession(password.pwm.http.PwmSession) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 8 with MacroMachine

use of password.pwm.util.macro.MacroMachine 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 9 with MacroMachine

use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.

the class ConfigEditorServlet method generateSettingData.

public static Map<String, Object> generateSettingData(final PwmApplication pwmApplication, final StoredConfigurationImpl storedConfiguration, final SessionLabel sessionLabel, final Locale locale) throws PwmUnrecoverableException {
    final LinkedHashMap<String, Object> returnMap = new LinkedHashMap<>();
    final MacroMachine macroMachine = MacroMachine.forNonUserSpecific(pwmApplication, sessionLabel);
    final PwmSettingTemplateSet template = storedConfiguration.getTemplateSet();
    {
        final LinkedHashMap<String, Object> settingMap = new LinkedHashMap<>();
        for (final PwmSetting setting : PwmSetting.values()) {
            settingMap.put(setting.getKey(), SettingInfo.forSetting(setting, template, macroMachine, locale));
        }
        returnMap.put("settings", settingMap);
    }
    {
        final LinkedHashMap<String, Object> categoryMap = new LinkedHashMap<>();
        for (final PwmSettingCategory category : PwmSettingCategory.values()) {
            categoryMap.put(category.getKey(), CategoryInfo.forCategory(category, macroMachine, locale));
        }
        returnMap.put("categories", categoryMap);
    }
    {
        final LinkedHashMap<String, Object> labelMap = new LinkedHashMap<>();
        for (final PwmLocaleBundle localeBundle : PwmLocaleBundle.values()) {
            final LocaleInfo localeInfo = new LocaleInfo();
            localeInfo.description = localeBundle.getTheClass().getSimpleName();
            localeInfo.key = localeBundle.toString();
            localeInfo.adminOnly = localeBundle.isAdminOnly();
            labelMap.put(localeBundle.getTheClass().getSimpleName(), localeInfo);
        }
        returnMap.put("locales", labelMap);
    }
    {
        final LinkedHashMap<String, Object> varMap = new LinkedHashMap<>();
        varMap.put("ldapProfileIds", storedConfiguration.readSetting(PwmSetting.LDAP_PROFILE_LIST).toNativeObject());
        varMap.put("currentTemplate", storedConfiguration.getTemplateSet());
        varMap.put("configurationNotes", storedConfiguration.readConfigProperty(ConfigurationProperty.NOTES));
        returnMap.put("var", varMap);
    }
    return Collections.unmodifiableMap(returnMap);
}
Also used : PwmSetting(password.pwm.config.PwmSetting) PwmSettingCategory(password.pwm.config.PwmSettingCategory) PwmSettingTemplateSet(password.pwm.config.PwmSettingTemplateSet) PwmLocaleBundle(password.pwm.i18n.PwmLocaleBundle) MacroMachine(password.pwm.util.macro.MacroMachine) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with MacroMachine

use of password.pwm.util.macro.MacroMachine in project pwm by pwm-project.

the class ConfigEditorServlet method restTestMacro.

@ActionHandler(action = "testMacro")
private ProcessStatus restTestMacro(final PwmRequest pwmRequest) throws IOException, ServletException {
    try {
        final Map<String, String> inputMap = pwmRequest.readBodyAsJsonStringMap(PwmHttpRequestWrapper.Flag.BypassValidation);
        if (inputMap == null || !inputMap.containsKey("input")) {
            pwmRequest.outputJsonResult(RestResultBean.withData("missing input"));
            return ProcessStatus.Halt;
        }
        final MacroMachine macroMachine;
        if (pwmRequest.isAuthenticated()) {
            macroMachine = pwmRequest.getPwmSession().getSessionManager().getMacroMachine(pwmRequest.getPwmApplication());
        } else {
            macroMachine = MacroMachine.forNonUserSpecific(pwmRequest.getPwmApplication(), pwmRequest.getSessionLabel());
        }
        final String input = inputMap.get("input");
        final String output = macroMachine.expandMacros(input);
        pwmRequest.outputJsonResult(RestResultBean.withData(output));
    } catch (PwmUnrecoverableException e) {
        LOGGER.error(pwmRequest, e.getErrorInformation());
        pwmRequest.respondWithError(e.getErrorInformation());
    }
    return ProcessStatus.Halt;
}
Also used : MacroMachine(password.pwm.util.macro.MacroMachine) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException)

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