bshaffer / oauth2-server-php

A library for implementing an OAuth2 Server in php
http://bshaffer.github.io/oauth2-server-php-docs
MIT License
3.26k stars 950 forks source link

Server Bootstrap with Symfony #840

Open wimpog opened 7 years ago

wimpog commented 7 years ago

If I want to bootstrap the server in my Symfony application, where would I do that?

https://bshaffer.github.io/oauth2-server-php-docs/cookbook/ the "Bootstrap your OAuth2 Server" section.

Thanks

onward85 commented 7 years ago

I am baffled at this step as well.

svycka commented 7 years ago

https://github.com/bshaffer/oauth2-server-bundle

wimpog commented 7 years ago

I use Symfony 3.3 and PostgreSQL database. I was able to bootstrap it. I created an OAuth2ServerBootStrap service:

<?php
/**
 * File OAuth2ServerBootStrap.php
 */

namespace AppBundle\Service;

use OAuth2\GrantType\AuthorizationCode;
use OAuth2\GrantType\ClientCredentials;
use OAuth2\Server;
use OAuth2\Storage\Pdo;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class OAuth2ServerBootStrap.
 */
class OAuth2ServerBootStrap
{
    /**
     * @var Server
     */
    protected $server;

    /**
     * @var Pdo
     */
    protected $storage;

    /**
     * @var string
     */
    protected $dsn;

    /**
     * @var string
     */
    protected $username;

    /**
     * @var string
     */
    protected $password;

    /**
     * OAuth2ServerBootStrap constructor.
     *
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        # Configure the DSN
        $this->dsn  = 'pgsql:';
        $this->dsn .= 'host=' . $this->container->getParameter('database_host') . ';';
        $this->dsn .= 'port=' . $this->container->getParameter('database_port') . ';';
        $this->dsn .= 'dbname=' . $this->container->getParameter('database_name') . ';';

        # Get credentials
        $this->username = $this->container->getParameter('database_user');
        $this->password = $this->container->getParameter('database_password');

        // $dsn is the Data Source Name for your database, for exanple "mysql:dbname=my_oauth2_db;host=localhost"
        $this->storage = new Pdo(
            array(
                'dsn' => $this->dsn,
                'username' => $this->username,
                'password' => $this->password
            )
        );

        // Pass a storage object or array of storage objects to the OAuth2 server class
        $this->server = new Server($this->storage);

        // Add the "Client Credentials" grant type (it is the simplest of the grant types)
        $this->server->addGrantType(new ClientCredentials($this->storage));

        // Add the "Authorization Code" grant type (this is where the oauth magic happens)
        $this->server->addGrantType(new AuthorizationCode($this->storage));
    }

    /**
     * @return Server
     */
    public function getServer()
    {
        return $this->server;
    }

    /**
     * @return Pdo
     */
    public function getStorage()
    {
        return $this->storage;
    }

    /**
     * @return string
     */
    public function getDsn()
    {
        return $this->dsn;
    }
}

I registered it in service.yml:

AppBundle\Service\OAuth2ServerBootStrap:
        public: true

I then created a RequestListener class which listens to kernel events and registered it as a service:

AppBundle\EventListener\RequestListener:
        arguments: ['@doctrine.orm.entity_manager', '@service_container']
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

See: http://stackoverflow.com/questions/11771368/how-can-i-access-entity-manager-if-i-create-custom-event-in-symfony2 and http://symfony.com/doc/current/event_dispatcher/before_after_filters.html on how to create it.

In RequestListener class in its onKernelController() function I bootstrap the OAuth2 Server:

/** @var OAuth2ServerBootStrap $oAuth2ServerBootStrapService */
$oAuth2ServerBootStrapService = $this->container->get('AppBundle\Service\OAuth2ServerBootStrap');

/** @var Server $server */
$server = $oAuth2ServerBootStrapService->getServer();

And then you can use the OAuth2 Server. I was able to create a TokenController and VerifyController and have them function. At the moment I'm stuck at connecting this server to the Symfony's firewall.