tamtamp/oauth2-server-bundle

Symfony OAuth2ServerBundle

安装次数: 3,692

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 72

类型:symfony-bundle

v0.1 2022-12-27 11:06 UTC

This package is auto-updated.

Last update: 2024-09-27 14:41:48 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 接口的实现。

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

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

为了便于插入您自己的用户提供者,我们已符合 UserInterfaceUserProviderInterface & EncoderFactoryInterface

因此,为了正确使用用户凭据授权类型,您需要修改您的 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
            ));
        }
    }
}