waystocom/oauth2-server-bundle

Symfony 4 OAuth2ServerBundle

安装: 262

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 0

开放问题: 0

类型:symfony-bundle

v2.0.0 2024-03-25 14:20 UTC

This package is auto-updated.

Last update: 2024-09-25 15:24:45 UTC


README

Fork of OAuth2 Server Bundle for Symfony 3, built on the oauth2-server-php library.

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添加要求并运行以下命令下载

$ composer require micropole_idea/oauth2-server-bundle

Composer将更新您的composer.json并在您的项目的vendor/bshaffer目录中安装包。

步骤2:启用包

在内核中启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new OAuth2ServerBundle\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接口的实现。

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

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

为了方便集成您自己的User Provider,我们已遵守UserInterfaceUserProviderInterface & EncoderFactoryInterface

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

# app/config/config.yml

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

如果要在每个用户的基础上利用作用域限制,则您的User实体需要实现OAuth2ServerBundle\OAuth2UserInterfaceOAuth2ServerBundle\AdvancedOAuth2UserInterface

我们直接提供了一些基本的用户提供者和实体供您使用。设置您的security.yml以使用它

# app/config/security.yml

security:
    encoders:
        OAuth2ServerBundle\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
            ));
        }
    }
}