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();
}
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;
}
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));
}
}
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);
}
}
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;
}
Aggregations