Skip to content
This repository

JOAuth 2.0 Client #1423

Closed
wants to merge 23 commits into from

6 participants

Aaron Schmitz elinw Stefan Neculai Sam Moffatt Rouven Weßling Louis Landry
Aaron Schmitz

This is an OAuth 2.0 client for requesting access tokens and sending signed requests. I created it as a part of my JGoogle GSoC project (http://goo.gl/5dj50).

Aaron Schmitz

See also Diana's 1.0a client: #1422

libraries/joomla/oauth/oauth2client.php
... ...
@@ -0,0 +1,344 @@
  1
+<?php
  2
+/**
  3
+ * @package     Joomla.Platform
  4
+ * @subpackage  Oauth
  5
+ *
  6
+ * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7
+ * @license     GNU General Public License version 2 or later; see LICENSE
  8
+ */
  9
+
  10
+defined('JPATH_PLATFORM') or die;
  11
+jimport('joomla.environment.response');
  12
+jimport('joomla.environment.uri');
1
Rouven Weßling Collaborator

The jimport for JUri isn't necessary anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
libraries/joomla/oauth/oauth2client.php
((5 lines not shown))
  5
+ *
  6
+ * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7
+ * @license     GNU General Public License version 2 or later; see LICENSE
  8
+ */
  9
+
  10
+defined('JPATH_PLATFORM') or die;
  11
+jimport('joomla.environment.response');
  12
+
  13
+/**
  14
+ * Joomla Platform class for interacting with an OAuth 2.0 server.
  15
+ *
  16
+ * @package     Joomla.Platform
  17
+ * @subpackage  Oauth
  18
+ * @since       12.2
  19
+ */
  20
+class JOauthOauth2client
1

I find it strange to name the class JOauthOauth2client. Wouldn't it be better to change the name to JOauth2client (it sounds more natural)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
elinw

Then wouldn't the autoloader be looking for a file named 2client?

Stefan Neculai

Would be a problem to name it 2client.php?
Maybe make a folder called v2 and put the client.php in it. This way it would be JOAuthV2client.

Aaron Schmitz

I suppose it wouldn't be a problem.

elinw

I think I'd like to hear from the maintainers on this one.

Just FYI Aaron needs to know about the patch() issue in order to know how to fix the unit test that has an error.

Aaron Schmitz

Ok. I switch to extending JHttp like JGithub does. If the maintainers accept @dianaprajescu's pull for the patch method I'm 95% of the way there, and if they don't this is a superior alternative to using JHttpTransport.

For now, I renamed the class to JOauth2client and the file to 2client, but I'm flexible on what others prefer.

Aaron Schmitz

@louislandry, is this what you were thinking?

That's fine, but you'd also have to call $application->sendHeaders(); I believe. Is there some reason that you couldn't just use $application->redirect() and needed to do it this way?

Sam Moffatt

@aaronschmitz do you mind renaming it from oauth/v2client.php to oauth2/client.php? Given it seems to be an independent implementation with no sharing of code between the two implementations.

elinw

@pasamio can you comment on whether the class name should be JOAuth or JOauth (my understanding is that the loader looks for non consecutive upper case letters which would make JOAuth okay. Codestyle originally called for uppercase where appropriate (HTML ZXML) but we veered away from that with implementation of the autoloader. We probably should aim for some consistency.

Aaron Schmitz

Closing in favor of #1480.

Aaron Schmitz aaronschmitz closed this October 04, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 23 unique commits by 1 author.

Jul 26, 2012
Aaron Schmitz Split JOauth client code from JGoogle branch. f70bc5b
Jul 31, 2012
Aaron Schmitz Allow 300-399 to be considered valid HTTP response codes. 31eed8d
Aug 01, 2012
Aaron Schmitz Change @since to 12.2 0c5515b
Aug 02, 2012
Aaron Schmitz Alter unit test to be more flexible of JHttpTransport type. ee06028
Aaron Schmitz Merge branch 'JOauth' of github.com:aaronschmitz/joomla-platform into…
… JOauth
92a4e04
Aaron Schmitz Remove unnecessary JUri import. 3195536
Aug 04, 2012
Aaron Schmitz Rename to 2client.php. f8d34a2
Aaron Schmitz Switch from using JHttpTransport to JOauthHttp (extended from JHttp) …
…to make the patch method available.
4841f99
Aaron Schmitz Make iaAuth method more robust and add unit test. 6b71202
Aug 06, 2012
Aaron Schmitz Fix autoloading by renaming to JOauthV2client. 91ae54b
Aug 08, 2012
Aaron Schmitz Two small bugfixes for JOauth. 398de52
Aaron Schmitz Merge branch 'staging' of https://github.com/joomla/joomla-platform i…
…nto JOauth
ff53bf6
Aaron Schmitz Bug fix in Http unit test. 9fb42ac
Aaron Schmitz Improve coverage of unit tests.
Lines: 89.58%
Methods: 63.64%
Classes: 50%
7b03465
Aug 14, 2012
Aaron Schmitz Remove additional http class. 995fac8
Aaron Schmitz Minor tweak to setting the token. 101885c
Aug 22, 2012
Aaron Schmitz Rename auth -> authenticated
Rename isAuth -> isAuthenticated
Replace bool -> boolean
0d5a3b9
Aaron Schmitz Rename auth -> authenticate
Rename isAuth -> isAuthenticated
Replace bool -> boolean
936133c
Aaron Schmitz Merge branch 'JOauth' of github.com:aaronschmitz/joomla-platform into…
… JOauth
04860ec
Sep 11, 2012
Aaron Schmitz Switch to using JApplication for redirects. f468100
Aaron Schmitz Merge branch 'staging' of https://github.com/joomla/joomla-platform i…
…nto JOauth
dce699c
Aaron Schmitz Fix a small error in the unit test. f2bad5c
Sep 17, 2012
Aaron Schmitz Change to JApplicationWeb. e5102e5
This page is out of date. Refresh to see the latest.
370  libraries/joomla/oauth/v2client.php
... ...
@@ -0,0 +1,370 @@
  1
+<?php
  2
+/**
  3
+ * @package     Joomla.Platform
  4
+ * @subpackage  Oauth
  5
+ *
  6
+ * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7
+ * @license     GNU General Public License version 2 or later; see LICENSE
  8
+ */
  9
+
  10
+defined('JPATH_PLATFORM') or die;
  11
+jimport('joomla.environment.response');
  12
+
  13
+/**
  14
+ * Joomla Platform class for interacting with an OAuth 2.0 server.
  15
+ *
  16
+ * @package     Joomla.Platform
  17
+ * @subpackage  Oauth
  18
+ * @since       12.2
  19
+ */
  20
+class JOauthV2client
  21
+{
  22
+	/**
  23
+	 * @var    JRegistry  Options for the JOauthV2client object.
  24
+	 * @since  12.2
  25
+	 */
  26
+	protected $options;
  27
+
  28
+	/**
  29
+	 * @var    JHttp  The HTTP client object to use in sending HTTP requests.
  30
+	 * @since  12.2
  31
+	 */
  32
+	protected $http;
  33
+
  34
+	/**
  35
+	 * @var    JInput  The input object to use in retrieving GET/POST data.
  36
+	 * @since  12.2
  37
+	 */
  38
+	protected $input;
  39
+
  40
+	/**
  41
+	 * @var    JApplicationWeb  The application object to send HTTP headers for redirects.
  42
+	 * @since  12.2
  43
+	 */
  44
+	protected $application;
  45
+
  46
+	/**
  47
+	 * Constructor.
  48
+	 *
  49
+	 * @param   JRegistry        $options      JOauthV2client options object
  50
+	 * @param   JHttp            $http         The HTTP client object
  51
+	 * @param   JInput           $input        The input object
  52
+	 * @param   JApplicationWeb  $application  The application object
  53
+	 *
  54
+	 * @since   12.2
  55
+	 */
  56
+	public function __construct(JRegistry $options = null, JHttp $http = null, JInput $input = null, JApplicationWeb $application = null)
  57
+	{
  58
+		$this->options = isset($options) ? $options : new JRegistry;
  59
+		$this->http = isset($http) ? $http : new JHttp($this->options);
  60
+		$this->input = isset($input) ? $input : JFactory::getApplication()->input;
  61
+		$this->application = isset($application) ? $application : new JApplicationWeb;
  62
+	}
  63
+
  64
+	/**
  65
+	 * Get the access token or redict to the authentication URL.
  66
+	 *
  67
+	 * @return  string  The access token
  68
+	 *
  69
+	 * @since   12.2
  70
+	 */
  71
+	public function authenticate()
  72
+	{
  73
+		if ($data['code'] = $this->input->get('code', false, 'raw'))
  74
+		{
  75
+			$data['grant_type'] = 'authorization_code';
  76
+			$data['redirect_uri'] = $this->getOption('redirecturi');
  77
+			$data['client_id'] = $this->getOption('clientid');
  78
+			$data['client_secret'] = $this->getOption('clientsecret');
  79
+			$response = $this->http->post($this->getOption('tokenurl'), $data);
  80
+
  81
+			if ($response->code >= 200 && $response->code < 400)
  82
+			{
  83
+
  84
+				if ($response->headers['Content-Type'] == 'application/json')
  85
+				{
  86
+					$token = array_merge(json_decode($response->body, true), array('created' => time()));
  87
+				}
  88
+				else
  89
+				{
  90
+					parse_str($response->body, $token);
  91
+					$token = array_merge($token, array('created' => time()));
  92
+				}
  93
+
  94
+				$this->setToken($token);
  95
+				return $token;
  96
+			}
  97
+			else
  98
+			{
  99
+				throw new RuntimeException('Error code ' . $response->code . ' received requesting access token: ' . $response->body . '.');
  100
+			}
  101
+		}
  102
+
  103
+		if ($this->getOption('sendheaders'))
  104
+		{
  105
+			$this->application->redirect($this->createUrl());
  106
+		}
  107
+		return false;
  108
+	}
  109
+
  110
+	/**
  111
+	 * Verify if the client has been authenticated
  112
+	 *
  113
+	 * @return  boolean  Is authenticated
  114
+	 *
  115
+	 * @since   12.2
  116
+	 */
  117
+	public function isAuthenticated()
  118
+	{
  119
+		$token = $this->getToken();
  120
+
  121
+		if (!$token || !array_key_exists('access_token', $token))
  122
+		{
  123
+			return false;
  124
+		}
  125
+		elseif (array_key_exists('expires_in', $token) && $token['created'] + $token['expires_in'] < time() + 20)
  126
+		{
  127
+			return false;
  128
+		}
  129
+		else
  130
+		{
  131
+			return true;
  132
+		}
  133
+	}
  134
+
  135
+	/**
  136
+	 * Create the URL for authentication.
  137
+	 *
  138
+	 * @return  JHttpResponse  The HTTP response
  139
+	 *
  140
+	 * @since   12.2
  141
+	 */
  142
+	public function createUrl()
  143
+	{
  144
+		if (!$this->getOption('authurl') || !$this->getOption('clientid'))
  145
+		{
  146
+			throw new InvalidArgumentException('Authorization URL and client_id are required');
  147
+		}
  148
+
  149
+		$url = $this->getOption('authurl');
  150
+		if (strpos($url, '?'))
  151
+		{
  152
+			$url .= '&';
  153
+		}
  154
+		else
  155
+		{
  156
+			$url .= '?';
  157
+		}
  158
+
  159
+		$url .= 'response_type=code';
  160
+		$url .= '&client_id=' . urlencode($this->getOption('clientid'));
  161
+
  162
+		if ($this->getOption('redirecturi'))
  163
+		{
  164
+			$url .= '&redirect_uri=' . urlencode($this->getOption('redirecturi'));
  165
+		}
  166
+
  167
+		if ($this->getOption('scope'))
  168
+		{
  169
+			$scope = is_array($this->getOption('scope')) ? implode(' ', $this->getOption('scope')) : $this->getOption('scope');
  170
+			$url .= '&scope=' . urlencode($scope);
  171
+		}
  172
+
  173
+		if ($this->getOption('state'))
  174
+		{
  175
+			$url .= '&state=' . urlencode($this->getOption('state'));
  176
+		}
  177
+
  178
+		if (is_array($this->getOption('requestparams')))
  179
+		{
  180
+			foreach ($this->getOption('requestparams') as $key => $value)
  181
+			{
  182
+				$url .= '&' . $key . '=' . urlencode($value);
  183
+			}
  184
+		}
  185
+
  186
+		return $url;
  187
+	}
  188
+
  189
+	/**
  190
+	 * Send a signed Oauth request.
  191
+	 *
  192
+	 * @param   string  $url      The URL forf the request.
  193
+	 * @param   mixed   $data     The data to include in the request
  194
+	 * @param   array   $headers  The headers to send with the request
  195
+	 * @param   string  $method   The method with which to send the request
  196
+	 * @param   int     $timeout  The timeout for the request
  197
+	 *
  198
+	 * @return  string  The URL.
  199
+	 *
  200
+	 * @since   12.2
  201
+	 */
  202
+	public function query($url, $data = null, $headers = array(), $method = 'get', $timeout = null)
  203
+	{
  204
+		$token = $this->getToken();
  205
+		if (array_key_exists('expires_in', $token) && $token['created'] + $token['expires_in'] < time() + 20)
  206
+		{
  207
+			if (!$this->getOption('userefresh'))
  208
+			{
  209
+				return false;
  210
+			}
  211
+			$token = $this->refreshToken($token['refresh_token']);
  212
+		}
  213
+
  214
+		if (!$this->getOption('authmethod') || $this->getOption('authmethod') == 'bearer')
  215
+		{
  216
+			$headers['Authorization'] = 'Bearer ' . $token['access_token'];
  217
+		}
  218
+		elseif ($this->getOption('authmethod') == 'get')
  219
+		{
  220
+			if (strpos($url, '?'))
  221
+			{
  222
+				$url .= '&';
  223
+			}
  224
+			else
  225
+			{
  226
+				$url .= '?';
  227
+			}
  228
+			$url .= $this->getOption('getparam') ? $this->getOption('getparam') : 'access_token';
  229
+			$url .= '=' . $token['access_token'];
  230
+		}
  231
+
  232
+		switch ($method)
  233
+		{
  234
+			case 'head':
  235
+			case 'get':
  236
+			case 'delete':
  237
+			case 'trace':
  238
+			$response = $this->http->$method($url, $headers, $timeout);
  239
+			break;
  240
+			case 'post':
  241
+			case 'put':
  242
+			case 'patch':
  243
+			$response = $this->http->$method($url, $data, $headers, $timeout);
  244
+			break;
  245
+			default:
  246
+			throw new InvalidArgumentException('Unknown HTTP request method: ' . $method . '.');
  247
+		}
  248
+
  249
+		if ($response->code < 200 || $response->code >= 400)
  250
+		{
  251
+			throw new RuntimeException('Error code ' . $response->code . ' received requesting data: ' . $response->body . '.');
  252
+		}
  253
+		return $response;
  254
+	}
  255
+
  256
+	/**
  257
+	 * Get an option from the JOauthV2client instance.
  258
+	 *
  259
+	 * @param   string  $key  The name of the option to get
  260
+	 *
  261
+	 * @return  mixed  The option value
  262
+	 *
  263
+	 * @since   12.2
  264
+	 */
  265
+	public function getOption($key)
  266
+	{
  267
+		return $this->options->get($key);
  268
+	}
  269
+
  270
+	/**
  271
+	 * Set an option for the JOauthV2client instance.
  272
+	 *
  273
+	 * @param   string  $key    The name of the option to set
  274
+	 * @param   mixed   $value  The option value to set
  275
+	 *
  276
+	 * @return  JOauthV2client  This object for method chaining
  277
+	 *
  278
+	 * @since   12.2
  279
+	 */
  280
+	public function setOption($key, $value)
  281
+	{
  282
+		$this->options->set($key, $value);
  283
+		return $this;
  284
+	}
  285
+
  286
+	/**
  287
+	 * Get the access token from the JOauthV2client instance.
  288
+	 *
  289
+	 * @return  array  The access token
  290
+	 *
  291
+	 * @since   12.2
  292
+	 */
  293
+	public function getToken()
  294
+	{
  295
+		return $this->getOption('accesstoken');
  296
+	}
  297
+
  298
+	/**
  299
+	 * Set an option for the JOauthV2client instance.
  300
+	 *
  301
+	 * @param   array  $value  The access token
  302
+	 *
  303
+	 * @return  JOauthV2client  This object for method chaining
  304
+	 *
  305
+	 * @since   12.2
  306
+	 */
  307
+	public function setToken($value)
  308
+	{
  309
+		if (is_array($value) && !array_key_exists('expires_in', $value) && array_key_exists('expires', $value))
  310
+		{
  311
+			$value['expires_in'] = $value['expires'];
  312
+			unset($value['expires']);
  313
+		}
  314
+		$this->setOption('accesstoken', $value);
  315
+		return $this;
  316
+	}
  317
+
  318
+	/**
  319
+	 * Refresh the access token instance.
  320
+	 *
  321
+	 * @param   string  $token  The refresh token
  322
+	 *
  323
+	 * @return  array  The new access token
  324
+	 *
  325
+	 * @since   12.2
  326
+	 */
  327
+	public function refreshToken($token = null)
  328
+	{
  329
+		if (!$this->getOption('userefresh'))
  330
+		{
  331
+			throw new RuntimeException('Refresh token is not supported for this OAuth instance.');
  332
+		}
  333
+
  334
+		if (!$token)
  335
+		{
  336
+			$token = $this->getToken();
  337
+
  338
+			if (!array_key_exists('refresh_token', $token))
  339
+			{
  340
+				throw new RuntimeException('No refresh token is available.');
  341
+			}
  342
+			$token = $token['refresh_token'];
  343
+		}
  344
+		$data['grant_type'] = 'refresh_token';
  345
+		$data['refresh_token'] = $token;
  346
+		$data['client_id'] = $this->getOption('clientid');
  347
+		$data['client_secret'] = $this->getOption('clientsecret');
  348
+		$response = $this->http->post($this->getOption('tokenurl'), $data);
  349
+
  350
+		if ($response->code >= 200 || $response->code < 400)
  351
+		{
  352
+			if ($response->headers['Content-Type'] == 'application/json')
  353
+			{
  354
+				$token = array_merge(json_decode($response->body, true), array('created' => time()));
  355
+			}
  356
+			else
  357
+			{
  358
+				parse_str($response->body, $token);
  359
+				$token = array_merge($token, array('created' => time()));
  360
+			}
  361
+
  362
+			$this->setToken($token);
  363
+			return $token;
  364
+		}
  365
+		else
  366
+		{
  367
+			throw new Exception('Error code ' . $response->code . ' received refreshing token: ' . $response->body . '.');
  368
+		}
  369
+	}
  370
+}
419  tests/suites/unit/joomla/oauth/JOauthV2clientTest.php
... ...
@@ -0,0 +1,419 @@
  1
+<?php
  2
+/**
  3
+ * @package     Joomla.UnitTest
  4
+ * @subpackage  Client
  5
+ *
  6
+ * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7
+ * @license     GNU General Public License version 2 or later; see LICENSE
  8
+ */
  9
+
  10
+require_once JPATH_PLATFORM . '/joomla/oauth/v2client.php';
  11
+
  12
+/**
  13
+ * Test class for JOauthV2client.
  14
+ *
  15
+ * @package     Joomla.UnitTest
  16
+ * @subpackage  Oauth
  17
+ * @since       12.2
  18
+ */
  19
+class JOauthV2clientTest extends PHPUnit_Framework_TestCase
  20
+{
  21
+	/**
  22
+	 * @var    JRegistry  Options for the JOauthV2client object.
  23
+	 */
  24
+	protected $options;
  25
+
  26
+	/**
  27
+	 * @var    JHttp  Mock client object.
  28
+	 */
  29
+	protected $client;
  30
+
  31
+	/**
  32
+	 * @var    JInput  The input object to use in retrieving GET/POST data.
  33
+	 */
  34
+	protected $input;
  35
+
  36
+	/**
  37
+	 * @var    JApplicationWeb  The application object to send HTTP headers for redirects.
  38
+	 */
  39
+	protected $application;
  40
+
  41
+	/**
  42
+	 * @var    JOauthV2client  Object under test.
  43
+	 */
  44
+	protected $object;
  45
+
  46
+	/**
  47
+	 * Sets up the fixture, for example, opens a network connection.
  48
+	 * This method is called before a test is executed.
  49
+	 *
  50
+	 * @access protected
  51
+	 * @return void
  52
+	 */
  53
+	protected function setUp()
  54
+	{
  55
+		$_SERVER['HTTP_HOST'] = 'mydomain.com';
  56
+		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
  57
+		$_SERVER['REQUEST_URI'] = '/index.php';
  58
+		$_SERVER['SCRIPT_NAME'] = '/index.php';
  59
+
  60
+		$this->options = new JRegistry;
  61
+		$this->http = $this->getMock('JHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
  62
+		$array = array();
  63
+		$this->input = new JInput($array);
  64
+		$this->application = new JApplicationWebInspector;
  65
+		$this->object = new JOauthV2client($this->options, $this->http, $this->input, $this->application);
  66
+	}
  67
+
  68
+	/**
  69
+	 * Tears down the fixture, for example, closes a network connection.
  70
+	 * This method is called after a test is executed.
  71
+	 *
  72
+	 * @access protected
  73
+	 * @return void
  74
+	 */
  75
+	protected function tearDown()
  76
+	{
  77
+	}
  78
+
  79
+	/**
  80
+	 * Tests the auth method
  81
+	 *
  82
+	 * @group	JOauth
  83
+	 * @return void
  84
+	 */
  85
+	public function testAuth()
  86
+	{
  87
+		$this->object->setOption('authurl', 'https://accounts.google.com/o/oauth2/auth');
  88
+		$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  89
+		$this->object->setOption('scope', array('https://www.googleapis.com/auth/adsense', 'https://www.googleapis.com/auth/calendar'));
  90
+		$this->object->setOption('redirecturi', 'http://localhost/oauth');
  91
+		$this->object->setOption('requestparams', array('access_type' => 'offline', 'approval_prompt' => 'auto'));
  92
+		$this->object->setOption('sendheaders', true);
  93
+
  94
+		$this->object->authenticate();
  95
+		$this->assertEquals(0, $this->application->closed);
  96
+
  97
+		$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
  98
+		$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  99
+		$this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
  100
+
  101
+		$this->http->expects($this->once())->method('post')->will($this->returnCallback('encodedGrantOauthCallback'));
  102
+		$result = $this->object->authenticate();
  103
+		$this->assertEquals('accessvalue', $result['access_token']);
  104
+		$this->assertEquals('refreshvalue', $result['refresh_token']);
  105
+		$this->assertEquals(3600, $result['expires_in']);
  106
+		$this->assertEquals(time(), $result['created'], 10);
  107
+	}
  108
+
  109
+	/**
  110
+	 * Tests the auth method with JSON data
  111
+	 *
  112
+	 * @group	JOauth
  113
+	 * @return void
  114
+	 */
  115
+	public function testAuthJson()
  116
+	{
  117
+		$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
  118
+		$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  119
+		$this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
  120
+
  121
+		$this->http->expects($this->once())->method('post')->will($this->returnCallback('jsonGrantOauthCallback'));
  122
+		$result = $this->object->authenticate();
  123
+		$this->assertEquals('accessvalue', $result['access_token']);
  124
+		$this->assertEquals('refreshvalue', $result['refresh_token']);
  125
+		$this->assertEquals(3600, $result['expires_in']);
  126
+		$this->assertEquals(time(), $result['created'], 10);
  127
+	}
  128
+
  129
+	/**
  130
+	 * Tests the isauth method
  131
+	 *
  132
+	 * @group	JOauth
  133
+	 * @return void
  134
+	 */
  135
+	public function testIsAuth()
  136
+	{
  137
+		$this->assertEquals(false, $this->object->isAuthenticated());
  138
+
  139
+		$token['access_token'] = 'accessvalue';
  140
+		$token['refresh_token'] = 'refreshvalue';
  141
+		$token['created'] = time();
  142
+		$token['expires_in'] = 3600;
  143
+		$this->object->setToken($token);
  144
+
  145
+		$this->assertTrue($this->object->isAuthenticated());
  146
+
  147
+		$token['created'] = time() - 4000;
  148
+		$token['expires_in'] = 3600;
  149
+		$this->object->setToken($token);
  150
+
  151
+		$this->assertFalse($this->object->isAuthenticated());
  152
+	}
  153
+
  154
+	/**
  155
+	 * Tests the auth method
  156
+	 *
  157
+	 * @group	JOauth
  158
+	 * @return void
  159
+	 */
  160
+	public function testCreateUrl()
  161
+	{
  162
+		$this->object->setOption('authurl', 'https://accounts.google.com/o/oauth2/auth');
  163
+		$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  164
+		$this->object->setOption('scope', array('https://www.googleapis.com/auth/adsense', 'https://www.googleapis.com/auth/calendar'));
  165
+		$this->object->setOption('state', '123456');
  166
+		$this->object->setOption('redirecturi', 'http://localhost/oauth');
  167
+		$this->object->setOption('requestparams', array('access_type' => 'offline', 'approval_prompt' => 'auto'));
  168
+
  169
+		$url = $this->object->createUrl();
  170
+		$expected = 'https://accounts.google.com/o/oauth2/auth?response_type=code';
  171
+		$expected .= '&client_id=01234567891011.apps.googleusercontent.com';
  172
+		$expected .= '&redirect_uri=http%3A%2F%2Flocalhost%2Foauth';
  173
+		$expected .= '&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadsense';
  174
+		$expected .= '+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar';
  175
+		$expected .= '&state=123456&access_type=offline&approval_prompt=auto';
  176
+		$this->assertEquals($expected, $url);
  177
+	}
  178
+
  179
+	/**
  180
+	 * Tests the auth method
  181
+	 *
  182
+	 * @group	JOauth
  183
+	 * @return void
  184
+	 */
  185
+	public function testQuery()
  186
+	{
  187
+		$token['access_token'] = 'accessvalue';
  188
+		$token['refresh_token'] = 'refreshvalue';
  189
+		$token['created'] = time() - 1800;
  190
+		$token['expires_in'] = 600;
  191
+		$this->object->setToken($token);
  192
+
  193
+		$result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'get');
  194
+		$this->assertFalse($result);
  195
+
  196
+		$token['expires_in'] = 3600;
  197
+		$this->object->setToken($token);
  198
+
  199
+		$this->http->expects($this->once())->method('post')->will($this->returnCallback('queryOauthCallback'));
  200
+		$result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'post');
  201
+		$this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
  202
+		$this->assertEquals(200, $result->code);
  203
+
  204
+		$this->object->setOption('authmethod', 'get');
  205
+		$this->http->expects($this->once())->method('get')->will($this->returnCallback('getOauthCallback'));
  206
+		$result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'get');
  207
+		$this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
  208
+		$this->assertEquals(200, $result->code);
  209
+	}
  210
+
  211
+	/**
  212
+	 * Tests the setOption method
  213
+	 *
  214
+	 * @group	JOauth
  215
+	 * @return void
  216
+	 */
  217
+	public function testSetOption()
  218
+	{
  219
+		$this->object->setOption('key', 'value');
  220
+
  221
+		$this->assertThat(
  222
+			$this->options->get('key'),
  223
+			$this->equalTo('value')
  224
+		);
  225
+	}
  226
+
  227
+	/**
  228
+	 * Tests the getOption method
  229
+	 *
  230
+	 * @group	JOauth
  231
+	 * @return void
  232
+	 */
  233
+	public function testGetOption()
  234
+	{
  235
+		$this->options->set('key', 'value');
  236
+
  237
+		$this->assertThat(
  238
+			$this->object->getOption('key'),
  239
+			$this->equalTo('value')
  240
+		);
  241
+	}
  242
+
  243
+	/**
  244
+	 * Tests the setToken method
  245
+	 *
  246
+	 * @group	JOauth
  247
+	 * @return void
  248
+	 */
  249
+	public function testSetToken()
  250
+	{
  251
+		$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA'));
  252
+
  253
+		$this->assertThat(
  254
+			$this->options->get('accesstoken'),
  255
+			$this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
  256
+		);
  257
+
  258
+		$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600));
  259
