Skip to content
Snippets Groups Projects
pirati.php 3.86 KiB
<?php
/**
*
* This file is not a part of the phpBB Forum Software package.
*
* @copyleft (c) Czech Pirates Party <https://www.pirati.cz>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/


namespace pirates\pirid;

/**
* Pirati OAuth service
*/
class pirati extends \phpbb\auth\provider\oauth\service\base
{

    /**
    * phpBB config
    *
    * @var \phpbb\config\config
    */
    protected $config;

    /**
    * phpBB request
    *
    * @var \phpbb\request\request_interface
    */
    protected $request;

    /**
    * Constructor
    *
    * @param    \phpbb\config\config               $config
    * @param    \phpbb\request\request_interface   $request
    */
    public function __construct(\phpbb\config\config $config, \phpbb\request\request_interface $request)
    {

	    $this->config = $config;
        $this->request = $request;

	    global $phpbb_container;
        $language = $phpbb_container->get('language');
        $language->add_lang(array('common', 'acp/common'), 'pirates/pirid');

    }

    public function get_external_service_class()
    {
            return 'Pirati';
    }

    /**
     * {@inheritdoc}
     */
    public function get_auth_scope()
    {
            return [
                   'openid',
                   'email',
            ];
    }

    /**
    * {@inheritdoc}
    */
    public function get_service_credentials()
    {
        return array(
            'key'     => $this->config['auth_oauth_pirid_key'],
            'secret'  => $this->config['auth_oauth_pirid_secret'],
        );
    }


    function octopussy_callback( $uid )
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://chobotnice.pirati.cz/import-person/" . $uid . "/");
        curl_exec($ch);
        curl_close($ch);
    }


    /**
    * {@inheritdoc}
    */
    public function perform_auth_login()
    {
      if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Pirati))
        {
            throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
        }

        try
        {
                $this->service_provider->requestAccessToken($this->request->variable('code', ''));
        }
        catch (\OAuth\Common\Http\Exception\TokenResponseException $e)
        {
                throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
        }

        try
        {
                // Send a request with it
                $result = (array) json_decode($this->service_provider->request('protocol/openid-connect/userinfo'), true);
        }
        catch (\OAuth\Common\Exception\Exception $e)
        {
                throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
        }

        if (!isset($result['preferred_username']))
        {
                throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_RETURN_ERROR');
        }

        $this->octopussy_callback($result['sub']);
        return $result['sub'];

    }

    /**
    * {@inheritdoc}
    */
    public function perform_token_auth()
    {
        if (!($this->service_provider instanceof \OAuth\OAuth2\Service\Pirati))
        {
            throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_ERROR_INVALID_SERVICE_TYPE');
        }

        try
        {
                $result = (array) json_decode($this->service_provider->request('protocol/openid-connect/userinfo'), true);
        }
        catch (\OAuth\Common\Exception\Exception $e)
        {
                throw new exception('AUTH_PROVIDER_OAUTH_ERROR_REQUEST');
        }

        if (!isset($result['preferred_username']))
        {
                throw new \phpbb\auth\provider\oauth\service\exception('AUTH_PROVIDER_OAUTH_RETURN_ERROR');
        }

        $this->octopussy_callback($result['sub']);
        return $result['sub'];

    }
}