pe/symfony-bundle-oauth2-server

Symfony integration of league/oauth2-server

dev-master / 1.0.x-dev 2018-09-05 05:41 UTC

This package is auto-updated.

Last update: 2024-11-05 19:58:27 UTC


README

This bundle integrates with league/oauth2-server.

Installation

Install the library via Composer by running the following command:

composer require pe/symfony-bundle-oauth2-server

Then enable the bundle in your kernel:

<?php
// app/AppKernel.php
class AppKernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new PE\Bundle\OAuth2ServerBundle\PEOAuth2ServerBundle(),
            // ...
        ];
    }
}

or for Symfony 4.0

<?php
// SF 4.0 config/bundles.php

return [
    PE\Bundle\OAuth2ServerBundle\PEOAuth2ServerBundle::class => ['all' => true],
];

Configuration

Add to your config

pe_oauth2_server:
    driver: orm
    class:
        access_token:  App\Entity\AccessToken
        auth_code:     App\Entity\AuthorizationCode
        client:        App\Entity\Client
        refresh_token: App\Entity\RefreshToken
        scope:         App\Entity\Scope
    login_path: /login/ # This must be login path from configured firewall
    name: Some Auth Server # This required for identifying server

Create entities

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_access_tokens")
 */
class AccessToken extends \PE\Bundle\OAuth2ServerBundle\Model\AccessToken
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $identifier;
}

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_authorization_codes")
 */
class AuthorizationCode extends \PE\Bundle\OAuth2ServerBundle\Model\AuthorizationCode
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $identifier;
}

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_clients")
 */
class Client extends \PE\Bundle\OAuth2ServerBundle\Model\Client
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $identifier;
}

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_refresh_tokens")
 */
class RefreshToken extends \PE\Bundle\OAuth2ServerBundle\Model\RefreshToken
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $identifier;
}

/**
 * @ORM\Entity()
 * @ORM\Table(name="oauth2_scopes")
 */
class Scope extends \PE\Bundle\OAuth2ServerBundle\Model\Scope
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="guid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @var string
     */
    protected $identifier;
}

Create event listeners to resolve user identifier, example:

<?php

namespace App\EventSubscriber;

use PE\Bundle\OAuth2ServerBundle\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OAuth2ServerSubscriber implements EventSubscriberInterface
{
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            UserEvent::GET_USER_BY_CREDENTIALS => 'onGetUserByCredentials',
            UserEvent::GET_USER_BY_OBJECT      => 'onGetUserByObject',
        ];
    }
    
    public function onGetUserByCredentials(UserEvent $event)
    {
        $user = $this->someRepository->findUserByUsername($event->getUsername());
        
        if ($user) {
            $event->setIdentifier($user->getId());
        }
    }
    
    public function onGetUserByObject(UserEvent $event)
    {
        if ($user = $event->getObject()) {
            $event->setIdentifier($user->getId());
        }
    }
}