/** * Get key * * @param string $consumerSecret Consumer secret value * @param string $tokenSecret Token secret value (if exists) * * @return string Signature key */ public function getKey($consumerSecret, $tokenSecret = '') { $secrets = array($consumerSecret, $tokenSecret); $key = implode('&', HTTP_OAuth::urlencode($secrets)); $this->debug('Signing with key: ' . $key); return $key; }
/** * Creates OAuth header * * Given the passed in OAuth parameters, put them together * in a formated string for a Authorization header. * * @param array $params OAuth parameters * * @return void */ protected function getAuthForHeader(array $params) { $url = $this->getUrl(); $realm = $url->getScheme() . '://' . $url->getHost() . '/'; $header = 'OAuth realm="' . $realm . '"'; foreach ($params as $name => $value) { $header .= ", " . HTTP_OAuth::urlencode($name) . '="' . HTTP_OAuth::urlencode($value) . '"'; } return $header; }
public function testBuild() { $signature = new HTTP_OAuth_Signature_HMAC_SHA1(); $result = $signature->build('POST', 'http://twitter.com/oauth/request_token', array('oauth_consumer_key' => 'e1nTvIGVCPkbfqZdIE7OyA', 'oauth_nonce' => '5319B2C4-92DD-4568-B34C-993C5A102B2D', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => '1245709445', 'oauth_version' => '1.0'), 's85GLpyelma8rvNCgOjxi3lBXoedqsoDas6OYIQCeI'); $this->assertEquals('6vdoM0LiiLr%2FjqcZqIE5Nq3I8Dc%3D', HTTP_OAuth::urlencode($result)); }
/** * Send an OAuth signed request with a body to the API * * @param string $url The URL to send the request to * @param string $body The raw body to PUT/POST to the URL * @param string $method The HTTP method to use (POST or PUT) * * @return object Instance of {@link HTTP_Request2_Response} * @see http://bit.ly/cdZGfr */ private function _sendRequestWithBody($url, $body, $method = "PUT") { static $map = array('PUT' => HTTP_Request2::METHOD_PUT, 'POST' => HTTP_Request2::METHOD_POST); if (array_key_exists($method, $map)) { $method = $map[$method]; } else { throw new Services_SimpleGeo_Exception('Invalid HTTP method ' . $method); } $signatureMethod = $this->_oauth->getSignatureMethod(); $params = array('oauth_nonce' => (string) rand(0, 100000000), 'oauth_timestamp' => time(), 'oauth_consumer_key' => $this->_oauth->getKey(), 'oauth_signature_method' => $signatureMethod, 'oauth_version' => '1.0'); $sig = HTTP_OAuth_Signature::factory($signatureMethod); $params['oauth_signature'] = $sig->build($method, $url, $params, $this->_secret); // Build the header $header = 'OAuth realm="' . $this->_api . '"'; foreach ($params as $name => $value) { $header .= ", " . HTTP_OAuth::urlencode($name) . '="' . HTTP_OAuth::urlencode($value) . '"'; } $req = new HTTP_Request2(new Net_URL2($url), $method); $req->setHeader('Authorization', $header); $req->setBody($body); try { $result = $req->send(); } catch (Exception $e) { throw new Services_SimpleGeo_Exception($e->getMessage(), $e->getCode()); } $check = (int) substr($result->getStatus(), 0, 1); if ($check !== 2) { $body = @json_decode($result->getBody()); throw new Services_SimpleGeo_Exception($body->message, $result->getStatus()); } return $result; }
private function build_header($tweet = false) { $consumer = TwitPic_Config::getConsumer(); $oauth = TwitPic_Config::getOAuth(); $signature = HTTP_OAuth_Signature::factory('HMAC_SHA1'); $timestamp = gmdate('U'); $nonce = uniqid(); $version = '1.0'; if (is_string($tweet)) { $params = array('oauth_consumer_key' => $consumer['key'], 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $oauth['token'], 'oauth_timestamp' => $timestamp, 'oauth_nonce' => $nonce, 'oauth_version' => $version, 'status' => $tweet); $sig_text = $signature->build('POST', "http://api.twitter.com/1/statuses/update.{$this->format}", $params, $consumer['secret'], $oauth['secret']); $params['oauth_signature'] = $sig_text; } else { $params = array('oauth_consumer_key' => $consumer['key'], 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $oauth['token'], 'oauth_timestamp' => $timestamp, 'oauth_nonce' => $nonce, 'oauth_version' => $version); $sig_text = $signature->build('GET', 'https://api.twitter.com/1/account/verify_credentials.json', $params, $consumer['secret'], $oauth['secret']); $params['oauth_signature'] = $sig_text; } $realm = 'http://api.twitter.com/'; $header = 'OAuth realm="' . $realm . '"'; foreach ($params as $name => $value) { $header .= ", " . HTTP_OAuth::urlencode($name) . '="' . HTTP_OAuth::urlencode($value) . '"'; } return $header; }
public function testScalarCheck() { $o = HTTP_OAuth::urlencode(new stdClass()); $this->assertInstanceOf('stdClass', $o); }