Search in sources :

Example 1 with PublicUserInfoBean

use of password.pwm.bean.pub.PublicUserInfoBean in project pwm by pwm-project.

the class RestStatusServer method doGetStatusData.

@RestMethodHandler(method = HttpMethod.GET, produces = HttpContentType.json, consumes = HttpContentType.json)
public RestResultBean doGetStatusData(final RestRequest restRequest) throws PwmUnrecoverableException {
    final Instant startTime = Instant.now();
    final String username = restRequest.readParameterAsString("username");
    final TargetUserIdentity targetUserIdentity = RestUtility.resolveRequestedUsername(restRequest, username);
    try {
        final ChaiProvider chaiProvider = targetUserIdentity.getChaiProvider();
        final UserInfo userInfo = UserInfoFactory.newUserInfo(restRequest.getPwmApplication(), restRequest.getSessionLabel(), restRequest.getLocale(), targetUserIdentity.getUserIdentity(), chaiProvider);
        final MacroMachine macroMachine = MacroMachine.forUser(restRequest.getPwmApplication(), restRequest.getLocale(), restRequest.getSessionLabel(), targetUserIdentity.getUserIdentity());
        final PublicUserInfoBean publicUserInfoBean = PublicUserInfoBean.fromUserInfoBean(userInfo, restRequest.getPwmApplication().getConfig(), restRequest.getLocale(), macroMachine);
        StatisticsManager.incrementStat(restRequest.getPwmApplication(), Statistic.REST_STATUS);
        final RestResultBean restResultBean = RestResultBean.withData(publicUserInfoBean);
        LOGGER.debug(restRequest.getSessionLabel(), "completed REST status request in " + TimeDuration.compactFromCurrent(startTime) + ", result=" + JsonUtil.serialize(restResultBean));
        return restResultBean;
    } catch (PwmException e) {
        return RestResultBean.fromError(e.getErrorInformation());
    } catch (Exception e) {
        final String errorMsg = "unexpected error building json response: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) ChaiProvider(com.novell.ldapchai.provider.ChaiProvider) Instant(java.time.Instant) MacroMachine(password.pwm.util.macro.MacroMachine) UserInfo(password.pwm.ldap.UserInfo) PublicUserInfoBean(password.pwm.bean.pub.PublicUserInfoBean) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) RestResultBean(password.pwm.ws.server.RestResultBean) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Example 2 with PublicUserInfoBean

use of password.pwm.bean.pub.PublicUserInfoBean in project pwm by pwm-project.

the class RestTokenDataClient method invoke.

private TokenDestinationData invoke(final SessionLabel sessionLabel, final TokenDestinationData tokenDestinationData, final UserIdentity userIdentity, final String url, final Locale locale) throws PwmOperationalException, ChaiUnavailableException, PwmUnrecoverableException {
    if (tokenDestinationData == null) {
        throw new NullPointerException("tokenDestinationData can not be null");
    }
    final Map<String, Object> sendData = new LinkedHashMap<>();
    sendData.put(DATA_KEY_TOKENDATA, tokenDestinationData);
    if (userIdentity != null) {
        final UserInfo userInfo = UserInfoFactory.newUserInfoUsingProxy(pwmApplication, sessionLabel, userIdentity, locale);
        final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, PwmConstants.DEFAULT_LOCALE, SessionLabel.SYSTEM_LABEL, userInfo.getUserIdentity());
        final PublicUserInfoBean publicUserInfoBean = PublicUserInfoBean.fromUserInfoBean(userInfo, pwmApplication.getConfig(), PwmConstants.DEFAULT_LOCALE, macroMachine);
        sendData.put(RestClient.DATA_KEY_USERINFO, publicUserInfoBean);
    }
    final String jsonRequestData = JsonUtil.serializeMap(sendData);
    final String responseBody = RestClientHelper.makeOutboundRestWSCall(pwmApplication, locale, url, jsonRequestData);
    return JsonUtil.deserialize(responseBody, TokenDestinationData.class);
}
Also used : MacroMachine(password.pwm.util.macro.MacroMachine) UserInfo(password.pwm.ldap.UserInfo) PublicUserInfoBean(password.pwm.bean.pub.PublicUserInfoBean) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with PublicUserInfoBean

use of password.pwm.bean.pub.PublicUserInfoBean in project pwm by pwm-project.

the class PwmPasswordRuleValidator method invokeExternalRuleMethods.

public List<ErrorInformation> invokeExternalRuleMethods(final Configuration config, final PwmPasswordPolicy pwmPasswordPolicy, final PasswordData password, final UserInfo userInfo) throws PwmUnrecoverableException {
    final List<ErrorInformation> returnedErrors = new ArrayList<>();
    final String restURL = config.readSettingAsString(PwmSetting.EXTERNAL_PWCHECK_REST_URLS);
    final boolean haltOnError = Boolean.parseBoolean(config.readAppProperty(AppProperty.WS_REST_CLIENT_PWRULE_HALTONERROR));
    final Map<String, Object> sendData = new LinkedHashMap<>();
    if (restURL == null || restURL.isEmpty()) {
        return Collections.emptyList();
    }
    {
        final String passwordStr = password == null ? "" : password.getStringValue();
        sendData.put("password", passwordStr);
    }
    if (pwmPasswordPolicy != null) {
        final LinkedHashMap<String, Object> policyData = new LinkedHashMap<>();
        for (final PwmPasswordRule rule : PwmPasswordRule.values()) {
            policyData.put(rule.name(), pwmPasswordPolicy.getValue(rule));
        }
        sendData.put("policy", policyData);
    }
    if (userInfo != null) {
        final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, PwmConstants.DEFAULT_LOCALE, SessionLabel.SYSTEM_LABEL, userInfo.getUserIdentity());
        final PublicUserInfoBean publicUserInfoBean = PublicUserInfoBean.fromUserInfoBean(userInfo, pwmApplication.getConfig(), locale, macroMachine);
        sendData.put("userInfo", publicUserInfoBean);
    }
    final String jsonRequestBody = JsonUtil.serializeMap(sendData);
    try {
        final String responseBody = RestClientHelper.makeOutboundRestWSCall(pwmApplication, locale, restURL, jsonRequestBody);
        final Map<String, Object> responseMap = JsonUtil.deserialize(responseBody, new TypeToken<Map<String, Object>>() {
        });
        if (responseMap.containsKey(REST_RESPONSE_KEY_ERROR) && Boolean.parseBoolean(responseMap.get(REST_RESPONSE_KEY_ERROR).toString())) {
            if (responseMap.containsKey(REST_RESPONSE_KEY_ERROR_MSG)) {
                final String errorMessage = responseMap.get(REST_RESPONSE_KEY_ERROR_MSG).toString();
                LOGGER.trace("external web service reported error: " + errorMessage);
                returnedErrors.add(new ErrorInformation(PwmError.PASSWORD_CUSTOM_ERROR, errorMessage, errorMessage, null));
            } else {
                LOGGER.trace("external web service reported error without specifying an errorMessage");
                returnedErrors.add(new ErrorInformation(PwmError.PASSWORD_CUSTOM_ERROR));
            }
        } else {
            LOGGER.trace("external web service did not report an error");
        }
    } catch (PwmOperationalException e) {
        final String errorMsg = "error executing external rule REST call: " + e.getMessage();
        LOGGER.error(errorMsg);
        if (haltOnError) {
            throw new PwmUnrecoverableException(e.getErrorInformation(), e);
        }
        throw new IllegalStateException("http response error code: " + e.getMessage());
    }
    return returnedErrors;
}
Also used : ArrayList(java.util.ArrayList) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PublicUserInfoBean(password.pwm.bean.pub.PublicUserInfoBean) LinkedHashMap(java.util.LinkedHashMap) PwmOperationalException(password.pwm.error.PwmOperationalException) ErrorInformation(password.pwm.error.ErrorInformation) PwmPasswordRule(password.pwm.config.profile.PwmPasswordRule) MacroMachine(password.pwm.util.macro.MacroMachine) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with PublicUserInfoBean

