Implemented desktop application type

This commit is contained in:
ErickSkrauch
2025-01-15 14:13:08 +01:00
parent 3bba99a757
commit 1c2969a4be
22 changed files with 183 additions and 214 deletions

View File

@@ -94,7 +94,7 @@ class ClientsController extends Controller {
$client = new OauthClient();
$client->account_id = $account->id;
$client->type = $type;
$client->type = $type; // @phpstan-ignore assign.propertyType (this value will be validated in the createForm())
$requestModel = $this->createForm($client);
$requestModel->load(Yii::$app->request->post());
$form = new OauthClientForm($client);
@@ -163,7 +163,7 @@ class ClientsController extends Controller {
/** @var \common\models\OauthSession[] $oauthSessions */
$oauthSessions = $account->getOauthSessions()
->innerJoinWith(['client c' => function(ActiveQuery $query): void {
$query->andOnCondition(['c.type' => OauthClient::TYPE_APPLICATION]);
$query->andOnCondition(['c.type' => OauthClient::TYPE_WEB_APPLICATION]);
}])
->andWhere([
'OR',
@@ -206,10 +206,12 @@ class ClientsController extends Controller {
return ['success' => true];
}
/**
* @return array<string, mixed>
*/
private function formatClient(OauthClient $client): array {
$result = [
'clientId' => $client->id,
'clientSecret' => $client->secret,
'type' => $client->type,
'name' => $client->name,
'websiteUrl' => $client->website_url,
@@ -217,12 +219,18 @@ class ClientsController extends Controller {
];
switch ($client->type) {
case OauthClient::TYPE_APPLICATION:
case OauthClient::TYPE_WEB_APPLICATION:
$result['clientSecret'] = $client->secret;
$result['description'] = $client->description;
$result['redirectUri'] = $client->redirect_uri;
$result['countUsers'] = (int)$client->getSessions()->count();
break;
case OauthClient::TYPE_DESKTOP_APPLICATION:
$result['description'] = $client->description;
$result['countUsers'] = (int)$client->getSessions()->count();
break;
case OauthClient::TYPE_MINECRAFT_SERVER:
$result['clientSecret'] = $client->secret;
$result['minecraftServerIp'] = $client->minecraft_server_ip;
break;
}

View File

@@ -9,9 +9,9 @@ use common\models\OauthClient;
abstract class BaseOauthClientType extends ApiForm implements OauthClientTypeForm {
public $name;
public mixed $name = null;
public $websiteUrl;
public mixed $websiteUrl = null;
public function rules(): array {
return [

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace api\modules\oauth\models;
use common\models\OauthClient;
use yii\helpers\ArrayHelper;
final class DesktopApplicationType extends BaseOauthClientType {
public mixed $description = null;
public function rules(): array {
return ArrayHelper::merge(parent::rules(), [
['description', 'string'],
]);
}
public function applyToClient(OauthClient $client): void {
parent::applyToClient($client);
$client->description = $this->description;
}
}

View File

@@ -6,27 +6,30 @@ namespace api\modules\oauth\models;
use api\modules\oauth\exceptions\UnsupportedOauthClientType;
use common\models\OauthClient;
class OauthClientFormFactory {
final class OauthClientFormFactory {
/**
* @param OauthClient $client
*
* @return OauthClientTypeForm
* @throws UnsupportedOauthClientType
*/
public static function create(OauthClient $client): OauthClientTypeForm {
return match ($client->type) {
OauthClient::TYPE_APPLICATION => new ApplicationType([
OauthClient::TYPE_WEB_APPLICATION => new WebApplicationType([
'name' => $client->name,
'websiteUrl' => $client->website_url,
'description' => $client->description,
'redirectUri' => $client->redirect_uri,
]),
OauthClient::TYPE_DESKTOP_APPLICATION => new DesktopApplicationType([
'name' => $client->name,
'description' => $client->description,
'websiteUrl' => $client->website_url,
]),
OauthClient::TYPE_MINECRAFT_SERVER => new MinecraftServerType([
'name' => $client->name,
'websiteUrl' => $client->website_url,
'minecraftServerIp' => $client->minecraft_server_ip,
]),
// @phpstan-ignore match.unreachable (Not quite correct code, but the value comes from the user and might be not expected)
default => throw new UnsupportedOauthClientType($client->type),
};
}

View File

@@ -3,21 +3,20 @@ declare(strict_types=1);
namespace api\modules\oauth\models;
use Closure;
use common\helpers\Error as E;
use common\models\OauthClient;
use yii\helpers\ArrayHelper;
final class ApplicationType extends BaseOauthClientType {
final class WebApplicationType extends BaseOauthClientType {
public $description;
public mixed $description = null;
public $redirectUri;
public mixed $redirectUri = null;
public function rules(): array {
return ArrayHelper::merge(parent::rules(), [
['redirectUri', 'required', 'message' => E::REDIRECT_URI_REQUIRED],
['redirectUri', Closure::fromCallable([$this, 'validateUrl'])],
['redirectUri', $this->validateUrl(...)],
['description', 'string'],
]);
}