+
  260
+		$this->assertThat(
  261
+			$this->options->get('accesstoken'),
  262
+			$this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
  263
+		);
  264
+
  265
+		$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600));
  266
+
  267
+		$this->assertThat(
  268
+			$this->options->get('accesstoken'),
  269
+			$this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
  270
+		);
  271
+	}
  272
+
  273
+	/**
  274
+	 * Tests the getToken method
  275
+	 *
  276
+	 * @group	JOauth
  277
+	 * @return void
  278
+	 */
  279
+	public function testGetToken()
  280
+	{
  281
+		$this->options->set('accesstoken', array('access_token' => 'RANDOM STRING OF DATA'));
  282
+
  283
+		$this->assertThat(
  284
+			$this->object->getToken(),
  285
+			$this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
  286
+		);
  287
+	}
  288
+
  289
+	/**
  290
+	 * Tests the refreshToken method
  291
+	 *
  292
+	 * @group	JOauth
  293
+	 * @return void
  294
+	 */
  295
+	public function testRefreshToken()
  296
+	{
  297
+		$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
  298
+		$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  299
+		$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  300
+		$this->object->setOption('redirecturi', 'http://localhost/oauth');
  301
+		$this->object->setOption('userefresh', true);
  302
+		$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
  303
+
  304
+		$this->http->expects($this->once())->method('post')->will($this->returnCallback('encodedGrantOauthCallback'));
  305
+		$result = $this->object->refreshToken();
  306
+		$this->assertEquals('accessvalue', $result['access_token']);
  307
+		$this->assertEquals('refreshvalue', $result['refresh_token']);
  308
+		$this->assertEquals(3600, $result['expires_in']);
  309
+		$this->assertEquals(time(), $result['created'], 10);
  310
+	}
  311
