mobilejazz / yii2-oauth2-server
OAuth2 Server for PHP
Installs: 10 951
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 11
Forks: 172
Type:yii2-extension
Requires
This package is not auto-updated.
Last update: 2023-09-06 23:20:55 UTC
README
A wrapper for implementing an OAuth2 Server(https://github.com/bshaffer/oauth2-server-php)
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist mobilejazz/yii2-oauth2-server "*"
or add
"mobilejazz/yii2-oauth2-server": "~2.1"
to the require section of your composer.json.
To use this extension, simply add the following code in your application configuration:
'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
- user model implementing an interface \OAuth2\Storage\UserCredentialsInterface
, so the oauth2 credentials data stored in user table
The next step your shold run migration
yii migrate --migrationPath=@vendor/mobilejazz/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
'urlManager' => [ 'rules' => [ 'POST oauth2/<action:\w+>' => 'oauth2/default/<action>', ... ] ]
Usage
To use this extension, simply add the behaviors for your base controller:
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() ], ]); } }
For more, see https://github.com/bshaffer/oauth2-server-php