macfly/yii2-oauth2-server

该软件包已被废弃,不再维护。未建议替代软件包。

filsh yii2-oauth2-server 的包装器,提供授权、令牌、用户控制器

安装次数: 2,375

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 1

分支: 2

开放问题: 1

类型:yii2-extension

0.2.1 2017-11-17 17:41 UTC

This package is auto-updated.

Last update: 2023-04-13 03:41:38 UTC


README

Filsh/yii2-oauth2server 的包装器,实现了一个 OAuth2 服务器

添加缺失的代码,使其易于与像来自 dektrium/yii2-user 的社交网络感知用户模块一起使用,您可以使用 macfly/yii2-authclient-oauth2,它无需额外配置即可与之一起使用。

添加控制器

  • 授权
  • 令牌
  • 用户(提供用户信息:id、用户名、电子邮件等)

/!\ 与 Yii 框架 2.0.13 版本一起使用时,存在一个 问题,您可以通过更新您的 composer.json 来修复它

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/Marty-Macfly/yii2-oauth2-server-1"
        }
    ],

...

    "require": {

...

        "filsh/yii2-oauth2-server": "2.0.2.x-dev",

...

    },

...

安装

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

运行以下命令

php composer.phar require --prefer-dist macfly/yii2-oauth2-server "*"

或将其添加到您的 composer.json 的 require 部分。

"macfly/yii2-oauth2-server": "*"

要使用此扩展,只需在您的应用程序配置中添加以下代码作为新的模块

可以在请求组件中启用 json 解析器美观的 URL

'modules'=>[
    //other modules .....
    'oauth2' => [
        'class' => 'macfly\oauth2server\Module',
        'tokenParamName' => 'accessToken',
        'tokenAccessLifetime' => 3600 * 24, // Default token lifetime
        'userModel' => 'app\models\User',
        'userAttributes' => [ // List of user attributes you want to provide through the /oauth2/user api call
            'id',
            'username',
            'email',
        ],
    ]
    // yii2-oauth2-server is using the kartik\datecontrol\Module so you should define the configuration of the module
    // See more details at http://demos.krajee.com/datecontrol
    'datecontrol' =>  [
        'class'             => 'kartik\datecontrol\Module',
        'ajaxConversion'    => true,
        'autoWidget'        => true,
        'saveTimezone'      => 'UTC',
    ],
],

此外,扩展 app\models\User - 用户模型 - 实现接口 \OAuth2\Storage\UserCredentialsInterface,以便将 OAuth2 凭据数据存储在用户表中。

您应该实现以下内容(为了方便,提供了一种特质)

findIdentityByAccessToken()

  • checkUserCredentials()
  • getUserDetails()
  • getAuthKey()
  • getOauthClient() 将一个用户映射到一个 OAuth 客户端。
  • 您可以扩展模型,如果您愿意(请记住更新配置文件)

下一步,您应该运行迁移

use Yii;

class User extends app\models\User implements \OAuth2\Storage\UserCredentialsInterface
{
	use \macfly\oauth2server\traits\Oauth2User;
}

此迁移创建 OAuth2 数据库模式并插入测试用户凭据 testclient:testpass 用于 http://127.0.0.1:8888/user/security/auth?authclient=oauth2

yii migrate --migrationPath=@vendor/macfly/yii2-oauth2-server/src/migrations

使用方法

可用操作列表

/oauth2/authorize:通过浏览器进行会话验证

管理界面

管理客户端凭据

/oauth2/clients

  • CRUD 操作

/oauth2/clients/index

  • /oauth2/clients/create
  • /oauth2/clients/update
  • /oauth2/clients/delete
  • /oauth2/clients/index

管理访问令牌

  • /oauth2/访问令牌

/oauth2/clients/index

  • /oauth2/访问令牌/索引
  • /oauth2/访问令牌/创建
  • /oauth2/访问令牌/更新
  • /oauth2/访问令牌/删除