+
  312
+	/**
  313
+	 * Tests the refreshToken method with JSON
  314
+	 *
  315
+	 * @group	JOauth
  316
+	 * @return void
  317
+	 */
  318
+	public function testRefreshTokenJson()
  319
+	{
  320
+		$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
  321
+		$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
  322
+		$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
  323
+		$this->object->setOption('redirecturi', 'http://localhost/oauth');
  324
+		$this->object->setOption('userefresh', true);
  325
+		$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
  326
+
  327
+		$this->http->expects($this->once())->method('post')->will($this->returnCallback('jsonGrantOauthCallback'));
  328
+		$result = $this->object->refreshToken();
  329
+		$this->assertEquals('accessvalue', $result['access_token']);
  330
+		$this->assertEquals('refreshvalue', $result['refresh_token']);
  331
+		$this->assertEquals(3600, $result['expires_in']);
  332
+		$this->assertEquals(time(), $result['created'], 10);
  333
+	}
  334
+}
  335
+
  336
+/**
  337
+ * Dummy
  338
+ *
  339
+ * @param   string   $url      Path to the resource.
  340
+ * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  341
+ * @param   array    $headers  An array of name-value pairs to include in the header of the request
  342
+ * @param   integer  $timeout  Read timeout in seconds.
  343
+ *
  344
+ * @return  JHttpResponse
  345
+ *
  346
+ * @since   12.2
  347
+ */
  348
