wolfpack-it/yii2-oauth

Yii2 OAuth2 服务器实现 https://oauth2.thephpleague.com/

v2.2.0 2024-03-21 12:01 UTC

README

本扩展为 Yii2 框架提供 The PHP League: OAuth 2.0 Server 模块。

The PHP League: OAuth 2.0 Server 是一个使设置 OAuth2 服务器变得简单的包。

安装

安装此扩展的首选方式是通过 composer

运行

$ composer require wolfpack-it/yii2-oauth

或将

"wolfpack-it/yii2-oauth": "^<latest version>"

添加到您的 composer.json 文件的 require 部分。

配置

配置模块

基本配置是应用程序中的一个模块

'modules' => [
    'oauth' => [
        'class' => \WolfpackIT\oauth\Module::class,
        'userClass' => '<class of ActiveRecordUser implementing UserEntityInterface>',
        'db' => 'db', // component that should be used for the database connection
        'publicKey' => '<path to public key file te be used by CryptKey, or configuration>',
        'privateKey' => '<path to private key file te be used by CryptKey, or configuration>',
        'encryptionKey' => '<random string for encryption>',
        'tokenValidationLeeway' => null|int, // The number of seconds to use as leeway when validating access tokens
    ]
]

有关完整的配置选项,请参阅 Module 的公共变量。

如何生成密钥的示例可以在 此处 找到。

每次通过 DI 注入 AccessTokenService 时,都需要将模块添加到应用程序的引导中。

运行迁移

可以通过将它们添加到 迁移命名空间来自动运行迁移。

'migrationNamespaces' => [
    'WolfpackIT\oauth\migrations',
    'console\migrations',
]

为此,您需要添加别名

'aliases' => [
    '@WolfpackIT/oauth' => '@vendor/wolfpack-it/yii2-oauth/src',
]

如果您想覆盖迁移中的数据库连接,您还需要在控制台中引导 oauth 模块。

添加路由

为了使模块可访问,请确保在 urlManager 中设置了正确的路由。

例如,当您的模块名为 oauth

'urlManager' => [
    'rules' => [
        'oauth/<controller:[\w-]+>/<action:[\w-]+>' => 'oauth/<controller>/<action>'
    ]
]

待办事项

  • 添加测试

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE