Search in sources :

Example 1 with PwmHttpClientResponse

use of password.pwm.http.client.PwmHttpClientResponse in project pwm by pwm-project.

the class ApplianceStatusChecker method readApplianceHealthStatus.

private List<HealthRecord> readApplianceHealthStatus(final PwmApplication pwmApplication) throws IOException, PwmUnrecoverableException, PwmOperationalException {
    final List<HealthRecord> healthRecords = new ArrayList<>();
    final String url = figureUrl(pwmApplication);
    final Map<String, String> requestHeaders = Collections.singletonMap("sspr-authorization-token", getApplianceAccessToken(pwmApplication));
    final PwmHttpClientConfiguration pwmHttpClientConfiguration = PwmHttpClientConfiguration.builder().promiscuous(true).build();
    final PwmHttpClient pwmHttpClient = new PwmHttpClient(pwmApplication, SessionLabel.HEALTH_SESSION_LABEL, pwmHttpClientConfiguration);
    final PwmHttpClientRequest pwmHttpClientRequest = new PwmHttpClientRequest(HttpMethod.GET, url, null, requestHeaders);
    final PwmHttpClientResponse response = pwmHttpClient.makeRequest(pwmHttpClientRequest);
    LOGGER.trace(SessionLabel.HEALTH_SESSION_LABEL, "https response from appliance server request: " + response.getBody());
    final String jsonString = response.getBody();
    LOGGER.debug("response from /sspr/appliance-update-status: " + jsonString);
    final UpdateStatus updateStatus = JsonUtil.deserialize(jsonString, UpdateStatus.class);
    if (updateStatus.pendingInstallation) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.Appliance_PendingUpdates));
    }
    if (!updateStatus.autoUpdatesEnabled) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.Appliance_UpdatesNotEnabled));
    }
    if (!updateStatus.updateServiceConfigured) {
        healthRecords.add(HealthRecord.forMessage(HealthMessage.Appliance_UpdateServiceNotConfigured));
    }
    return healthRecords;
}
Also used : PwmHttpClient(password.pwm.http.client.PwmHttpClient) PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) ArrayList(java.util.ArrayList) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse)

Example 2 with PwmHttpClientResponse

use of password.pwm.http.client.PwmHttpClientResponse 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 3 with PwmHttpClientResponse

use of password.pwm.http.client.PwmHttpClientResponse in project pwm by pwm-project.

the class OAuthMachine method makeOAuthRefreshRequest.

private OAuthResolveResults makeOAuthRefreshRequest(final PwmRequest pwmRequest, final String refreshCode) throws PwmUnrecoverableException {
    final Configuration config = pwmRequest.getConfig();
    final String requestUrl = settings.getCodeResolveUrl();
    final String grantType = config.readAppProperty(AppProperty.OAUTH_ID_REFRESH_GRANT_TYPE);
    final Map<String, String> requestParams = new HashMap<>();
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_REFRESH_TOKEN), refreshCode);
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_GRANT_TYPE), grantType);
    final PwmHttpClientResponse restResults = makeHttpRequest(pwmRequest, "OAuth refresh resolver", settings, requestUrl, requestParams);
    final String resolveResponseBodyStr = restResults.getBody();
    final Map<String, String> resolveResultValues = JsonUtil.deserializeStringMap(resolveResponseBodyStr);
    final OAuthResolveResults oAuthResolveResults = new OAuthResolveResults();
    oAuthResolveResults.setAccessToken(resolveResultValues.get(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_ACCESS_TOKEN)));
    oAuthResolveResults.setRefreshToken(refreshCode);
    oAuthResolveResults.setExpiresSeconds(0);
    try {
        oAuthResolveResults.setExpiresSeconds(Integer.parseInt(resolveResultValues.get(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_EXPIRES))));
    } catch (Exception e) {
        LOGGER.warn(pwmRequest, "error parsing oauth expires value in resolve request: " + e.getMessage());
    }
    return oAuthResolveResults;
}
Also used : Configuration(password.pwm.config.Configuration) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) URISyntaxException(java.net.URISyntaxException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException)

Example 4 with PwmHttpClientResponse

use of password.pwm.http.client.PwmHttpClientResponse in project pwm by pwm-project.

the class CaptchaUtility method verifyReCaptcha.

