phpBB

Development Wiki

OAuth Providers

From phpBB Development Wiki

Revision as of 04:43, 15 September 2013 by Hardolaf (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

phpBB 3.1 ships with a new authentcation provider: OAuth. This provider is based on Lusitanian/PHPoAuthLib.

Enabling an OAuth Provider

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 Class file

<?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 $configphpbb_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'];
    }
}

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.

services:
    auth.provider.oauth.service.bitly:
        class: phpbb_auth_provider_oauth_service_bitly
        arguments:
            - @config
            - @request
        tags:
            - { name: auth.provider.oauth.service }