binhvd / oauth2-server-bundle
Symfony OAuth2ServerBundle
Requires
- php: >=5.3
- binhvd/oauth2-server-httpfoundation-bridge: >=1.6
- binhvd/oauth2-server-php: >=1.12.0
Requires (Dev)
- doctrine/orm: ~2.4,>=2.4.5
- symfony/symfony: >=3
This package is not auto-updated.
Last update: 2024-10-03 00:11:33 UTC
README
基于 oauth2-server-php 库构建的 Symfony OAuth2ServerBundle,用于实现 OAuth2.0 协议。
入门
有关 OAuth2.0 协议和此捆绑包使用的 PHP 库的信息,请参阅完整文档。
有关此捆绑包的特定文档,请继续阅读以下内容。
捆绑包概述
以下授权类型支持直接使用
- 客户端凭证
- 授权码
- 刷新令牌
- 用户凭证(见下文)
您可以通过 POST 请求向 /token
路径发送令牌请求。
您可以在数据库中限制每个客户端可用的授权类型,使用编译器传递或在您自己的 TokenController 中,您可以执行如下操作
public function tokenAction() { $server = $this->get('oauth2.server'); // Override default grant types to authorization code only $server->addGrantType($this->get('oauth2.grant_type.authorization_code')); return $server->handleTokenRequest($this->get('oauth2.request'), $this->get('oauth2.response')); }
安装
步骤 1:将包添加到 Composer
使用 composer 添加依赖关系并通过运行以下命令下载它
$ php composer.phar require bshaffer/oauth2-server-bundle
Composer 将更新您的 composer.json 并将捆绑包安装到您的项目的 vendor/bshaffer
目录。
步骤 2:启用捆绑包
在内核中启用捆绑包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new OAuth2\ServerBundle\OAuth2ServerBundle(), ); }
步骤 3:安装数据库
您需要更新您的模式以设置此模块提供的实体。
$ php app/console doctrine:schema:update --force
步骤 4:添加路由
您需要将以下内容添加到您的 routing.yml 中
# app/config/routing.yml oauth2_server: resource: "@OAuth2ServerBundle/Controller/" type: annotation prefix: /
步骤 5:创建作用域
在创建客户端之前,您需要设置作用域,使用此命令。您在这里提供的描述将在授权页面上显示。
$ php app/console OAuth2:CreateScope scope (description)
步骤 6:创建客户端
使用此控制台命令创建新的客户端
$ php app/console OAuth2:CreateClient client_id redirect_uri (grant_types) (scope)
可选配置
您可以通过在 config.yml 中添加新参数来覆盖您自己的捆绑包中的任何内置组件
# app/config/config.yml parameters: oauth2.storage.client_credentials.class: Amce\OAuth2ServerBundle\Storage\ClientCredentials
其中 Amce\OAuth2ServerBundle\Storage\ClientCredentials
是您的 ClientCredentials 接口的实现。
如果您提供自己的存储管理器,则可以将一切连接到您自己的自定义实体。
用户凭证(资源所有者密码)
为了便于插入您自己的用户提供程序,我们已符合 UserInterface
、UserProviderInterface
& EncoderFactoryInterface
。
因此,要正确使用用户凭证授权类型,您需要使用相关的类修改您的 config.yml。
# app/config/config.yml parameters: oauth2.user_provider.class: Amce\OAuth2ServerBundle\User\OAuth2UserProvider
如果您想利用基于每个用户的作用域限制,则您的用户实体需要实现 OAuth2\ServerBundle\OAuth2UserInterface
或 OAuth2\ServerBundle\AdvancedOAuth2UserInterface
。
我们提供基本的用户提供程序和实体以供您使用。设置您的 security.yml 以使用它
# app/config/security.yml security: encoders: OAuth2\ServerBundle\Entity\User: algorithm: sha512 encode_as_base64: true iterations: 5000 providers: oauth2: id: oauth2.user_provider
但是您需要一些用户!使用控制台命令创建新用户
$ php app/console OAuth2:CreateUser username password
配置授权类型
您需要使用编译器传递来配置授权类型的设置。例如,如果我们想始终续订刷新令牌
// Amce/OAuth2ServerBundle/AmceOAuth2ServerBundle.php namespace Amce\OAuth2ServerBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Amce\OAuth2ServerBundle\DependencyInjection\Compiler\OAuth2CompilerPass; class AmceOAuth2ServerBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass(new OAuth2CompilerPass()); } }
// Amce/OAuth2ServerBundle/DependencyInjection\Compiler\OAuth2CompilerPass.php namespace Amce\OAuth2ServerBundle\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Reference; class OAuth2CompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { // Override Refresh Token Grant Type Settings $serviceId = 'oauth2.grant_type.refresh_token'; if ($container->hasDefinition($serviceId)) { $definition = $container->getDefinition($serviceId); $definition->replaceArgument(1, array( 'always_issue_new_refresh_token' => TRUE )); } } }