public static boolean verifyReCaptcha(final PwmRequest pwmRequest, final String recaptchaResponse) throws PwmUnrecoverableException {
    if (!captchaEnabledForRequest(pwmRequest)) {
        return true;
    }
    final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
    final PasswordData privateKey = pwmApplication.getConfig().readSettingAsPassword(PwmSetting.RECAPTCHA_KEY_PRIVATE);
    final StringBuilder bodyText = new StringBuilder();
    bodyText.append("secret=").append(privateKey.getStringValue());
    bodyText.append("&");
    bodyText.append("remoteip=").append(pwmRequest.getSessionLabel().getSrcAddress());
    bodyText.append("&");
    bodyText.append("response=").append(recaptchaResponse);
    try {
        final PwmHttpClientRequest clientRequest = new PwmHttpClientRequest(HttpMethod.POST, pwmApplication.getConfig().readAppProperty(AppProperty.RECAPTCHA_VALIDATE_URL), bodyText.toString(), Collections.singletonMap("Content-Type", HttpContentType.form.getHeaderValue()));
        LOGGER.debug(pwmRequest, "sending reCaptcha verification request");
        final PwmHttpClient client = new PwmHttpClient(pwmRequest.getPwmApplication(), pwmRequest.getSessionLabel());
        final PwmHttpClientResponse clientResponse = client.makeRequest(clientRequest);
        if (clientResponse.getStatusCode() != HttpServletResponse.SC_OK) {
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_CAPTCHA_API_ERROR, "unexpected HTTP status code (" + clientResponse.getStatusCode() + ")"));
        }
        final JsonElement responseJson = new JsonParser().parse(clientResponse.getBody());
        final JsonObject topObject = responseJson.getAsJsonObject();
        if (topObject != null && topObject.has("success")) {
            final boolean success = topObject.get("success").getAsBoolean();
            if (success) {
                writeCaptchaSkipCookie(pwmRequest);
                return true;
            }
            if (topObject.has("error-codes")) {
                final List<String> errorCodes = new ArrayList<>();
                for (final JsonElement element : topObject.get("error-codes").getAsJsonArray()) {
                    final String errorCode = element.getAsString();
                    errorCodes.add(errorCode);
                }
                LOGGER.debug(pwmRequest, "recaptcha error codes: " + JsonUtil.serializeCollection(errorCodes));
            }
        }
    } catch (Exception e) {
        final String errorMsg = "unexpected error during reCaptcha API execution: " + e.getMessage();
        LOGGER.error(errorMsg, e);
        final ErrorInformation errorInfo = new ErrorInformation(PwmError.ERROR_CAPTCHA_API_ERROR, errorMsg);
        final PwmUnrecoverableException pwmE = new PwmUnrecoverableException(errorInfo);
        pwmE.initCause(e);
        throw pwmE;
    }
    return false;
}
Also used : PwmApplication(password.pwm.PwmApplication) PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) IOException(java.io.IOException) ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClient(password.pwm.http.client.PwmHttpClient) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 5 with PwmHttpClientResponse

use of password.pwm.http.client.PwmHttpClientResponse in project pwm by pwm-project.

the class OAuthMachine method makeOAuthResolveRequest.

OAuthResolveResults makeOAuthResolveRequest(final PwmRequest pwmRequest, final String requestCode) throws PwmUnrecoverableException {
    final Configuration config = pwmRequest.getConfig();
    final String requestUrl = settings.getCodeResolveUrl();
    final String grantType = config.readAppProperty(AppProperty.OAUTH_ID_ACCESS_GRANT_TYPE);
    final String redirectUri = figureOauthSelfEndPointUrl(pwmRequest);
    final String clientID = settings.getClientID();
    final Map<String, String> requestParams = new HashMap<>();
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_CODE), requestCode);
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_GRANT_TYPE), grantType);
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_REDIRECT_URI), redirectUri);
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_CLIENT_ID), clientID);
    final PwmHttpClientResponse restResults = makeHttpRequest(pwmRequest, "oauth code resolver", settings, requestUrl, requestParams);
    final String resolveResponseBodyStr = restResults.getBody();
    final Map<String, String> resolveResultValues = JsonUtil.deserializeStringMap(resolveResponseBodyStr);
    final OAuthResolveResults oAuthResolveResults = new OAuthResolveResults();
    oAuthResolveResults.setAccessToken(resolveResultValues.get(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_ACCESS_TOKEN)));
    oAuthResolveResults.setRefreshToken(resolveResultValues.get(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_REFRESH_TOKEN)));
    oAuthResolveResults.setExpiresSeconds(0);
    try {
        oAuthResolveResults.setExpiresSeconds(Integer.parseInt(resolveResultValues.get(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_EXPIRES))));
    } catch (Exception e) {
        LOGGER.warn(pwmRequest, "error parsing oauth expires value in code resolver response from server at " + requestUrl + ", error: " + e.getMessage());
    }
    return oAuthResolveResults;
}
Also used : Configuration(password.pwm.config.Configuration) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) URISyntaxException(java.net.URISyntaxException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException)

Aggregations

PwmHttpClientResponse (password.pwm.http.client.PwmHttpClientResponse)10 LinkedHashMap (java.util.LinkedHashMap)8 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)7 PwmHttpClientConfiguration (password.pwm.http.client.PwmHttpClientConfiguration)6 PwmHttpClientRequest (password.pwm.http.client.PwmHttpClientRequest)6 HashMap (java.util.HashMap)5 ErrorInformation (password.pwm.error.ErrorInformation)5 PwmException (password.pwm.error.PwmException)5 Map (java.util.Map)4 PwmHttpClient (password.pwm.http.client.PwmHttpClient)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Configuration (password.pwm.config.Configuration)3 BasicAuthInfo (password.pwm.util.BasicAuthInfo)3 MacroMachine (password.pwm.util.macro.MacroMachine)3 URISyntaxException (java.net.URISyntaxException)2 PasswordData (password.pwm.util.PasswordData)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1