use of password.pwm.bean.pub.PublicUserInfoBean in project pwm by pwm-project.

the class ExternalRestMacro method replaceValue.

public String replaceValue(final String matchValue, final MacroRequestInfo macroRequestInfo) {
    final PwmApplication pwmApplication = macroRequestInfo.getPwmApplication();
    final UserInfo userInfoBean = macroRequestInfo.getUserInfo();
    final String inputString = matchValue.substring(11, matchValue.length() - 1);
    final Map<String, Object> sendData = new HashMap<>();
    try {
        if (userInfoBean != null) {
            final MacroMachine macroMachine = MacroMachine.forUser(pwmApplication, PwmConstants.DEFAULT_LOCALE, SessionLabel.SYSTEM_LABEL, userInfoBean.getUserIdentity());
            final PublicUserInfoBean publicUserInfoBean = PublicUserInfoBean.fromUserInfoBean(userInfoBean, pwmApplication.getConfig(), PwmConstants.DEFAULT_LOCALE, macroMachine);
            sendData.put("userInfo", publicUserInfoBean);
        }
        sendData.put("input", inputString);
        final String requestBody = JsonUtil.serializeMap(sendData);
        final String responseBody = RestClientHelper.makeOutboundRestWSCall(pwmApplication, PwmConstants.DEFAULT_LOCALE, url, requestBody);
        final Map<String, Object> responseMap = JsonUtil.deserialize(responseBody, new TypeToken<Map<String, Object>>() {
        });
        if (responseMap.containsKey("output")) {
            return responseMap.get("output").toString();
        } else {
            return "";
        }
    } catch (PwmException e) {
        final String errorMsg = "error while executing external macro '" + matchValue + "', error: " + e.getMessage();
        LOGGER.error(errorMsg);
        throw new IllegalStateException(errorMsg);
    }
}
Also used : PwmException(password.pwm.error.PwmException) PwmApplication(password.pwm.PwmApplication) HashMap(java.util.HashMap) UserInfo(password.pwm.ldap.UserInfo) HashMap(java.util.HashMap) Map(java.util.Map) PublicUserInfoBean(password.pwm.bean.pub.PublicUserInfoBean)

Aggregations

PublicUserInfoBean (password.pwm.bean.pub.PublicUserInfoBean)4 UserInfo (password.pwm.ldap.UserInfo)3 MacroMachine (password.pwm.util.macro.MacroMachine)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ErrorInformation (password.pwm.error.ErrorInformation)2 PwmException (password.pwm.error.PwmException)2 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)2 ChaiProvider (com.novell.ldapchai.provider.ChaiProvider)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PwmApplication (password.pwm.PwmApplication)1 PwmPasswordRule (password.pwm.config.profile.PwmPasswordRule)1 PwmOperationalException (password.pwm.error.PwmOperationalException)1 RestMethodHandler (password.pwm.ws.server.RestMethodHandler)1 RestResultBean (password.pwm.ws.server.RestResultBean)1