/** * Sign the request using OAuth. This uses the consumer token and key * * @param string $method the method (get/put/delete/post) * @param string $url the url to sign (http://site/social/rest/people/1/@me) * @param array $params the params that should be appended to the url (count=20 fields=foo, etc) * @param string $postBody for POST/PUT requests, the postBody is included in the signature * @return string the signed url */ public function sign(apiHttpRequest $request) { // add the developer key to the request before signing it if ($this->developerKey) { $request->setUrl($request->getUrl() . (strpos($request->getUrl(), '?') === false ? '?' : '&') . 'key=' . urlencode($this->developerKey)); } // and sign the request $oauthRequest = apiClientOAuthRequest::from_request($request->getMethod(), $request->getBaseUrl(), $request->getQueryParams()); $params = $this->mergeParameters($request->getQueryParams()); foreach ($params as $key => $val) { if (is_array($val)) { $val = implode(',', $val); } $oauthRequest->set_parameter($key, $val); } $oauthRequest->sign_request($this->signatureMethod, $this->consumerToken, $this->accessToken); $authHeaders = $oauthRequest->to_header(); $headers = $request->getHeaders(); $headers[] = $authHeaders; $request->setHeaders($headers); // and add the access token key to it (since it doesn't include the secret, it's still secure to store this in cache) $request->accessKey = $this->accessToken->key; return $request; }
/** * pretty much a helper function to set up the request */ public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL) { @$parameters or $parameters = array(); $defaults = array("oauth_version" => apiClientOAuthRequest::$version, "oauth_nonce" => apiClientOAuthRequest::generate_nonce(), "oauth_timestamp" => apiClientOAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key); $parameters = array_merge($defaults, $parameters); if ($token) { $parameters['oauth_token'] = $token->key; } // oauth v1.0a /*if (isset($_REQUEST['oauth_verifier'])) { $parameters['oauth_verifier'] = $_REQUEST['oauth_verifier']; }*/ return new apiClientOAuthRequest($http_method, $http_url, $parameters); }