Java Code Examples for org.apache.http.message.HeaderGroup#addHeader()

The following examples show how to use org.apache.http.message.HeaderGroup#addHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InputStreamTestMocks.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
public static HeaderGroup toHeaderGroup(String[] read) {
	HeaderGroup ret = new HeaderGroup();
	for (String s : read) {
		String[] p = s.split(":", 2);
		if (p.length != 2) continue;
		ret.addHeader(new BasicHeader(p[0].trim(), p[1].trim()));
	}
	return ret;
}
 
Example 2
Source File: SessionUtil.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
/**
 * Given access token, query IDP URL snowflake app to get SAML response
 * We also need to perform important client side validation:
 * validate the post back url come back with the SAML response
 * contains the same prefix as the Snowflake's server url, which is the
 * intended destination url to Snowflake.
 * Explanation:
 * This emulates the behavior of IDP initiated login flow in the user
 * browser where the IDP instructs the browser to POST the SAML
 * assertion to the specific SP endpoint.  This is critical in
 * preventing a SAML assertion issued to one SP from being sent to
 * another SP.
 *
 * @param loginInput   Login Info for the request
 * @param ssoUrl       URL to use for SSO
 * @param oneTimeToken The token used for SSO
 * @return The response in HTML form
 * @throws SnowflakeSQLException Will be thrown if the destination URL in
 *                               the SAML assertion does not match
 */
private static String federatedFlowStep4(
    SFLoginInput loginInput,
    String ssoUrl,
    String oneTimeToken) throws SnowflakeSQLException
{
  String responseHtml = "";
  try
  {

    final URL url = new URL(ssoUrl);
    URI oktaGetUri = new URIBuilder()
        .setScheme(url.getProtocol())
        .setHost(url.getHost())
        .setPath(url.getPath())
        .setParameter("RelayState", "%2Fsome%2Fdeep%2Flink")
        .setParameter("onetimetoken", oneTimeToken).build();
    HttpGet httpGet = new HttpGet(oktaGetUri);

    HeaderGroup headers = new HeaderGroup();
    headers.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "*/*"));
    httpGet.setHeaders(headers.getAllHeaders());

    responseHtml = HttpUtil.executeGeneralRequest(
        httpGet,
        loginInput.getLoginTimeout(),
        loginInput.getOCSPMode());

    // step 5
    String postBackUrl = getPostBackUrlFromHTML(responseHtml);
    if (!isPrefixEqual(postBackUrl, loginInput.getServerUrl()))
    {
      logger.debug("The specified authenticator {} and the destination URL " +
                   "in the SAML assertion {} do not match.",
                   loginInput.getAuthenticator(), postBackUrl);
      throw new SnowflakeSQLException(
          SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,
          ErrorCode.IDP_INCORRECT_DESTINATION.getMessageCode());
    }
  }
  catch (IOException | URISyntaxException ex)
  {
    handleFederatedFlowError(loginInput, ex);
  }
  return responseHtml;
}
 
Example 3
Source File: SessionUtil.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
/**
 * Query IDP token url to authenticate and retrieve access token
 *
 * @param loginInput The login info for the request
 * @param tokenUrl   The URL used to retrieve the access token
 * @return Returns the one time token
 * @throws SnowflakeSQLException Will be thrown if the execute request fails
 */
private static String federatedFlowStep3(SFLoginInput loginInput, String tokenUrl)
throws SnowflakeSQLException

{
  String oneTimeToken = "";
  try
  {
    URL url = new URL(tokenUrl);
    URI tokenUri = url.toURI();
    final HttpPost postRequest = new HttpPost(tokenUri);

    String userName;
    if (Strings.isNullOrEmpty(loginInput.getOKTAUserName()))
    {
      userName = loginInput.getUserName();
    }
    else
    {
      userName = loginInput.getOKTAUserName();
    }
    StringEntity params = new StringEntity("{\"username\":\"" +
                                           userName + "\",\"password\":\"" +
                                           loginInput.getPassword() + "\"}");
    postRequest.setEntity(params);

    HeaderGroup headers = new HeaderGroup();
    headers.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "application/json"));
    headers.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
    postRequest.setHeaders(headers.getAllHeaders());

    final String idpResponse = HttpUtil.executeRequestWithoutCookies(
        postRequest,
        loginInput.getLoginTimeout(),
        0,
        null,
        loginInput.getOCSPMode());

    logger.debug("user is authenticated against {}.",
                 loginInput.getAuthenticator());

    // session token is in the data field of the returned json response
    final JsonNode jsonNode = mapper.readTree(idpResponse);
    oneTimeToken = jsonNode.get("cookieToken").asText();
  }
  catch (IOException | URISyntaxException ex)
  {
    handleFederatedFlowError(loginInput, ex);
  }
  return oneTimeToken;
}
 
Example 4
Source File: WarcHeader.java    From BUbiNG with Apache License 2.0 2 votes vote down vote up
/**
 * Adds the given header, if not present (otherwise does nothing).
 *
 * @param headers the headers where to add the new one.
 * @param name the name of the header to add.
 * @param value the value of the header to add.
 */
public static void addIfNotPresent(final HeaderGroup headers, final WarcHeader.Name name, final String value) {
	if (! headers.containsHeader(name.value)) headers.addHeader(new WarcHeader(name, value));
}