You are here

public function HttpClientOAuth::authenticate in Http Client 7.2

Same name and namespace in other branches
  1. 6.2 includes/HttpClientOAuth.inc \HttpClientOAuth::authenticate()

Used by the HttpClient to authenticate requests.

Parameters

HttpClientRequest $request:

Return value

void

Overrides HttpClientAuthentication::authenticate

File

includes/auth/oauth/HttpClientOAuth.inc, line 41

Class

HttpClientOAuth

Code

public function authenticate($request) {

  // Create a OAuth request object.
  $req = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $request->method, $request->url, $request->parameters);
  if ($this->hash_body) {

    // Add a body hash if applicable.
    $content_type = $request
      ->getHeader('Content-type', TRUE);
    if (in_array($request->method, array(
      'POST',
      'PUT',
    )) && $content_type !== 'application/x-www-form-urlencoded') {
      $data = $request->data;
      $data || ($data = '');
      $req
        ->set_parameter('oauth_body_hash', base64_encode(sha1($data, TRUE)));
    }
  }

  // Sign the request if we can and should.
  if ($this->sign && $this->signImpl) {
    $req
      ->sign_request($this->signImpl, $this->consumer, $this->token);
  }

  // Make sure that we use the normalized url for the request
  $request->url = $req
    ->get_normalized_http_url();

  // Transfer the parameters to the request objects
  foreach ($req
    ->get_parameters() as $key => $val) {
    if (!$this->header_auth || substr($key, 0, 5) != 'oauth') {
      $request->parameters[$key] = $val;
    }
  }
  if ($this->header_auth) {
    $auth_header = explode(':', $req
      ->to_header($this->realm), 2);
    $request
      ->setHeader($auth_header[0], trim($auth_header[1]));
  }
}