femike/yii2-oauth2-server

PHP OAuth2 服务器 (Fork 自 Filsh/yii2-oauth2-server)

安装: 183

依赖关系: 0

建议: 0

安全性: 0

星标: 2

关注者: 2

分支: 167

类型:yii2-extension

2.0.3 2015-10-26 14:52 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:13:07 UTC


README

OAuth2 服务器的包装器(https://github.com/bshaffer/oauth2-server-php

安装

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

可以运行

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

或者在你的 composer.json 文件的 require 部分添加

"filsh/yii2-oauth2-server": "~2.0"

要使用此扩展,只需在你的应用程序配置中添加以下代码

'bootstrap' => ['oauth2'],
'modules' => [
    'oauth2' => [
        'class' => 'filsh\yii2\oauth2server\Module',
        'tokenParamName' => 'accessToken',
        'tokenAccessLifetime' => 3600 * 24,
        'storageMap' => [
            'user_credentials' => 'common\models\User',
        ],
        'grantTypes' => [
            'user_credentials' => [
                'class' => 'OAuth2\GrantType\UserCredentials',
            ],
            'refresh_token' => [
                'class' => 'OAuth2\GrantType\RefreshToken',
                'always_issue_new_refresh_token' => true
            ]
        ]
    ]
]

common\models\User - 实现 \OAuth2\Storage\UserCredentialsInterface 接口的用户模型,因此 OAuth2 凭据数据存储在用户表中

下一步,你应该运行迁移

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

此迁移创建 OAuth2 数据库模式和插入测试用户凭据 testclient:testpasshttp://fake/

向 urlManager 添加 URL 规则

'urlManager' => [
    'rules' => [
        'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
        ...
    ]
]

使用方法

要使用此扩展,只需为你的基本控制器添加行为

use yii\helpers\ArrayHelper;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter;
use filsh\yii2\oauth2server\filters\auth\CompositeAuth;

class Controller extends \yii\rest\Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => CompositeAuth::className(),
                'authMethods' => [
                    ['class' => HttpBearerAuth::className()],
                    ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'],
                ]
            ],
            'exceptionFilter' => [
                'class' => ErrorToExceptionFilter::className()
            ],
        ]);
    }
}

在网站控制器中创建用于授权码的 action authorize 操作

https://api.mysite.com/authorize?response_type=code&client_id=TestClient&redirect_uri=https://fake/

更多信息

/**
 * SiteController
 */
class SiteController extends Controller
{
    /**
     * @return mixed
     */
    public function actionAuthorize()
    {
        if (Yii::$app->getUser()->getIsGuest())
            return $this->redirect('login');
    
        /** @var $module \filsh\yii2\oauth2server\Module */
        $module = Yii::$app->getModule('oauth2');
        $response = $module->handleAuthorizeRequest(!Yii::$app->getUser()->getIsGuest(), Yii::$app->getUser()->getId());
    
        /** @var object $response \OAuth2\Response */
        Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
    
        return $response->getParameters();
    }
}

更多信息,请参阅 https://github.com/bshaffer/oauth2-server-php