phpBB

Development Wiki

Difference between revisions of "OAuth Providers"

From phpBB Development Wiki

(Enabling an OAuth Provider)
(Moved to new docs)
 
Line 1: Line 1:
phpBB 3.1 ships with a new [[Authentication providers|authentcation provider]]: OAuth. This provider is based on [https://github.com/Lusitanian/PHPoAuthLib Lusitanian/PHPoAuthLib].
+
This documentation has been moved.
  
== Enabling an OAuth Provider ==
+
[https://area51.phpbb.com/docs/dev/31x/extensions/tutorial_authentication.html#oauth-providers phpBB Development Documentation: OAuth Providers].
 
+
To enable a new OAuth service in phpBB you need only create two files in your extension. The class file which defines functionality necessary for phpBB to get the data it needs from the service, and the service file which allows phpBB to find the class. To find out how you should most likely make calls to the OAuh service, it is recommended that you refer to the included OAuth services and to the examples provided by Lusitanian/PHPoAuthLib.
+
 
+
The example files below show the minimum needed to enable an OAuth service in phpBB. They are copies of the bitly service implementation from phpBB3's develop branch.
+
 
+
=== The Class file ===
+
<php>
+
<?php
+
/**
+
*
+
* @package auth
+
* @copyright (c) 2013 phpBB Group
+
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+
*
+
*/
+
 
+
/**
+
* @ignore
+
*/
+
if (!defined('IN_PHPBB'))
+
{
+
exit;
+
}
+
 
+
/**
+
* Bitly OAuth service
+
*
+
* @package auth
+
*/
+
class phpbb_auth_provider_oauth_service_bitly extends phpbb_auth_provider_oauth_service_base
+
{
+
/**
+
* phpBB config
+
*
+
* @var phpbb_config
+
*/
+
protected $config;
+
 
+
/**
+
* phpBB request
+
*
+
* @var phpbb_request
+
*/
+
protected $request;
+
 
+
/**
+
* Constructor
+
*
+
* @param phpbb_config $config
+
* @param phpbb_request $request
+
*/
+
public function __construct(phpbb_config $config, phpbb_request $request)
+
{
+
$this->config = $config;
+
$this->request = $request;
+
}
+
 
+
/**
+
* {@inheritdoc}
+
*/
+
public function get_service_credentials()
+
{
+
return array(
+
'key' => $this->config['auth_oauth_bitly_key'],
+
'secret' => $this->config['auth_oauth_bitly_secret'],
+
);
+
}
+
 
+
/**
+
* {@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');
+
}
+
 
+
// This was a callback request from bitly, get the token
+
$this->service_provider->requestAccessToken($this->request->variable('code', ''));
+
 
+
// Send a request with it
+
$result = json_decode($this->service_provider->request('user/info'), true);
+
 
+
// Return the unique identifier returned from bitly
+
return $result['data']['login'];
+
}
+
 
+
/**
+
* {@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');
+
}
+
 
+
// Send a request with it
+
$result = json_decode($this->service_provider->request('user/info'), true);
+
 
+
// Return the unique identifier returned from bitly
+
return $result['data']['login'];
+
}
+
}
+
</php>
+
 
+
=== The Service File ===
+
 
+
In the service file, the name of the service must be in the form of "auth.provider.oauth.service.<service name>" in order for phpBB to properly recognize it.
+
 
+
<pre>
+
services:
+
    auth.provider.oauth.service.bitly:
+
        class: phpbb_auth_provider_oauth_service_bitly
+
        arguments:
+
            - @config
+
            - @request
+
        tags:
+
            - { name: auth.provider.oauth.service }
+
</pre>
+

Latest revision as of 18:26, 5 December 2016

This documentation has been moved.

phpBB Development Documentation: OAuth Providers.