yii2-oauth2-server
A wrapper for implementing an OAuth2 Server(https://github.com/bshaffer/oauth2-server-php), (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist filsh/yii2-oauth2-server "*"
or add, (*4)
"filsh/yii2-oauth2-server": "~2.0"
to the require section of your composer.json., (*5)
To use this extension, simply add the following code in your application configuration:, (*6)
'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
- user model implementing an interface \OAuth2\Storage\UserCredentialsInterface
, so the oauth2 credentials data stored in user table, (*7)
The next step your shold run migration, (*8)
yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations
this migration create the oauth2 database scheme and insert test user credentials testclient:testpass
for http://fake/
, (*9)
add url rule to urlManager, (*10)
'urlManager' => [
'rules' => [
'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
...
]
]
Usage
To use this extension, simply add the behaviors for your base controller:, (*11)
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()
],
]);
}
}
Create action authorize in site controller for Authorization Code, (*12)
https://api.mysite.com/authorize?response_type=code&client_id=TestClient&redirect_uri=https://fake/
, (*13)
see more, (*14)
/**
* 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();
}
}
For more, see https://github.com/bshaffer/oauth2-server-php, (*15)