2016-02-14 20:50:10 +03:00
|
|
|
<?php
|
2018-02-28 01:27:35 +03:00
|
|
|
namespace api\modules\oauth\controllers;
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2018-02-28 01:27:35 +03:00
|
|
|
use api\controllers\Controller;
|
|
|
|
use api\modules\oauth\models\OauthProcess;
|
2017-09-19 20:06:16 +03:00
|
|
|
use common\rbac\Permissions as P;
|
2016-02-14 20:50:10 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
2018-02-28 01:27:35 +03:00
|
|
|
class AuthorizationController extends Controller {
|
2016-02-14 20:50:10 +03:00
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
public function behaviors(): array {
|
2018-02-28 01:27:35 +03:00
|
|
|
return ArrayHelper::merge(Controller::behaviors(), [
|
2016-02-24 01:15:04 +03:00
|
|
|
'authenticator' => [
|
2016-12-12 00:07:39 +03:00
|
|
|
'only' => ['complete'],
|
2016-02-24 01:15:04 +03:00
|
|
|
],
|
2016-02-14 20:50:10 +03:00
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
2016-12-12 00:07:39 +03:00
|
|
|
'only' => ['complete'],
|
2016-02-14 20:50:10 +03:00
|
|
|
'rules' => [
|
|
|
|
[
|
2017-09-19 20:06:16 +03:00
|
|
|
'allow' => true,
|
2016-02-14 20:50:10 +03:00
|
|
|
'actions' => ['complete'],
|
2017-09-19 20:06:16 +03:00
|
|
|
'roles' => [P::COMPLETE_OAUTH_FLOW],
|
|
|
|
'roleParams' => function() {
|
|
|
|
return [
|
|
|
|
'accountId' => Yii::$app->user->identity->getAccount()->id,
|
|
|
|
];
|
|
|
|
},
|
2016-02-14 20:50:10 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
public function verbs(): array {
|
2016-02-14 20:50:10 +03:00
|
|
|
return [
|
2016-07-27 01:11:13 +03:00
|
|
|
'validate' => ['GET'],
|
|
|
|
'complete' => ['POST'],
|
2018-04-17 23:47:25 +03:00
|
|
|
'token' => ['POST'],
|
2016-02-14 20:50:10 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
public function actionValidate(): array {
|
|
|
|
return $this->createOauthProcess()->validate();
|
2016-02-23 00:49:46 +03:00
|
|
|
}
|
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
public function actionComplete(): array {
|
|
|
|
return $this->createOauthProcess()->complete();
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
public function actionToken(): array {
|
|
|
|
return $this->createOauthProcess()->getToken();
|
2016-08-18 00:57:01 +03:00
|
|
|
}
|
|
|
|
|
2017-06-12 14:36:20 +03:00
|
|
|
private function createOauthProcess(): OauthProcess {
|
2019-02-20 22:58:52 +03:00
|
|
|
$server = Yii::$app->oauth->authServer;
|
|
|
|
$server->setRequest(null); // Enforce request recreation (test environment bug)
|
|
|
|
|
|
|
|
return new OauthProcess($server);
|
2016-08-18 00:57:01 +03:00
|
|
|
}
|
|
|
|
|
2016-02-14 20:50:10 +03:00
|
|
|
}
|