2016-02-14 23:20:10 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
namespace api\modules\oauth\controllers;
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
use api\controllers\Controller;
|
|
|
|
use api\modules\oauth\models\OauthProcess;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2019-12-09 22:01:54 +05:30
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2016-02-14 23:20:10 +05:30
|
|
|
use Yii;
|
2024-12-06 06:04:09 +05:30
|
|
|
use yii\base\Module;
|
2016-02-14 23:20:10 +05:30
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
2024-12-06 06:04:09 +05:30
|
|
|
final class AuthorizationController extends Controller {
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $id,
|
|
|
|
Module $module,
|
|
|
|
private readonly OauthProcess $oauthProcess,
|
|
|
|
array $config = [],
|
|
|
|
) {
|
|
|
|
parent::__construct($id, $module, $config);
|
|
|
|
}
|
2016-02-14 23:20:10 +05:30
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function behaviors(): array {
|
2018-02-28 03:57:35 +05:30
|
|
|
return ArrayHelper::merge(Controller::behaviors(), [
|
2016-02-24 03:45:04 +05:30
|
|
|
'authenticator' => [
|
2016-12-12 02:37:39 +05:30
|
|
|
'only' => ['complete'],
|
2016-02-24 03:45:04 +05:30
|
|
|
],
|
2016-02-14 23:20:10 +05:30
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
2016-12-12 02:37:39 +05:30
|
|
|
'only' => ['complete'],
|
2016-02-14 23:20:10 +05:30
|
|
|
'rules' => [
|
|
|
|
[
|
2017-09-19 22:36:16 +05:30
|
|
|
'allow' => true,
|
2016-02-14 23:20:10 +05:30
|
|
|
'actions' => ['complete'],
|
2017-09-19 22:36:16 +05:30
|
|
|
'roles' => [P::COMPLETE_OAUTH_FLOW],
|
2024-12-02 15:40:55 +05:30
|
|
|
'roleParams' => fn(): array => [
|
|
|
|
'accountId' => Yii::$app->user->identity->getAccount()->id,
|
|
|
|
],
|
2016-02-14 23:20:10 +05:30
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function verbs(): array {
|
2016-02-14 23:20:10 +05:30
|
|
|
return [
|
2016-07-27 03:41:13 +05:30
|
|
|
'validate' => ['GET'],
|
|
|
|
'complete' => ['POST'],
|
2024-12-08 21:24:45 +05:30
|
|
|
'device' => ['POST'],
|
2018-04-18 02:17:25 +05:30
|
|
|
'token' => ['POST'],
|
2016-02-14 23:20:10 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function actionValidate(): array {
|
2024-12-06 06:04:09 +05:30
|
|
|
return $this->oauthProcess->validate($this->getServerRequest());
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function actionComplete(): array {
|
2024-12-06 06:04:09 +05:30
|
|
|
return $this->oauthProcess->complete($this->getServerRequest());
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|
|
|
|
|
2024-12-08 21:24:45 +05:30
|
|
|
public function actionDevice(): array {
|
|
|
|
return $this->oauthProcess->deviceCode($this->getServerRequest());
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
public function actionToken(): array {
|
2024-12-06 06:04:09 +05:30
|
|
|
return $this->oauthProcess->getToken($this->getServerRequest());
|
2016-08-18 03:27:01 +05:30
|
|
|
}
|
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
private function getServerRequest(): ServerRequestInterface {
|
2022-12-10 08:26:19 +05:30
|
|
|
$request = Yii::$app->request;
|
|
|
|
|
|
|
|
return (new ServerRequest(
|
|
|
|
$request->getMethod(),
|
|
|
|
$request->getAbsoluteUrl() . '?' . $request->getQueryString(),
|
|
|
|
$request->getHeaders()->toArray(),
|
|
|
|
))
|
|
|
|
->withParsedBody($request->getBodyParams())
|
|
|
|
->withQueryParams($request->getQueryParams());
|
2019-12-09 22:01:54 +05:30
|
|
|
}
|
|
|
|
|
2016-02-14 23:20:10 +05:30
|
|
|
}
|