Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

JOAuth 2.0 Client #1423

Closed
wants to merge 23 commits into from

6 participants

@aaronschmitz

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).

@aaronschmitz

See also Diana's 1.0a client: #1422

libraries/joomla/oauth/oauth2client.php
@@ -0,0 +1,344 @@
+<?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');
+jimport('joomla.environment.uri');
@realityking 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))
+ *
+ * @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 JOauthOauth2client

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?

@stefanneculai

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.

@aaronschmitz

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.

@aaronschmitz

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.

@aaronschmitz

@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?

@pasamio

@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.

@aaronschmitz

Closing in favor of #1480.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 26, 2012
  1. @aaronschmitz
Commits on Jul 31, 2012
  1. @aaronschmitz
Commits on Aug 2, 2012
  1. @aaronschmitz

    Change @since to 12.2

    aaronschmitz authored
  2. @aaronschmitz
  3. @aaronschmitz
  4. @aaronschmitz
Commits on Aug 4, 2012
  1. @aaronschmitz

    Rename to 2client.php.

    aaronschmitz authored
  2. @aaronschmitz

    Switch from using JHttpTransport to JOauthHttp (extended from JHttp) …

    aaronschmitz authored
    …to make the patch method available.
Commits on Aug 5, 2012
  1. @aaronschmitz
Commits on Aug 6, 2012
  1. @aaronschmitz
Commits on Aug 8, 2012
  1. @aaronschmitz
  2. @aaronschmitz
  3. @aaronschmitz
  4. @aaronschmitz

    Improve coverage of unit tests.

    aaronschmitz authored
    Lines: 89.58%
    Methods: 63.64%
    Classes: 50%
Commits on Aug 14, 2012
  1. @aaronschmitz
  2. @aaronschmitz
Commits on Aug 22, 2012
  1. @aaronschmitz

    Rename auth -> authenticated

    aaronschmitz authored
    Rename isAuth -> isAuthenticated
    Replace bool -> boolean
  2. @aaronschmitz

    Rename auth -> authenticate

    aaronschmitz authored
    Rename isAuth -> isAuthenticated
    Replace bool -> boolean
  3. @aaronschmitz
Commits on Sep 11, 2012
  1. @aaronschmitz
  2. @aaronschmitz
  3. @aaronschmitz
Commits on Sep 17, 2012
  1. @aaronschmitz
