2018-02-28 03:57:35 +05:30
|
|
|
<?php
|
2020-09-30 23:00:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
namespace api\modules\oauth\controllers;
|
|
|
|
|
|
|
|
use api\controllers\Controller;
|
|
|
|
use api\modules\oauth\exceptions\UnsupportedOauthClientType;
|
|
|
|
use api\modules\oauth\models\OauthClientForm;
|
|
|
|
use api\modules\oauth\models\OauthClientFormFactory;
|
|
|
|
use api\modules\oauth\models\OauthClientTypeForm;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2018-02-28 03:57:35 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\OauthClient;
|
2019-12-13 23:18:13 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2018-02-28 03:57:35 +05:30
|
|
|
use Yii;
|
2020-09-30 23:00:04 +05:30
|
|
|
use yii\db\ActiveQuery;
|
|
|
|
use yii\db\Expression;
|
2018-02-28 03:57:35 +05:30
|
|
|
use yii\filters\AccessControl;
|
2020-09-30 23:00:04 +05:30
|
|
|
use yii\filters\VerbFilter;
|
2018-02-28 03:57:35 +05:30
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
|
|
|
class ClientsController extends Controller {
|
|
|
|
|
|
|
|
public function behaviors(): array {
|
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
|
|
|
'actions' => ['create'],
|
|
|
|
'allow' => true,
|
|
|
|
'permissions' => [P::CREATE_OAUTH_CLIENTS],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'actions' => ['update', 'delete', 'reset'],
|
|
|
|
'allow' => true,
|
|
|
|
'permissions' => [P::MANAGE_OAUTH_CLIENTS],
|
2020-09-30 23:00:04 +05:30
|
|
|
'roleParams' => fn() => [
|
|
|
|
'clientId' => Yii::$app->request->get('clientId'),
|
|
|
|
],
|
2018-02-28 03:57:35 +05:30
|
|
|
],
|
|
|
|
[
|
|
|
|
'actions' => ['get'],
|
|
|
|
'allow' => true,
|
|
|
|
'permissions' => [P::VIEW_OAUTH_CLIENTS],
|
2020-09-30 23:00:04 +05:30
|
|
|
'roleParams' => fn() => [
|
|
|
|
'clientId' => Yii::$app->request->get('clientId'),
|
|
|
|
],
|
2018-02-28 03:57:35 +05:30
|
|
|
],
|
|
|
|
[
|
|
|
|
'actions' => ['get-per-account'],
|
|
|
|
'allow' => true,
|
|
|
|
'permissions' => [P::VIEW_OAUTH_CLIENTS],
|
2020-09-30 23:00:04 +05:30
|
|
|
'roleParams' => fn() => [
|
|
|
|
'accountId' => Yii::$app->request->get('accountId'),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'actions' => ['get-authorized-clients', 'revoke-client'],
|
|
|
|
'allow' => true,
|
|
|
|
'permissions' => [P::MANAGE_OAUTH_SESSIONS],
|
|
|
|
'roleParams' => fn() => [
|
|
|
|
'accountId' => Yii::$app->request->get('accountId'),
|
|
|
|
],
|
2018-02-28 03:57:35 +05:30
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2020-09-30 23:00:04 +05:30
|
|
|
'verb' => [
|
|
|
|
'class' => VerbFilter::class,
|
|
|
|
'actions' => [
|
|
|
|
'get' => ['GET'],
|
|
|
|
'create' => ['POST'],
|
|
|
|
'update' => ['PUT'],
|
|
|
|
'delete' => ['DELETE'],
|
|
|
|
'reset' => ['POST'],
|
|
|
|
'get-per-account' => ['GET'],
|
|
|
|
'get-authorized-clients' => ['GET'],
|
|
|
|
'revoke-client' => ['DELETE'],
|
|
|
|
],
|
|
|
|
],
|
2018-02-28 03:57:35 +05:30
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionGet(string $clientId): array {
|
|
|
|
return $this->formatClient($this->findOauthClient($clientId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionCreate(string $type): array {
|
|
|
|
$account = Yii::$app->user->identity->getAccount();
|
2019-12-13 23:18:13 +05:30
|
|
|
Assert::notNull($account === null, 'This form should not to be executed without associated account');
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
$client = new OauthClient();
|
|
|
|
$client->account_id = $account->id;
|
|
|
|
$client->type = $type;
|
|
|
|
$requestModel = $this->createForm($client);
|
|
|
|
$requestModel->load(Yii::$app->request->post());
|
|
|
|
$form = new OauthClientForm($client);
|
|
|
|
if (!$form->save($requestModel)) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $requestModel->getValidationErrors(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
'data' => $this->formatClient($client),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionUpdate(string $clientId): array {
|
|
|
|
$client = $this->findOauthClient($clientId);
|
|
|
|
$requestModel = $this->createForm($client);
|
|
|
|
$requestModel->load(Yii::$app->request->post());
|
|
|
|
$form = new OauthClientForm($client);
|
|
|
|
if (!$form->save($requestModel)) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $requestModel->getValidationErrors(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
'data' => $this->formatClient($client),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionDelete(string $clientId): array {
|
|
|
|
$client = $this->findOauthClient($clientId);
|
|
|
|
(new OauthClientForm($client))->delete();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionReset(string $clientId, string $regenerateSecret = null): array {
|
|
|
|
$client = $this->findOauthClient($clientId);
|
|
|
|
$form = new OauthClientForm($client);
|
|
|
|
$form->reset($regenerateSecret !== null);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
'data' => $this->formatClient($client),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionGetPerAccount(int $accountId): array {
|
2018-03-26 00:51:22 +05:30
|
|
|
/** @var OauthClient[] $clients */
|
2020-09-30 23:00:04 +05:30
|
|
|
$clients = $this->findAccount($accountId)->getOauthClients()->orderBy(['created_at' => SORT_ASC])->all();
|
2018-02-28 03:57:35 +05:30
|
|
|
|
2020-06-10 20:12:02 +05:30
|
|
|
return array_map(fn(OauthClient $client): array => $this->formatClient($client), $clients);
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
public function actionGetAuthorizedClients(int $accountId): array {
|
|
|
|
$account = $this->findAccount($accountId);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
/** @var \common\models\OauthSession[] $oauthSessions */
|
|
|
|
$oauthSessions = $account->getOauthSessions()
|
|
|
|
->innerJoinWith(['client c' => function(ActiveQuery $query): void {
|
|
|
|
$query->andOnCondition(['c.type' => OauthClient::TYPE_APPLICATION]);
|
|
|
|
}])
|
|
|
|
->andWhere([
|
|
|
|
'OR',
|
|
|
|
['revoked_at' => null],
|
|
|
|
['>', 'last_used_at', new Expression('`revoked_at`')],
|
|
|
|
])
|
|
|
|
->all();
|
|
|
|
foreach ($oauthSessions as $oauthSession) {
|
|
|
|
$client = $oauthSession->client;
|
|
|
|
if ($client === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result[] = [
|
|
|
|
'id' => $client->id,
|
|
|
|
'name' => $client->name,
|
|
|
|
'description' => $client->description,
|
|
|
|
'scopes' => $oauthSession->getScopes(),
|
|
|
|
'authorizedAt' => $oauthSession->created_at,
|
|
|
|
'lastUsedAt' => $oauthSession->last_used_at,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionRevokeClient(int $accountId, string $clientId): ?array {
|
|
|
|
$account = $this->findAccount($accountId);
|
|
|
|
$client = $this->findOauthClient($clientId);
|
|
|
|
|
|
|
|
/** @var \common\models\OauthSession|null $session */
|
|
|
|
$session = $account->getOauthSessions()->andWhere(['client_id' => $client->id])->one();
|
|
|
|
if ($session !== null && !$session->isRevoked()) {
|
|
|
|
$session->revoked_at = time();
|
|
|
|
Assert::true($session->save());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['success' => true];
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
private function formatClient(OauthClient $client): array {
|
|
|
|
$result = [
|
|
|
|
'clientId' => $client->id,
|
|
|
|
'clientSecret' => $client->secret,
|
|
|
|
'type' => $client->type,
|
|
|
|
'name' => $client->name,
|
|
|
|
'websiteUrl' => $client->website_url,
|
|
|
|
'createdAt' => $client->created_at,
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($client->type) {
|
|
|
|
case OauthClient::TYPE_APPLICATION:
|
|
|
|
$result['description'] = $client->description;
|
|
|
|
$result['redirectUri'] = $client->redirect_uri;
|
2018-03-26 00:51:22 +05:30
|
|
|
$result['countUsers'] = (int)$client->getSessions()->count();
|
2018-02-28 03:57:35 +05:30
|
|
|
break;
|
|
|
|
case OauthClient::TYPE_MINECRAFT_SERVER:
|
|
|
|
$result['minecraftServerIp'] = $client->minecraft_server_ip;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createForm(OauthClient $client): OauthClientTypeForm {
|
|
|
|
try {
|
|
|
|
$model = OauthClientFormFactory::create($client);
|
|
|
|
} catch (UnsupportedOauthClientType $e) {
|
2020-09-30 23:00:04 +05:30
|
|
|
Yii::warning('Someone tried to use ' . $client->type . ' type of oauth form.');
|
2018-02-28 03:57:35 +05:30
|
|
|
throw new NotFoundHttpException(null, 0, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
private function findAccount(int $id): Account {
|
|
|
|
/** @var Account|null $account */
|
|
|
|
$account = Account::findOne(['id' => $id]);
|
|
|
|
if ($account === null) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
private function findOauthClient(string $clientId): OauthClient {
|
|
|
|
/** @var OauthClient|null $client */
|
2020-09-30 23:00:04 +05:30
|
|
|
$client = OauthClient::findOne(['id' => $clientId]);
|
2018-02-28 03:57:35 +05:30
|
|
|
if ($client === null) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|