Skip to content
This repository
tree: 870abf644b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 364 lines (330 sloc) 8.721 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
<?php
/**
* @package Joomla.Platform
* @subpackage Oauth
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;
jimport('joomla.environment.response');

/**
* Joomla Platform class for interacting with an OAuth 2.0 server.
*
* @package Joomla.Platform
* @subpackage Oauth
* @since 12.2
*/
class JOauthV2client
{
/**
* @var JRegistry Options for the JOauthV2client object.
* @since 12.2
*/
protected $options;

/**
* @var JHttp The HTTP client object to use in sending HTTP requests.
* @since 12.2
*/
protected $http;

/**
* @var JInput The input object to use in retrieving GET/POST data.
* @since 12.2
*/
protected $input;

/**
* Constructor.
*
* @param JRegistry $options JOauthV2client options object
* @param JHttp $http The HTTP client object
* @param JInput $input The input object
*
* @since 12.2
*/
public function __construct(JRegistry $options = null, JHttp $http = null, JInput $input = null)
{
$this->options = isset($options) ? $options : new JRegistry;
$this->http = isset($http) ? $http : new JHttp($this->options);
$this->input = isset($input) ? $input : JFactory::getApplication()->input;
}

/**
* Get the access token or redict to the authentication URL.
*
* @return string The access token
*
* @since 12.2
*/
public function auth()
{
if ($data['code'] = $this->input->get('code', false, 'raw'))
{
$data['grant_type'] = 'authorization_code';
$data['redirect_uri'] = $this->getOption('redirecturi');
$data['client_id'] = $this->getOption('clientid');
$data['client_secret'] = $this->getOption('clientsecret');
$response = $this->http->post($this->getOption('tokenurl'), $data);

if ($response->code >= 200 && $response->code < 400)
{

if ($response->headers['Content-Type'] == 'application/json')
{
$token = array_merge(json_decode($response->body, true), array('created' => time()));
}
else
{
parse_str($response->body, $token);
$token = array_merge($token, array('created' => time()));
}

$this->setToken($token);
return $token;
}
else
{
throw new RuntimeException('Error code ' . $response->code . ' received requesting access token: ' . $response->body . '.');
}
}

if ($this->getOption('sendheaders'))
{
JResponse::setHeader('Location', $this->createUrl(), true);
JResponse::sendHeaders();
}
return false;
}

/**
* Verify if the client has been authenticated
*
* @return bool Is authenticated
*
* @since 12.2
*/
public function isAuth()
{
$token = $this->getToken();

if (!$token || !array_key_exists('access_token', $token))
{
return false;
}
elseif (array_key_exists('expires_in', $token) && $token['created'] + $token['expires_in'] < time() + 20)
{
return false;
}
else
{
return true;
}
}

/**
* Create the URL for authentication.
*
* @return JHttpResponse The HTTP response
*
* @since 12.2
*/
public function createUrl()
{
if (!$this->getOption('authurl') || !$this->getOption('clientid'))
{
throw new InvalidArgumentException('Authorization URL and client_id are required');
}

$url = $this->getOption('authurl');
if (strpos($url, '?'))
{
$url .= '&';
}
else
{
$url .= '?';
}

$url .= 'response_type=code';
$url .= '&client_id=' . urlencode($this->getOption('clientid'));

if ($this->getOption('redirecturi'))
{
$url .= '&redirect_uri=' . urlencode($this->getOption('redirecturi'));
}

if ($this->getOption('scope'))
{
$scope = is_array($this->getOption('scope')) ? implode(' ', $this->getOption('scope')) : $this->getOption('scope');
$url .= '&scope=' . urlencode($scope);
}

if ($this->getOption('state'))
{
$url .= '&state=' . urlencode($this->getOption('state'));
}

if (is_array($this->getOption('requestparams')))
{
foreach ($this->getOption('requestparams') as $key => $value)
{
$url .= '&' . $key . '=' . urlencode($value);
}
}

return $url;
}

/**
* Send a signed Oauth request.
*
* @param string $url The URL forf the request.
* @param mixed $data The data to include in the request
* @param array $headers The headers to send with the request
* @param string $method The method with which to send the request
* @param int $timeout The timeout for the request
*
* @return string The URL.
*
* @since 12.2
*/
public function query($url, $data = null, $headers = array(), $method = 'get', $timeout = null)
{
$token = $this->getToken();
if (array_key_exists('expires_in', $token) && $token['created'] + $token['expires_in'] < time() + 20)
{
if (!$this->getOption('userefresh'))
{
return false;
}
$token = $this->refreshToken($token['refresh_token']);
}

if (!$this->getOption('authmethod') || $this->getOption('authmethod') == 'bearer')
{
$headers['Authorization'] = 'Bearer ' . $token['access_token'];
}
elseif ($this->getOption('authmethod') == 'get')
{
if (strpos($url, '?'))
{
$url .= '&';
}
else
{
$url .= '?';
}
$url .= $this->getOption('getparam') ? $this->getOption('getparam') : 'access_token';
$url .= '=' . $token['access_token'];
}

switch ($method)
{
case 'head':
case 'get':
case 'delete':
case 'trace':
$response = $this->http->$method($url, $headers, $timeout);
break;
case 'post':
case 'put':
case 'patch':
$response = $this->http->$method($url, $data, $headers, $timeout);
break;
default:
throw new InvalidArgumentException('Unknown HTTP request method: ' . $method . '.');
}

if ($response->code < 200 || $response->code >= 400)
{
throw new RuntimeException('Error code ' . $response->code . ' received requesting data: ' . $response->body . '.');
}
return $response;
}

/**
* Get an option from the JOauthV2client instance.
*
* @param string $key The name of the option to get
*
* @return mixed The option value
*
* @since 12.2
*/
public function getOption($key)
{
return $this->options->get($key);
}

/**
* Set an option for the JOauthV2client instance.
*
* @param string $key The name of the option to set
* @param mixed $value The option value to set
*
* @return JOauthV2client This object for method chaining
*
* @since 12.2
*/
public function setOption($key, $value)
{
$this->options->set($key, $value);
return $this;
}

/**
* Get the access token from the JOauthV2client instance.
*
* @return array The access token
*
* @since 12.2
*/
public function getToken()
{
return $this->getOption('accesstoken');
}

/**
* Set an option for the JOauthV2client instance.
*
* @param array $value The access token
*
* @return JOauthV2client This object for method chaining
*
* @since 12.2
*/
public function setToken($value)
{
if (!array_key_exists('expires_in', $value) && array_key_exists('expires', $value))
{
$value['expires_in'] = $value['expires'];
unset($value['expires']);
}
$this->setOption('accesstoken', $value);
return $this;
}

/**
* Refresh the access token instance.
*
* @param string $token The refresh token
*
* @return array The new access token
*
* @since 12.2
*/
public function refreshToken($token = null)
{
if (!$this->getOption('userefresh'))
{
throw new RuntimeException('Refresh token is not supported for this OAuth instance.');
}

if (!$token)
{
$token = $this->getToken();

if (!array_key_exists('refresh_token', $token))
{
throw new RuntimeException('No refresh token is available.');
}
$token = $token['refresh_token'];
}
$data['grant_type'] = 'refresh_token';
$data['refresh_token'] = $token;
$data['client_id'] = $this->getOption('clientid');
$data['client_secret'] = $this->getOption('clientsecret');
$response = $this->http->post($this->getOption('tokenurl'), $data);

if ($response->code >= 200 || $response->code < 400)
{
if ($response->headers['Content-Type'] == 'application/json')
{
$token = array_merge(json_decode($response->body, true), array('created' => time()));
}
else
{
parse_str($response->body, $token);
$token = array_merge($token, array('created' => time()));
}

$this->setToken($token);
return $token;
}
else
{
throw new Exception('Error code ' . $response->code . ' received refreshing token: ' . $response->body . '.');
}
}
}
Something went wrong with that request. Please try again.