This page is out of date. Refresh to see the latest.
View
370 libraries/joomla/oauth/v2client.php
@@ -0,0 +1,370 @@
+<?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;
+
+ /**
+ * @var JApplicationWeb The application object to send HTTP headers for redirects.
+ * @since 12.2
+ */
+ protected $application;
+
+ /**
+ * Constructor.
+ *
+ * @param JRegistry $options JOauthV2client options object
+ * @param JHttp $http The HTTP client object
+ * @param JInput $input The input object
+ * @param JApplicationWeb $application The application object
+ *
+ * @since 12.2
+ */
+ public function __construct(JRegistry $options = null, JHttp $http = null, JInput $input = null, JApplicationWeb $application = 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;
+ $this->application = isset($application) ? $application : new JApplicationWeb;
+ }
+
+ /**
+ * Get the access token or redict to the authentication URL.
+ *
+ * @return string The access token
+ *
+ * @since 12.2
+ */
+ public function authenticate()
+ {
+ 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'))
+ {
+ $this->application->redirect($this->createUrl());
+ }
+ return false;
+ }
+
+ /**
+ * Verify if the client has been authenticated
+ *
+ * @return boolean Is authenticated
+ *
+ * @since 12.2
+ */
+ public function isAuthenticated()
+ {
+ $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 (is_array($value) && !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 . '.');
+ }
+ }
+}
View
419 tests/suites/unit/joomla/oauth/JOauthV2clientTest.php
@@ -0,0 +1,419 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Client
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+require_once JPATH_PLATFORM . '/joomla/oauth/v2client.php';
+
+/**
+ * Test class for JOauthV2client.
+ *
+ * @package Joomla.UnitTest
+ * @subpackage Oauth
+ * @since 12.2
+ */
+class JOauthV2clientTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var JRegistry Options for the JOauthV2client object.
+ */
+ protected $options;
+
+ /**
+ * @var JHttp Mock client object.
+ */
+ protected $client;
+
+ /**
+ * @var JInput The input object to use in retrieving GET/POST data.
+ */
+ protected $input;
+
+ /**
+ * @var JApplicationWeb The application object to send HTTP headers for redirects.
+ */
+ protected $application;
+
+ /**
+ * @var JOauthV2client Object under test.
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ * @return void
+ */
+ protected function setUp()
+ {
+ $_SERVER['HTTP_HOST'] = 'mydomain.com';
+ $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
+ $_SERVER['REQUEST_URI'] = '/index.php';
+ $_SERVER['SCRIPT_NAME'] = '/index.php';
+
+ $this->options = new JRegistry;
+ $this->http = $this->getMock('JHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
+ $array = array();
+ $this->input = new JInput($array);
+ $this->application = new JApplicationWebInspector;
+ $this->object = new JOauthV2client($this->options, $this->http, $this->input, $this->application);
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ * @return void
+ */
+ protected function tearDown()
+ {
+ }
+
+ /**
+ * Tests the auth method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testAuth()
+ {
+ $this->object->setOption('authurl', 'https://accounts.google.com/o/oauth2/auth');
+ $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
+ $this->object->setOption('scope', array('https://www.googleapis.com/auth/adsense', 'https://www.googleapis.com/auth/calendar'));
+ $this->object->setOption('redirecturi', 'http://localhost/oauth');
+ $this->object->setOption('requestparams', array('access_type' => 'offline', 'approval_prompt' => 'auto'));
+ $this->object->setOption('sendheaders', true);
+
+ $this->object->authenticate();
+ $this->assertEquals(0, $this->application->closed);
+
+ $this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
+ $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
+ $this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
+
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('encodedGrantOauthCallback'));
+ $result = $this->object->authenticate();
+ $this->assertEquals('accessvalue', $result['access_token']);
+ $this->assertEquals('refreshvalue', $result['refresh_token']);
+ $this->assertEquals(3600, $result['expires_in']);
+ $this->assertEquals(time(), $result['created'], 10);
+ }
+
+ /**
+ * Tests the auth method with JSON data
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testAuthJson()
+ {
+ $this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
+ $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
+ $this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
+
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('jsonGrantOauthCallback'));
+ $result = $this->object->authenticate();
+ $this->assertEquals('accessvalue', $result['access_token']);
+ $this->assertEquals('refreshvalue', $result['refresh_token']);
+ $this->assertEquals(3600, $result['expires_in']);
+ $this->assertEquals(time(), $result['created'], 10);
+ }
+
+ /**
+ * Tests the isauth method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testIsAuth()
+ {
+ $this->assertEquals(false, $this->object->isAuthenticated());
+
+ $token['access_token'] = 'accessvalue';
+ $token['refresh_token'] = 'refreshvalue';
+ $token['created'] = time();
+ $token['expires_in'] = 3600;
+ $this->object->setToken($token);
+
+ $this->assertTrue($this->object->isAuthenticated());
+
+ $token['created'] = time() - 4000;
+ $token['expires_in'] = 3600;
+ $this->object->setToken($token);
+
+ $this->assertFalse($this->object->isAuthenticated());
+ }
+
+ /**
+ * Tests the auth method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testCreateUrl()
+ {
+ $this->object->setOption('authurl', 'https://accounts.google.com/o/oauth2/auth');
+ $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
+ $this->object->setOption('scope', array('https://www.googleapis.com/auth/adsense', 'https://www.googleapis.com/auth/calendar'));
+ $this->object->setOption('state', '123456');
+ $this->object->setOption('redirecturi', 'http://localhost/oauth');
+ $this->object->setOption('requestparams', array('access_type' => 'offline', 'approval_prompt' => 'auto'));
+
+ $url = $this->object->createUrl();
+ $expected = 'https://accounts.google.com/o/oauth2/auth?response_type=code';
+ $expected .= '&client_id=01234567891011.apps.googleusercontent.com';
+ $expected .= '&redirect_uri=http%3A%2F%2Flocalhost%2Foauth';
+ $expected .= '&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadsense';
+ $expected .= '+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar';
+ $expected .= '&state=123456&access_type=offline&approval_prompt=auto';
+ $this->assertEquals($expected, $url);
+ }
+
+ /**
+ * Tests the auth method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testQuery()
+ {
+ $token['access_token'] = 'accessvalue';
+ $token['refresh_token'] = 'refreshvalue';
+ $token['created'] = time() - 1800;
+ $token['expires_in'] = 600;
+ $this->object->setToken($token);
+
+ $result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'get');
+ $this->assertFalse($result);
+
+ $token['expires_in'] = 3600;
+ $this->object->setToken($token);
+
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('queryOauthCallback'));
+ $result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'post');
+ $this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
+ $this->assertEquals(200, $result->code);
+
+ $this->object->setOption('authmethod', 'get');
+ $this->http->expects($this->once())->method('get')->will($this->returnCallback('getOauthCallback'));
+ $result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'get');
+ $this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
+ $this->assertEquals(200, $result->code);
+ }
+
+ /**
+ * Tests the setOption method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testSetOption()
+ {
+ $this->object->setOption('key', 'value');
+
+ $this->assertThat(
+ $this->options->get('key'),
+ $this->equalTo('value')
+ );
+ }
+
+ /**
+ * Tests the getOption method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testGetOption()
+ {
+ $this->options->set('key', 'value');
+
+ $this->assertThat(
+ $this->object->getOption('key'),
+ $this->equalTo('value')
+ );
+ }
+
+ /**
+ * Tests the setToken method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testSetToken()
+ {
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA'));
+
+ $this->assertThat(
+ $this->options->get('accesstoken'),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
+ );
+
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600));
+
+ $this->assertThat(
+ $this->options->get('accesstoken'),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
+ );
+
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600));
+
+ $this->assertThat(
+ $this->options->get('accesstoken'),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
+ );
+ }
+
+ /**
+ * Tests the getToken method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testGetToken()
+ {
+ $this->options->set('accesstoken', array('access_token' => 'RANDOM STRING OF DATA'));
+
+ $this->assertThat(
+ $this->object->getToken(),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
+ );
+ }
+
+ /**
+ * Tests the refreshToken method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testRefreshToken()
+ {
+ $this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
+ $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
+ $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
+ $this->object->setOption('redirecturi', 'http://localhost/oauth');
+ $this->object->setOption('userefresh', true);
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
+
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('encodedGrantOauthCallback'));
+ $result = $this->object->refreshToken();
+ $this->assertEquals('accessvalue', $result['access_token']);
+ $this->assertEquals('refreshvalue', $result['refresh_token']);
+ $this->assertEquals(3600, $result['expires_in']);
+ $this->assertEquals(time(), $result['created'], 10);
+ }
+
+ /**
+ * Tests the refreshToken method with JSON
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testRefreshTokenJson()
+ {
+ $this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
+ $this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
+ $this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
+ $this->object->setOption('redirecturi', 'http://localhost/oauth');
+ $this->object->setOption('userefresh', true);
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
+
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('jsonGrantOauthCallback'));
+ $result = $this->object->refreshToken();
+ $this->assertEquals('accessvalue', $result['access_token']);
+ $this->assertEquals('refreshvalue', $result['refresh_token']);
+ $this->assertEquals(3600, $result['expires_in']);
+ $this->assertEquals(time(), $result['created'], 10);
+ }
+}
+
+/**
+ * Dummy
+ *
+ * @param string $url Path to the resource.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
+ * @param array $headers An array of name-value pairs to include in the header of the request
+ * @param integer $timeout Read timeout in seconds.
+ *
+ * @return JHttpResponse
+ *
+ * @since 12.2
+ */
+function encodedGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
+{
+
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'x-www-form-urlencoded');
+ $response->body = 'access_token=accessvalue&refresh_token=refreshvalue&expires_in=3600';
+
+ return $response;
+}
+
+/**
+ * Dummy
+ *
+ * @param string $url Path to the resource.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
+ * @param array $headers An array of name-value pairs to include in the header of the request
+ * @param integer $timeout Read timeout in seconds.
+ *
+ * @return JHttpResponse
+ *
+ * @since 12.2
+ */
+function jsonGrantOauthCallback($url, $data, array $headers = null, $timeout = null)
+{
+
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'application/json');
+ $response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';
+
+ return $response;
+}
+
+/**
+ * Dummy
+ *
+ * @param string $url Path to the resource.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
+ * @param array $headers An array of name-value pairs to include in the header of the request
+ * @param integer $timeout Read timeout in seconds.
+ *
+ * @return JHttpResponse
+ *
+ * @since 12.2
+ */
+function queryOauthCallback($url, $data, array $headers = null, $timeout = null)
+{
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'text/html');
+ $response->body = 'Lorem ipsum dolor sit amet.';
+
+ return $response;
+}
+
+/**
+ * Dummy
+ *
+ * @param string $url Path to the resource.
+ * @param array $headers An array of name-value pairs to include in the header of the request.
+ * @param integer $timeout Read timeout in seconds.
+ *
+ * @return JHttpResponse
+ *
+ * @since 12.2
+ */
+function getOauthCallback($url, array $headers = null, $timeout = null)
+{
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'text/html');
+ $response->body = 'Lorem ipsum dolor sit amet.';
+
+ return $response;
+}
Something went wrong with that request. Please try again.