10 defined(
'JPATH_PLATFORM') or die();
31 protected $token = array();
71 $this->options = isset($options) ? $options :
new JRegistry;
74 $this->application = isset($application) ? $application :
new JApplicationWeb;
75 $this->version = isset($version) ? $version :
'1.0a';
86 public function authenticate()
91 $response = $this->verifyCredentials();
104 if (strcmp($this->version,
'1.0a') === 0)
106 $verifier = $this->input->get(
'oauth_verifier');
110 $verifier = $this->input->get(
'oauth_token');
113 if (empty($verifier))
116 $this->_generateRequestToken();
128 $this->token = array(
'key' => $session->get(
'key', null,
'oauth_token'),
'secret' => $session->get(
'secret', null,
'oauth_token'));
131 if (strcmp($this->token[
'key'], $this->input->get(
'oauth_token')) !== 0)
133 throw new DomainException(
'Bad session!');
137 if (strcmp($this->version,
'1.0a') === 0)
139 $this->token[
'verifier'] = $this->input->get(
'oauth_verifier');
143 $this->_generateAccessToken();
158 private function _generateRequestToken()
161 if ($this->getOption(
'callback'))
164 'oauth_callback' => $this->getOption(
'callback')
169 $parameters = array();
173 $response = $this->oauthRequest($this->getOption(
'requestTokenURL'),
'POST', $parameters);
175 parse_str($response->body, $params);
177 if (strcmp($this->version,
'1.0a') === 0 && strcmp($params[
'oauth_callback_confirmed'],
'true') !== 0)
179 throw new DomainException(
'Bad request token!');
183 $this->token = array(
'key' => $params[
'oauth_token'],
'secret' => $params[
'oauth_token_secret']);
187 $session->set(
'key', $this->token[
'key'],
'oauth_token');
188 $session->set(
'secret', $this->token[
'secret'],
'oauth_token');
198 private function _authorise()
200 $url = $this->getOption(
'authoriseURL') .
'?oauth_token=' . $this->token[
'key'];
202 if ($this->getOption(
'scope'))
204 $scope = is_array($this->getOption(
'scope')) ? implode(
' ', $this->getOption(
'scope')) : $this->getOption(
'scope');
205 $url .=
'&scope=' . urlencode($scope);
208 if ($this->getOption(
'sendheaders'))
210 $this->application->redirect($url);
221 private function _generateAccessToken()
225 'oauth_token' => $this->token[
'key']
228 if (strcmp($this->version,
'1.0a') === 0)
230 $parameters = array_merge($parameters, array(
'oauth_verifier' => $this->token[
'verifier']));
234 $response = $this->oauthRequest($this->getOption(
'accessTokenURL'),
'POST', $parameters);
236 parse_str($response->body, $params);
239 $this->token = array(
'key' => $params[
'oauth_token'],
'secret' => $params[
'oauth_token_secret']);
256 public function oauthRequest($url, $method, $parameters, $data = array(), $headers = array())
260 'oauth_consumer_key' => $this->getOption(
'consumer_key'),
261 'oauth_signature_method' =>
'HMAC-SHA1',
262 'oauth_version' =>
'1.0',
263 'oauth_nonce' => $this->generateNonce(),
264 'oauth_timestamp' => time()
267 $parameters = array_merge($parameters, $defaults);
270 if (isset($headers[
'Content-Type']) && strpos($headers[
'Content-Type'],
'multipart/form-data') !==
false || !is_array($data))
272 $oauth_headers = $parameters;
277 $oauth_headers = array_merge($parameters, $data);
281 $oauth_headers = $this->_signRequest($url, $method, $oauth_headers);
286 $oauth_headers = array_diff_key($oauth_headers, $data);
293 $url = $this->toUrl($url, $data);
294 $response = $this->client->get($url, array(
'Authorization' => $this->_createHeader($oauth_headers)));
297 $headers = array_merge($headers, array(
'Authorization' => $this->_createHeader($oauth_headers)));
298 $response = $this->client->post($url, $data, $headers);
301 $headers = array_merge($headers, array(
'Authorization' => $this->_createHeader($oauth_headers)));
302 $response = $this->client->put($url, $data, $headers);
305 $headers = array_merge($headers, array(
'Authorization' => $this->_createHeader($oauth_headers)));
306 $response = $this->client->delete($url, $headers);
311 $this->validateResponse($url, $response);
327 abstract public function validateResponse($url, $response);
338 private function _createHeader($parameters)
342 foreach ($parameters as $key => $value)
344 if (!strcmp($header,
'OAuth '))
346 $header .= $key .
'="' . $this->safeEncode($value) .
'"';
350 $header .=
', ' . $key .
'="' . $value .
'"';
367 public function toUrl($url, $parameters)
369 foreach ($parameters as $key => $value)
371 if (is_array($value))
373 foreach ($value as $v)
375 if (strpos($url,
'?') ===
false)
377 $url .=
'?' . $key .
'=' . $v;
381 $url .=
'&' . $key .
'=' . $v;
387 if (strpos($value,
' ') !==
false)
389 $value = $this->safeEncode($value);
392 if (strpos($url,
'?') ===
false)
394 $url .=
'?' . $key .
'=' . $value;
398 $url .=
'&' . $key .
'=' . $value;
417 private function _signRequest($url, $method, $parameters)
420 $base = $this->_baseString($url, $method, $parameters);
422 $parameters[
'oauth_signature'] = $this->safeEncode(
424 hash_hmac(
'sha1', $base, $this->_prepareSigningKey(),
true)
442 private function _baseString($url, $method, $parameters)
445 uksort($parameters,
'strcmp');
448 foreach ($parameters as $key => $value)
450 $key = $this->safeEncode($key);
452 if (is_array($value))
454 foreach ($value as $v)
456 $v = $this->safeEncode($v);
457 $kv[] =
"{$key}={$v}";
462 $value = $this->safeEncode($value);
463 $kv[] =
"{$key}={$value}";
467 $params = implode(
'&', $kv);
477 return implode(
'&', $this->safeEncode($base));
490 public function safeEncode($data)
494 return array_map(array($this,
'safeEncode'), $data);
496 elseif (is_scalar($data))
517 public static function generateNonce()
523 return md5($mt . $rand);
533 private function _prepareSigningKey()
535 return $this->safeEncode($this->getOption(
'consumer_secret')) .
'&' . $this->safeEncode(($this->token) ? $this->token[
'secret'] :
'');
546 abstract public function verifyCredentials();
557 public function getOption($key)
559 return $this->options->get($key);
572 public function setOption($key, $value)
574 $this->options->set($key, $value);
586 public function getToken()
600 public function setToken($token)
602 $this->token = $token;