HttpClientOAuth.inc in Http Client 6.2
File
includes/HttpClientOAuth.inc
View source
<?php
class HttpClientOAuth implements HttpClientAuthentication {
private $consumer;
private $token;
private $sign;
private $signImpl;
private $hash_body;
private $header_auth;
private $realm;
public function __construct($consumer, $token = NULL, $sign_impl = NULL, $hash_body = TRUE, $header_auth = FALSE, $realm = NULL) {
$this->consumer = $consumer;
$this->token = $token;
$this->signImpl = $sign_impl;
$this->sign = is_object($sign_impl);
$this->hash_body = $hash_body;
$this->header_auth = $header_auth;
$this->realm = $realm;
}
public function signRequests($sign) {
$this->sign = $sign;
}
public function authenticate($request) {
$req = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $request->method, $request->url, $request->parameters);
if ($this->hash_body) {
$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)));
}
}
if ($this->sign && $this->signImpl) {
$req
->sign_request($this->signImpl, $this->consumer, $this->token);
}
$request->url = $req
->get_normalized_http_url();
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]));
}
}
}