kr-digital/oauth2-bundle

Symfony 扩展包,提供 OAuth 2.0 授权/资源服务器功能。这是一个为测试目的而创建的分支,直到原始扩展包支持 thephpleague/oauth2-server v8.0,它将一直被使用。

v2.0.1 2019-08-13 09:14 UTC

This package is auto-updated.

Last update: 2024-08-30 01:20:21 UTC


README

Build Status Latest Stable Version License Code coverage

Symfony 扩展包,提供 OAuth 2.0 授权/资源服务器功能。授权和资源服务器角色通过 thephpleague/oauth2-server 库实现。

重要提示

此扩展包提供了 thephpleague/oauth2-server 库和 Symfony 之间的“粘合剂”。它按照官方文档指定的方式实现了 thephpleague/oauth2-server 库。对于在 Symfony 项目中实施,请参阅 扩展包文档 和官方的 Symfony 安全文档

状态

该软件包目前正在积极开发中。

功能

  • 客户端授权和令牌发行的 API 端点
  • 可配置的客户端和令牌持久性(包括对 Doctrine 的支持)
  • 与 Symfony 的 Security 层集成

要求

安装

  1. 使用 Composer 需要该扩展包和一个 PSR 7/17 实现

    composer require trikoder/oauth2-bundle nyholm/psr7

    如果您的项目使用 Symfony Flex 管理,则无需执行以下步骤。请按照安装后的说明操作!🎉

    注意: 此扩展包需要 PSR 7/17 实现才能运行。我们建议您使用 nyholm/psr7。如果您希望使用不同的实现,请参阅此 文档

  2. config/packages/trikoder_oauth2.yaml 下创建扩展包配置文件。以下是一个参考配置文件

    trikoder_oauth2:
        authorization_server: # Required
    
            # Full path to the private key file.
            # How to generate a private key: https://oauth2.thephpleague.com/installation/#generating-public-and-private-keys
            private_key:          ~ # Required, Example: /var/oauth/private.key
    
            # Passphrase of the private key, if any
            private_key_passphrase: null
    
            # The plain string or the ascii safe string used to create a Defuse\Crypto\Key to be used as an encryption key.
            # How to generate an encryption key: https://oauth2.thephpleague.com/installation/#string-password
            encryption_key:       ~ # Required
    
            # The type of value of 'encryption_key'
            encryption_key_type:  plain # One of "plain"; "defuse"
    
            # How long the issued access token should be valid for.
            # The value should be a valid interval: https://php.ac.cn/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
            access_token_ttl:     PT1H
    
            # How long the issued refresh token should be valid for.
            # The value should be a valid interval: https://php.ac.cn/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
            refresh_token_ttl:    P1M
    
            # How long the issued auth code should be valid for.
            # The value should be a valid interval: https://php.ac.cn/manual/en/dateinterval.construct.php#refsect1-dateinterval.construct-parameters
            auth_code_ttl:        PT10M
    
            # Whether to enable the client credentials grant
            enable_client_credentials_grant: true
    
            # Whether to enable the password grant
            enable_password_grant: true
    
            # Whether to enable the refresh token grant
            enable_refresh_token_grant: true
    
            # Whether to enable the authorization code grant
            enable_auth_code_grant: true
    
            # Whether to enable the implicit grant
            enable_implicit_grant: true
        resource_server:      # Required
    
            # Full path to the public key file
            # How to generate a public key: https://oauth2.thephpleague.com/installation/#generating-public-and-private-keys
            public_key:           ~ # Required, Example: /var/oauth/public.key
    
        # Scopes that you wish to utilize in your application.
        # This should be a simple array of strings.
        scopes:               []
    
        # Configures different persistence methods that can be used by the bundle for saving client and token data.
        # Only one persistence method can be configured at a time.
        persistence:          # Required
            doctrine:
    
                # Name of the entity manager that you wish to use for managing clients and tokens.
                entity_manager:       default
            in_memory:            ~
    
        # The priority of the event listener that converts an Exception to a Response
        exception_event_listener_priority: 10
    
        # Set a custom prefix that replaces the default 'ROLE_OAUTH2_' role prefix
        role_prefix:          ROLE_OAUTH2_
  3. config/bundles.php 中通过将其添加到数组来启用扩展包

    Trikoder\Bundle\OAuth2Bundle\TrikoderOAuth2Bundle::class => ['all' => true]
  4. 更新数据库,以便扩展包实体可以使用 Doctrine 持久化

    bin/console doctrine:schema:update --force
  5. 将路线导入到您的 config/routes.yaml 文件中

    oauth2:
        resource: '@TrikoderOAuth2Bundle/Resources/config/routes.xml'

您可以通过向 /token 端点发送 POST 请求来验证一切是否正常工作。

❮ 注意 ❯ 建议您控制对授权端点的访问,以便只有已登录用户才能批准授权请求。您应该检查您的 security.yml 文件。以下是一个示例配置

security:
    access_control:
        - { path: ^/authorize, roles: IS_AUTHENTICATED_REMEMBERED }

配置

开发

开发环境需要 Docker 18.03+ 和 Docker Compose 1.13+。

构建环境

请确保使用以下命令构建并更新您的 Docker 镜像

dev/bin/docker-compose build

注意:在开发过程中,您可以通过添加--build-arg PHP_VERSION=<version>参数来指定不同的PHP版本。

之后,安装项目开发所需的所有包

dev/bin/php composer install

测试

您可以使用以下命令运行测试套件

dev/bin/php composer test

调试

您可以使用以下命令运行调试器

dev/bin/php-debug vendor/bin/phpunit

请确保您的IDE已正确设置,更多信息请参阅专门的文档

代码检查

此包在开发过程中使用PHP CS Fixer实用工具强制执行PSR-2和Symfony代码标准。在提交任何代码之前,您可以使用此工具来修复任何潜在的规则违规

dev/bin/php composer lint

变更

所有包的版本发布都记录在CHANGELOG文件中。

报告问题

使用问题跟踪器报告您可能遇到的问题。

许可证

有关许可证权利和限制(MIT)的更多信息,请参阅LICENSE文件。