pe / symfony-bundle-oauth2-server
league/oauth2-server 的 Symfony 集成
dev-master / 1.0.x-dev
2018-09-05 05:41 UTC
Requires
- php: ^5.5.9|>=7.0.8
- league/oauth2-server: ^7.0 <7.1
- symfony/framework-bundle: ~3.4|~4.0
Requires (Dev)
Suggests
- guzzlehttp/psr7: For use Guzzle PSR7 based request converter
- zendframework/zend-diactoros: For use Zend Diactoros PSR7 based request converter
This package is auto-updated.
Last update: 2024-09-05 19:37:25 UTC
README
此捆绑包与 league/oauth2-server 集成。
安装
通过运行以下命令使用 Composer 安装库
composer require pe/symfony-bundle-oauth2-server
然后在您的内核中启用捆绑包
<?php // app/AppKernel.php class AppKernel { public function registerBundles() { $bundles = [ // ... new PE\Bundle\OAuth2ServerBundle\PEOAuth2ServerBundle(), // ... ]; } }
或者对于 Symfony 4.0
<?php // SF 4.0 config/bundles.php return [ PE\Bundle\OAuth2ServerBundle\PEOAuth2ServerBundle::class => ['all' => true], ];
配置
添加到您的配置中
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
创建实体
<?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; }
创建事件监听器以解析用户标识,示例
<?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()); } } }