mobilejazz / yii2-oauth2-server
此包已被废弃,不再维护。未建议替代包。
PHP OAuth2 服务器
2.1.0
2017-05-04 21:05 UTC
Requires
This package is not auto-updated.
Last update: 2023-09-06 23:20:55 UTC
README
实现 OAuth2 服务器的包装器(https://github.com/bshaffer/oauth2-server-php)
安装
通过 composer 安装此扩展是首选方式。
运行以下命令之一
php composer.phar require --prefer-dist mobilejazz/yii2-oauth2-server "*"
或者添加
"mobilejazz/yii2-oauth2-server": "~2.1"
到你的 composer.json 的 require 部分。
要使用此扩展,只需在应用程序配置中添加以下代码
'oauth2' => [ 'class' => 'mobilejazz\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/mobilejazz/yii2-oauth2-server/migrations
此迁移创建 OAuth2 数据库模式并插入测试用户凭证 testclient:testpass
用于 http://fake/
添加 URL 规则到 urlManager
'urlManager' => [ 'rules' => [ 'POST oauth2/<action:\w+>' => 'oauth2/default/<action>', ... ] ]
使用
要使用此扩展,只需为你的基础控制器添加行为
use yii\helpers\ArrayHelper; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; use mobilejazz\yii2\oauth2server\filters\ErrorToExceptionFilter; use mobilejazz\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() ], ]); } }