1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

[ticket/16008] Clean up phpBB OAuth system

PHPBB3-16008
This commit is contained in:
mrgoldy
2019-05-05 18:26:43 +02:00
committed by Marc Alexander
parent 78ce646c69
commit 0b39e4e854
13 changed files with 1103 additions and 882 deletions

View File

@@ -1,51 +1,59 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* Base OAuth abstract class that all OAuth services should implement
*/
abstract class base implements \phpbb\auth\provider\oauth\service\service_interface
* Base OAuth abstract class that all OAuth services should implement
*/
abstract class base implements service_interface
{
/**
* External OAuth service provider
*
* @var \OAuth\Common\Service\ServiceInterface
*/
* External OAuth service provider
*
* @var \OAuth\Common\Service\ServiceInterface
*/
protected $service_provider;
/**
* {@inheritdoc}
*/
public function get_external_service_provider()
{
return $this->service_provider;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_auth_scope()
{
return array();
return [];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_external_service_class()
{
return '';
}
/**
* {@inheritdoc}
*/
public function set_external_service_provider(\OAuth\Common\Service\ServiceInterface $service_provider)
{
$this->service_provider = $service_provider;
}
/**
* {@inheritdoc}
*/
public function get_external_service_provider()
{
return $this->service_provider;
}
}

View File

@@ -1,94 +1,107 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* Bitly OAuth service
*/
class bitly extends \phpbb\auth\provider\oauth\service\base
* Bitly OAuth service
*/
class bitly extends base
{
/**
* phpBB config
*
* @var \phpbb\config\config
*/
/** @var \phpbb\config\config */
protected $config;
/**
* phpBB request
*
* @var \phpbb\request\request_interface
*/
/** @var \phpbb\request\request_interface */
protected $request;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\request\request_interface $request
*/
* Constructor.
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\request\request_interface $request Request object
*/
public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
{
$this->config = $config;
$this->request = $request;
$this->config = $config;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_service_credentials()
{
return array(
return [
'key' => $this->config['auth_oauth_bitly_key'],
'secret' => $this->config['auth_oauth_bitly_secret'],
);
];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_auth_login()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Bitly))
{
throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// This was a callback request from bitly, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
try
{
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
}
catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Send a request with it
$result = json_decode($this->service_provider->request('user/info'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('user/info'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier returned from bitly
return $result['data']['login'];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_token_auth()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Bitly))
{
throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// Send a request with it
$result = json_decode($this->service_provider->request('user/info'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('user/info'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier returned from bitly
// Return the unique identifier
return $result['data']['login'];
}
}

View File

@@ -1,63 +1,55 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* Facebook OAuth service
*/
* Facebook OAuth service
*/
class facebook extends base
{
/**
* phpBB config
*
* @var \phpbb\config\config
*/
/** @var \phpbb\config\config */
protected $config;
/**
* phpBB request
*
* @var \phpbb\request\request_interface
*/
/** @var \phpbb\request\request_interface */
protected $request;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\request\request_interface $request
*/
* Constructor.
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\request\request_interface $request Request object
*/
public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
{
$this->config = $config;
$this->request = $request;
$this->config = $config;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_service_credentials()
{
return array(
return [
'key' => $this->config['auth_oauth_facebook_key'],
'secret' => $this->config['auth_oauth_facebook_secret'],
);
];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_auth_login()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Facebook))
@@ -65,19 +57,33 @@ class facebook extends base
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
try
{
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
}
catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Send a request with it
$result = json_decode($this->service_provider->request('/me'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('/me'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier
return $result['id'];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_token_auth()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Facebook))
@@ -85,8 +91,15 @@ class facebook extends base
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// Send a request with it
$result = json_decode($this->service_provider->request('/me'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('/me'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier
return $result['id'];

View File

@@ -1,74 +1,66 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* Google OAuth service
*/
* Google OAuth service
*/
class google extends base
{
/**
* phpBB config
*
* @var \phpbb\config\config
*/
/** @var \phpbb\config\config */
protected $config;
/**
* phpBB request
*
* @var \phpbb\request\request_interface
*/
/** @var \phpbb\request\request_interface */
protected $request;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\request\request_interface $request
*/
* Constructor.
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\request\request_interface $request Request object
*/
public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
{
$this->config = $config;
$this->request = $request;
$this->config = $config;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_auth_scope()
{
return array(
return [
'userinfo_email',
'userinfo_profile',
);
];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_service_credentials()
{
return array(
return [
'key' => $this->config['auth_oauth_google_key'],
'secret' => $this->config['auth_oauth_google_secret'],
);
];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_auth_login()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Google))
@@ -76,19 +68,33 @@ class google extends base
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
try
{
// This was a callback request, get the token
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
}
catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Send a request with it
$result = json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier
return $result['id'];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_token_auth()
{
if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Google))
@@ -96,8 +102,15 @@ class google extends base
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// Send a request with it
$result = json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
try
{
// Send a request with it
$result = (array) json_decode($this->service_provider->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
}
catch (\OAuth\Common\Exception\Exception $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Return the unique identifier
return $result['id'];

View File

@@ -1,73 +1,87 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* OAuth service interface
*/
* OAuth service interface
*/
interface service_interface
{
/**
* Returns an array of the scopes necessary for auth
*
* @return array An array of the required scopes
*/
* Returns an array of the scopes necessary for auth
*
* @return array An array of the required scopes
*/
public function get_auth_scope();
/**
* Returns the external library service provider once it has been set
*
* @param \OAuth\Common\Service\ServiceInterface|null
*/
public function get_external_service_provider();
/**
* Returns an array containing the service credentials belonging to requested
* service.
*
* @return array An array containing the 'key' and the 'secret' of the
* service in the form:
* array(
* 'key' => string
* 'secret' => string
* )
*/
* Returns an array containing the service credentials belonging to requested
* service.
*
* @return array An array containing the 'key' and the 'secret' of the
* service in the form:
* array(
* 'key' => string
* 'secret' => string
* )
*/
public function get_service_credentials();
/**
* Returns the results of the authentication in json format
*
* @throws \phpbb\auth\provider\oauth\service\exception
* @return string The unique identifier returned by the service provider
* that is used to authenticate the user with phpBB.
*/
* Returns the results of the authentication in json format
*
* @throws \phpbb\auth\provider\oauth\service\exception
* @return string The unique identifier returned by the service provider
* that is used to authenticate the user with phpBB.
*/
public function perform_auth_login();
/**
* Returns the results of the authentication in json format
* Use this function when the user already has an access token
*
* @throws \phpbb\auth\provider\oauth\service\exception
* @return string The unique identifier returned by the service provider
* that is used to authenticate the user with phpBB.
*/
* Returns the results of the authentication in json format
* Use this function when the user already has an access token
*
* @throws \phpbb\auth\provider\oauth\service\exception
* @return string The unique identifier returned by the service provider
* that is used to authenticate the user with phpBB.
*/
public function perform_token_auth();
/**
* Sets the external library service provider
*
* @param \OAuth\Common\Service\ServiceInterface $service_provider
*/
* Returns the class of external library service provider that has to be used.
*
* @return string If the string is a class, it will register the provided string as a class,
* which later will be generated as the OAuth external service provider.
* If the string is not a class, it will use this string,
* trying to generate a service for the version 2 and 1 respectively:
* \OAuth\OAuth2\Service\<string>
* If the string is empty, it will default to OAuth's standard service classes,
* trying to generate a service for the version 2 and 1 respectively:
* \OAuth\OAuth2\Service\Facebook
*/
public function get_external_service_class();
/**
* Sets the external library service provider
*
* @param \OAuth\Common\Service\ServiceInterface $service_provider
*/
public function set_external_service_provider(\OAuth\Common\Service\ServiceInterface $service_provider);
/**
* Returns the external library service provider once it has been set
*
* @param \OAuth\Common\Service\ServiceInterface|null
*/
public function get_external_service_provider();
}

View File

@@ -1,102 +1,111 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\auth\provider\oauth\service;
/**
* Twitter OAuth service
*/
class twitter extends \phpbb\auth\provider\oauth\service\base
* Twitter OAuth service
*/
class twitter extends base
{
/**
* phpBB config
*
* @var \phpbb\config\config
*/
/** @var \phpbb\config\config */
protected $config;
/**
* phpBB request
*
* @var \phpbb\request\request_interface
*/
/** @var \phpbb\request\request_interface */
protected $request;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\request\request_interface $request
*/
* Constructor.
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\request\request_interface $request Request object
*/
public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
{
$this->config = $config;
$this->request = $request;
$this->config = $config;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function get_service_credentials()
{
return array(
return [
'key' => $this->config['auth_oauth_twitter_key'],
'secret' => $this->config['auth_oauth_twitter_secret'],
);
];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_auth_login()
{
if (!($this->service_provider instanceof \OAuth\OAuth1\Service\Twitter))
{
throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
$storage = $this->service_provider->getStorage();
$token = $storage->retrieveAccessToken('Twitter');
$tokensecret = $token->getRequestTokenSecret();
// This was a callback request from twitter, get the token
$this->service_provider->requestAccessToken(
$this->request->variable('oauth_token', ''),
$this->request->variable('oauth_verifier', ''),
$tokensecret
);
try
{
/** @var \OAuth\OAuth1\Token\TokenInterface $token */
$token = $storage->retrieveAccessToken('Twitter');
}
catch (\OAuth\Common\Storage\Exception\TokenNotFoundException $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
$secret = $token->getRequestTokenSecret();
try
{
// This was a callback request, get the token
$this->service_provider->requestAccessToken(
$this->request->variable('oauth_token', ''),
$this->request->variable('oauth_verifier', ''),
$secret
);
}
catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
{
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
}
// Send a request with it
$result = json_decode($this->service_provider->request('account/verify_credentials.json'), true);
$result = (array) json_decode($this->service_provider->request('account/verify_credentials.json'), true);
// Return the unique identifier returned from twitter
// Return the unique identifier
return $result['id'];
}
/**
* {@inheritdoc}
*/
* {@inheritdoc}
*/
public function perform_token_auth()
{
if (!($this->service_provider instanceof \OAuth\OAuth1\Service\Twitter))
{
throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
throw new exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
}
// Send a request with it
$result = json_decode($this->service_provider->request('account/verify_credentials.json'), true);
$result = (array) json_decode($this->service_provider->request('account/verify_credentials.json'), true);
// Return the unique identifier returned from twitter
// Return the unique identifier
return $result['id'];
}
}