bshaffer/oauth2-server-bundle

Symfony OAuth2ServerBundle

安装次数: 187,443

依赖者: 1

建议者: 0

安全性: 0

星标: 106

关注者: 18

分支: 72

开放问题: 15

类型:symfony-bundle

v0.4 2017-01-09 19:38 UTC

This package is auto-updated.

Last update: 2024-08-27 02:13:31 UTC


README

基于 oauth2-server-php 库构建的,用于 Symfony 2 的 OAuth2 服务器组件包。

Build Status

入门指南

有关 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 接口实现。

如果您提供了自己的存储管理器,则可以将一切连接到您自己的自定义实体。

用户凭据(资源所有者密码)

为了便于连接您自己的用户提供者,我们已遵循 UserInterfaceUserProviderInterfaceEncoderFactoryInterface

因此,要正确使用用户凭据授权类型,您需要使用相关类修改您的 config.yml。

# app/config/config.yml

parameters:
    oauth2.user_provider.class: Amce\OAuth2ServerBundle\User\OAuth2UserProvider

如果您想利用基于每个用户的作用域限制,则您的用户实体需要实现 OAuth2\ServerBundle\OAuth2UserInterfaceOAuth2\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
            ));
        }
    }
}