+function encodedGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
  349
+{
  350
+
  351
+	$response->code = 200;
  352
+	$response->headers = array('Content-Type' => 'x-www-form-urlencoded');
  353
+	$response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600';
  354
+
  355
+	return $response;
  356
+}
  357
+
  358
+/**
  359
+ * Dummy
  360
+ *
  361
+ * @param   string   $url      Path to the resource.
  362
+ * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  363
+ * @param   array    $headers  An array of name-value pairs to include in the header of the request
  364
+ * @param   integer  $timeout  Read timeout in seconds.
  365
+ *
  366
+ * @return  JHttpResponse
  367
+ *
  368
+ * @since   12.2
  369
+ */
  370
+function jsonGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
  371
+{
  372
+
  373
+	$response->code = 200;
  374
+	$response->headers = array('Content-Type' => 'application/json');
  375
+	$response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';
  376
+
  377
+	return $response;
  378
+}
  379
+
  380
+/**
  381
+ * Dummy
  382
+ *
  383
+ * @param   string   $url      Path to the resource.
  384
+ * @param   mixed    $data     Either an associative array or a string to be sent with the request.
  385
+ * @param   array    $headers  An array of name-value pairs to include in the header of the request
  386
+ * @param   integer  $timeout  Read timeout in seconds.
  387
+ *
  388
+ * @return  JHttpResponse
  389
+ *
  390
+ * @since   12.2
  391
+ */
  392
+function queryOauthCallback($url, $data, array $headers = null, $timeout = null)
  393
+{
  394
+	$response->code = 200;
  395
+	$response->headers = array('Content-Type' => 'text/html');
  396
+	$response->body = 'Lorem ipsum dolor sit amet.';
  397
+
  398
+	return $response;
  399
+}
  400
+
  401
+/**
  402
+ * Dummy
  403
+ *
  404
+ * @param   string   $url      Path to the resource.
  405
+ * @param   array    $headers  An array of name-value pairs to include in the header of the request.
  406
+ * @param   integer  $timeout  Read timeout in seconds.
  407
+ *
  408
+ * @return  JHttpResponse
  409
+ *
  410
+ * @since   12.2
  411
+ */
  412
+function getOauthCallback($url, array $headers = null, $timeout = null)
  413
+{
  414
+	$response->code = 200;
  415
+	$response->headers = array('Content-Type' => 'text/html');
  416
+	$response->body = 'Lorem ipsum dolor sit amet.';
  417
+
  418
+	return $response;
  419
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.