Search in sources :

Example 6 with PwmHttpClientResponse

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

the class OAuthMachine method makeOAuthGetAttributeRequest.

String makeOAuthGetAttributeRequest(final PwmRequest pwmRequest, final String accessToken) throws PwmUnrecoverableException {
    final Configuration config = pwmRequest.getConfig();
    final String requestUrl = settings.getAttributesUrl();
    final Map<String, String> requestParams = new HashMap<>();
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_ACCESS_TOKEN), accessToken);
    requestParams.put(config.readAppProperty(AppProperty.HTTP_PARAM_OAUTH_ATTRIBUTES), settings.getDnAttributeName());
    final PwmHttpClientResponse restResults = makeHttpRequest(pwmRequest, "OAuth getattribute", settings, requestUrl, requestParams);
    return restResults.getBody();
}
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)

Example 7 with PwmHttpClientResponse

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

the class OAuthMachine method makeHttpRequest.

private static PwmHttpClientResponse makeHttpRequest(final PwmRequest pwmRequest, final String debugText, final OAuthSettings settings, final String requestUrl, final Map<String, String> requestParams) throws PwmUnrecoverableException {
    final String requestBody = PwmURL.appendAndEncodeUrlParameters("", requestParams);
    final List<X509Certificate> certs = settings.getCertificates();
    final PwmHttpClientRequest pwmHttpClientRequest;
    {
        final Map<String, String> headers = new HashMap<>();
        headers.put(HttpHeader.Authorization.getHttpName(), new BasicAuthInfo(settings.getClientID(), settings.getSecret()).toAuthHeader());
        headers.put(HttpHeader.Content_Type.getHttpName(), HttpContentType.form.getHeaderValue());
        pwmHttpClientRequest = new PwmHttpClientRequest(HttpMethod.POST, requestUrl, requestBody, headers);
    }
    final PwmHttpClientResponse pwmHttpClientResponse;
    try {
        final PwmHttpClientConfiguration config = PwmHttpClientConfiguration.builder().certificates(JavaHelper.isEmpty(certs) ? null : certs).maskBodyDebugOutput(true).build();
        final PwmHttpClient pwmHttpClient = new PwmHttpClient(pwmRequest.getPwmApplication(), pwmRequest.getSessionLabel(), config);
        pwmHttpClientResponse = pwmHttpClient.makeRequest(pwmHttpClientRequest);
    } catch (PwmException e) {
        final String errorMsg = "error during " + debugText + " http request to oauth server, remote error: " + e.getErrorInformation().toDebugStr();
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_OAUTH_ERROR, errorMsg));
    }
    if (pwmHttpClientResponse.getStatusCode() != HttpStatus.SC_OK) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_OAUTH_ERROR, "unexpected HTTP status code (" + pwmHttpClientResponse.getStatusCode() + ") during " + debugText + " request to " + requestUrl));
    }
    return pwmHttpClientResponse;
}
Also used : PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) X509Certificate(java.security.cert.X509Certificate) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClient(password.pwm.http.client.PwmHttpClient) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) BasicAuthInfo(password.pwm.util.BasicAuthInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with PwmHttpClientResponse

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

the class ActionExecutor method executeWebserviceAction.

private void executeWebserviceAction(final SessionLabel sessionLabel, final ActionConfiguration actionConfiguration) throws PwmOperationalException, PwmUnrecoverableException {
    String url = actionConfiguration.getUrl();
    String body = actionConfiguration.getBody();
    final Map<String, String> headers = new LinkedHashMap<>();
    if (actionConfiguration.getHeaders() != null) {
        headers.putAll(actionConfiguration.getHeaders());
    }
    try {
        // expand using pwm macros
        if (settings.isExpandPwmMacros()) {
            if (settings.getMacroMachine() == null) {
                throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "executor specified macro expansion but did not supply macro machine"));
            }
            final MacroMachine macroMachine = settings.getMacroMachine();
            url = macroMachine.expandMacros(url);
            body = body == null ? "" : macroMachine.expandMacros(body);
            for (final Map.Entry<String, String> entry : headers.entrySet()) {
                final String headerName = entry.getKey();
                final String headerValue = entry.getValue();
                if (headerValue != null) {
                    headers.put(headerName, macroMachine.expandMacros(headerValue));
                }
            }
        }
        // add basic auth header;
        if (!StringUtil.isEmpty(actionConfiguration.getUsername()) && !StringUtil.isEmpty(actionConfiguration.getPassword())) {
            final String authHeaderValue = new BasicAuthInfo(actionConfiguration.getUsername(), new PasswordData(actionConfiguration.getPassword())).toAuthHeader();
            headers.put(HttpHeader.Authorization.getHttpName(), authHeaderValue);
        }
        final HttpMethod method = HttpMethod.fromString(actionConfiguration.getMethod().toString());
        final PwmHttpClientRequest clientRequest = new PwmHttpClientRequest(method, url, body, headers);
        final PwmHttpClient client;
        {
            if (actionConfiguration.getCertificates() != null) {
                final PwmHttpClientConfiguration clientConfiguration = PwmHttpClientConfiguration.builder().certificates(actionConfiguration.getCertificates()).build();
                client = new PwmHttpClient(pwmApplication, sessionLabel, clientConfiguration);
            } else {
                client = new PwmHttpClient(pwmApplication, sessionLabel);
            }
        }
        final PwmHttpClientResponse clientResponse = client.makeRequest(clientRequest);
        if (clientResponse.getStatusCode() != 200) {
            throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, "unexpected HTTP status code while calling external web service: " + clientResponse.getStatusCode() + " " + clientResponse.getStatusPhrase()));
        }
    } catch (PwmException e) {
        if (e instanceof PwmOperationalException) {
            throw (PwmOperationalException) e;
        }
        final String errorMsg = "unexpected error during API execution: " + e.getMessage();
        LOGGER.error(errorMsg);
        throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg));
    }
}
Also used : PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) LinkedHashMap(java.util.LinkedHashMap) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClient(password.pwm.http.client.PwmHttpClient) PasswordData(password.pwm.util.PasswordData) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) MacroMachine(password.pwm.util.macro.MacroMachine) BasicAuthInfo(password.pwm.util.BasicAuthInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HttpMethod(password.pwm.http.HttpMethod)

Example 9 with PwmHttpClientResponse

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

the class RestFormDataClient method invoke.

public FormDataResponseBean invoke(final FormDataRequestBean formDataRequestBean, final Locale locale) throws PwmUnrecoverableException {
    final Map<String, String> httpHeaders = new LinkedHashMap<>();
    httpHeaders.put(HttpHeader.Accept.getHttpName(), PwmConstants.AcceptValue.json.getHeaderValue());
    httpHeaders.put(HttpHeader.Content_Type.getHttpName(), HttpContentType.json.getHeaderValue());
    if (locale != null) {
        httpHeaders.put(HttpHeader.Accept_Language.getHttpName(), locale.toString());
    }
    {
        final Map<String, String> configuredHeaders = new LinkedHashMap<>(remoteWebServiceConfiguration.getHeaders());
        // add basic auth header;
        if (!StringUtil.isEmpty(remoteWebServiceConfiguration.getUsername()) && !StringUtil.isEmpty(remoteWebServiceConfiguration.getPassword())) {
            final String authHeaderValue = new BasicAuthInfo(remoteWebServiceConfiguration.getUsername(), new PasswordData(remoteWebServiceConfiguration.getPassword())).toAuthHeader();
            configuredHeaders.put(HttpHeader.Authorization.getHttpName(), authHeaderValue);
        }
        httpHeaders.putAll(configuredHeaders);
    }
    final String jsonRequestBody = JsonUtil.serialize(formDataRequestBean);
    final PwmHttpClientRequest pwmHttpClientRequest = new PwmHttpClientRequest(HttpMethod.POST, remoteWebServiceConfiguration.getUrl(), jsonRequestBody, httpHeaders);
    final PwmHttpClientResponse httpResponse;
    try {
        httpResponse = getHttpClient(pwmApplication.getConfig()).makeRequest(pwmHttpClientRequest);
        final String responseBody = httpResponse.getBody();
        LOGGER.trace("external rest call returned: " + httpResponse.getStatusPhrase() + ", body: " + responseBody);
        if (httpResponse.getStatusCode() != 200) {
            final String errorMsg = "received non-200 response code (" + httpResponse.getStatusCode() + ") when executing web-service";
            LOGGER.error(errorMsg);
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, errorMsg));
        }
        final FormDataResponseBean formDataResponseBean = JsonUtil.deserialize(responseBody, FormDataResponseBean.class);
        return formDataResponseBean;
    } catch (PwmUnrecoverableException e) {
        final String errorMsg = "http response error while executing external rest call, error: " + e.getMessage();
        LOGGER.error(errorMsg);
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, errorMsg), e);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) PasswordData(password.pwm.util.PasswordData) BasicAuthInfo(password.pwm.util.BasicAuthInfo) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with PwmHttpClientResponse

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

