landrain / yii2-oauth2-server
PHP 的 OAuth2 服务器
dev-master
2019-03-05 11:46 UTC
Requires
This package is not auto-updated.
Last update: 2020-08-05 22:24:10 UTC
README
一个用于实现 OAuth2 服务器的包装器(https://github.com/bshaffer/oauth2-server-php)
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist landrain/yii2-oauth2-server "*"
或者
"landrain/yii2-oauth2-server": "*"
将以下内容添加到您的 composer.json 文件的 require 部分。
要使用此扩展,只需在您的应用程序配置中添加以下代码
'oauth2' => [
'class' => 'landrain\oauth2server\Module',
'options' => [
'token_param_name' => 'accessToken',
'access_lifetime' => 3600 * 24
],
'storageMap' => [
'user_credentials' => 'common\models\User'
],
'grantTypes' => [
'client_credentials' => [
'class' => 'OAuth2\GrantType\ClientCredentials',
'allow_public_clients' => false
],
'user_credentials' => [
'class' => 'OAuth2\GrantType\UserCredentials'
],
'refresh_token' => [
'class' => 'OAuth2\GrantType\RefreshToken',
'always_issue_new_refresh_token' => true
]
],
]
The next step your shold run migration
```php
yii migrate --migrationPath=@vendor/landrain/yii2-oauth2-server/migrations
```
this migration create the oauth2 database scheme and insert test user credentials ```testclient:testpass``` for ```http://fake/```
add url rule to urlManager
```php
'urlManager' => [
'rules' => [
'POST oauth2/<action:\w+>' => 'oauth2/default/<action>',
...
]
]
```
Usage
-----
To use this extension, simply add the behaviors for your base controller:
```php
use yii\helpers\ArrayHelper;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
use landrain\oauth2server\filters\ErrorToExceptionFilter;
use landrain\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()
],
]);
}
}
```
For more, see https://github.com/bshaffer/oauth2-server-php