the class OAuthMachine method figureUsernameGrantParam.

private String figureUsernameGrantParam(final PwmRequest pwmRequest, final UserIdentity userIdentity) throws PwmUnrecoverableException {
    if (userIdentity == null) {
        return null;
    }
    final String macroText = settings.getUsernameSendValue();
    if (StringUtil.isEmpty(macroText)) {
        return null;
    }
    final MacroMachine macroMachine = MacroMachine.forUser(pwmRequest, userIdentity);
    final String username = macroMachine.expandMacros(macroText);
    LOGGER.debug(pwmRequest, "calculated username value for user as: " + username);
    final String grantUrl = settings.getLoginURL();
    final String signUrl = grantUrl.replace("/grant", "/sign");
    final Map<String, String> requestPayload;
    {
        final Map<String, String> dataPayload = new HashMap<>();
        dataPayload.put("username", username);
        final List<Map<String, String>> listWrapper = new ArrayList<>();
        listWrapper.add(dataPayload);
        requestPayload = new HashMap<>();
        requestPayload.put("data", JsonUtil.serializeCollection(listWrapper));
    }
    LOGGER.debug(pwmRequest, "preparing to send username to OAuth /sign endpoint for future injection to /grant redirect");
    final PwmHttpClientResponse restResults = makeHttpRequest(pwmRequest, "OAuth pre-inject username signing service", settings, signUrl, requestPayload);
    final String resultBody = restResults.getBody();
    final Map<String, String> resultBodyMap = JsonUtil.deserializeStringMap(resultBody);
    final String data = resultBodyMap.get("data");
    LOGGER.debug(pwmRequest, "oauth /sign endpoint returned signed username data: " + data);
    return data;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MacroMachine(password.pwm.util.macro.MacroMachine) ArrayList(java.util.ArrayList) List(java.util.List) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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