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'],
]);
}

View File

@@ -1,54 +0,0 @@
<?php
declare(strict_types=1);
namespace api\tests\_pages;
/**
* @deprecated
* TODO: remove
*/
class OauthRoute extends BasePage {
/**
* @deprecated
*/
public function createClient(string $type, array $postParams): void {
$this->getActor()->sendPOST('/api/v1/oauth2/' . $type, $postParams);
}
/**
* @deprecated
*/
public function updateClient(string $clientId, array $params): void {
$this->getActor()->sendPUT('/api/v1/oauth2/' . $clientId, $params);
}
/**
* @deprecated
*/
public function deleteClient(string $clientId): void {
$this->getActor()->sendDELETE('/api/v1/oauth2/' . $clientId);
}
/**
* @deprecated
*/
public function resetClient(string $clientId, bool $regenerateSecret = false): void {
$this->getActor()->sendPOST("/api/v1/oauth2/{$clientId}/reset" . ($regenerateSecret ? '?regenerateSecret' : ''));
}
/**
* @deprecated
*/
public function getClient(string $clientId): void {
$this->getActor()->sendGET("/api/v1/oauth2/{$clientId}");
}
/**
* @deprecated
*/
public function getPerAccount(int $accountId): void {
$this->getActor()->sendGET("/api/v1/accounts/{$accountId}/oauth2/clients");
}
}

View File

@@ -3,20 +3,13 @@ declare(strict_types=1);
namespace api\tests\functional\dev\applications;
use api\tests\_pages\OauthRoute;
use api\tests\FunctionalTester;
final class CreateClientCest {
private OauthRoute $route;
public function _before(FunctionalTester $I): void {
$this->route = new OauthRoute($I);
}
public function testCreateApplication(FunctionalTester $I): void {
public function testCreateWebApplication(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$this->route->createClient('application', [
$I->sendPOST('/api/v1/oauth2/application', [
'name' => 'My admin application',
'description' => 'Application description.',
'redirectUri' => 'http://some-site.com/oauth/ely',
@@ -39,9 +32,33 @@ final class CreateClientCest {
$I->canSeeResponseJsonMatchesJsonPath('$.data.createdAt');
}
public function testCreateDesktopApplication(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$I->sendPOST('/api/v1/oauth2/desktop-application', [
'name' => 'Mega Launcher',
'description' => "Launcher's description.",
'websiteUrl' => 'http://mega-launcher.com',
]);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'success' => true,
'data' => [
'clientId' => 'mega-launcher',
'type' => 'desktop-application',
'name' => 'Mega Launcher',
'description' => "Launcher's description.",
'websiteUrl' => 'http://mega-launcher.com',
'countUsers' => 0,
],
]);
$I->cantSeeResponseJsonMatchesJsonPath('$.data.clientSecret');
$I->canSeeResponseJsonMatchesJsonPath('$.data.createdAt');
}
public function testCreateMinecraftServer(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$this->route->createClient('minecraft-server', [
$I->sendPOST('/api/v1/oauth2/minecraft-server', [
'name' => 'My amazing server',
'websiteUrl' => 'http://some-site.com',
'minecraftServerIp' => 'hypixel.com:25565',
@@ -64,7 +81,7 @@ final class CreateClientCest {
public function testCreateApplicationWithTheSameNameAsDeletedApp(FunctionalTester $I): void {
$I->wantTo('create application with the same name as the recently deleted application');
$I->amAuthenticated('admin');
$this->route->createClient('application', [
$I->sendPOST('/api/v1/oauth2/application', [
'name' => 'Deleted OAuth Client',
'description' => '',
'redirectUri' => 'http://some-site.com/oauth/ely',
@@ -82,8 +99,7 @@ final class CreateClientCest {
public function testCreateApplicationWithWrongParams(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$this->route->createClient('application', []);
$I->sendPOST('/api/v1/oauth2/application', []);
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseContainsJson([
'success' => false,

View File

@@ -3,20 +3,13 @@ declare(strict_types=1);
namespace api\tests\functional\dev\applications;
use api\tests\_pages\OauthRoute;
use api\tests\FunctionalTester;
class DeleteClientCest {
private OauthRoute $route;
public function _before(FunctionalTester $I): void {
$this->route = new OauthRoute($I);
}
final class DeleteClientCest {
public function testDelete(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->deleteClient('first-test-oauth-client');
$I->sendDELETE('/api/v1/oauth2/first-test-oauth-client');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([

View File

@@ -3,20 +3,13 @@ declare(strict_types=1);
namespace api\tests\functional\dev\applications;
use api\tests\_pages\OauthRoute;
use api\tests\FunctionalTester;
class GetClientsCest {
private OauthRoute $route;
public function _before(FunctionalTester $I): void {
$this->route = new OauthRoute($I);
}
final class GetClientsCest {
public function testGet(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$this->route->getClient('admin-oauth-client');
$I->sendGET('/api/v1/oauth2/admin-oauth-client');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
@@ -33,7 +26,7 @@ class GetClientsCest {
public function testGetNotOwn(FunctionalTester $I): void {
$I->amAuthenticated('admin');
$this->route->getClient('another-test-oauth-client');
$I->sendGET('/api/v1/oauth2/another-test-oauth-client');
$I->canSeeResponseCodeIs(403);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
@@ -44,8 +37,8 @@ class GetClientsCest {
}
public function testGetAllPerAccountList(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->getPerAccount(14);
$accountId = $I->amAuthenticated('TwoOauthClients');
$I->sendGET("/api/v1/accounts/{$accountId}/oauth2/clients");
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
@@ -74,7 +67,7 @@ class GetClientsCest {
public function testGetAllPerNotOwnAccount(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->getPerAccount(1);
$I->sendGET('/api/v1/accounts/1/oauth2/clients');
$I->canSeeResponseCodeIs(403);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([

View File

@@ -3,20 +3,13 @@ declare(strict_types=1);
namespace api\tests\functional\dev\applications;
use api\tests\_pages\OauthRoute;
use api\tests\FunctionalTester;
class ResetClientCest {
private OauthRoute $route;
public function _before(FunctionalTester $I): void {
$this->route = new OauthRoute($I);
}
final class ResetClientCest {
public function testReset(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->resetClient('first-test-oauth-client');
$I->sendPOST('/api/v1/oauth2/first-test-oauth-client/reset');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
@@ -36,7 +29,7 @@ class ResetClientCest {
public function testResetWithSecretChanging(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->resetClient('first-test-oauth-client', true);
$I->sendPOST('/api/v1/oauth2/first-test-oauth-client/reset?regenerateSecret');
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([

View File

@@ -3,20 +3,13 @@ declare(strict_types=1);
namespace api\tests\functional\dev\applications;
use api\tests\_pages\OauthRoute;
use api\tests\FunctionalTester;
class UpdateClientCest {
final class UpdateClientCest {
private OauthRoute $route;
public function _before(FunctionalTester $I): void {
$this->route = new OauthRoute($I);
}
public function testUpdateApplication(FunctionalTester $I): void {
public function testUpdateWebApplication(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->updateClient('first-test-oauth-client', [
$I->sendPUT('/api/v1/oauth2/first-test-oauth-client', [
'name' => 'Updated name',
'description' => 'Updated description.',
'redirectUri' => 'http://new-site.com/oauth/ely',
@@ -41,7 +34,7 @@ class UpdateClientCest {
public function testUpdateMinecraftServer(FunctionalTester $I): void {
$I->amAuthenticated('TwoOauthClients');
$this->route->updateClient('another-test-oauth-client', [
$I->sendPUT('/api/v1/oauth2/another-test-oauth-client', [
'name' => 'Updated server name',
'websiteUrl' => 'http://new-site.com',
'minecraftServerIp' => 'hypixel.com:25565',

View File

@@ -7,7 +7,7 @@ use api\modules\oauth\models\BaseOauthClientType;
use api\tests\unit\TestCase;
use common\models\OauthClient;
class BaseOauthClientTypeTest extends TestCase {
final class BaseOauthClientTypeTest extends TestCase {
public function testApplyTyClient(): void {
$client = new OauthClient();

View File

@@ -1,11 +1,13 @@
<?php
declare(strict_types=1);
namespace api\tests\unit\modules\oauth\models;
use api\modules\oauth\models\MinecraftServerType;
use api\tests\unit\TestCase;
use common\models\OauthClient;
class MinecraftServerTypeTest extends TestCase {
final class MinecraftServerTypeTest extends TestCase {
public function testApplyToClient(): void {
$model = new MinecraftServerType();

View File

@@ -4,29 +4,46 @@ declare(strict_types=1);
namespace api\tests\unit\modules\oauth\models;
use api\modules\oauth\exceptions\UnsupportedOauthClientType;
use api\modules\oauth\models\ApplicationType;
use api\modules\oauth\models\DesktopApplicationType;
use api\modules\oauth\models\MinecraftServerType;
use api\modules\oauth\models\OauthClientFormFactory;
use api\modules\oauth\models\WebApplicationType;
use api\tests\unit\TestCase;
use common\models\OauthClient;
class OauthClientFormFactoryTest extends TestCase {
final class OauthClientFormFactoryTest extends TestCase {
public function testCreate(): void {
public function testCreateWebApplication(): void {
$client = new OauthClient();
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$client->name = 'Application name';
$client->description = 'Application description.';
$client->website_url = 'http://example.com';
$client->redirect_uri = 'http://example.com/oauth/ely';
/** @var ApplicationType $requestForm */
/** @var WebApplicationType $requestForm */
$requestForm = OauthClientFormFactory::create($client);
$this->assertInstanceOf(ApplicationType::class, $requestForm);
$this->assertInstanceOf(WebApplicationType::class, $requestForm);
$this->assertSame('Application name', $requestForm->name);
$this->assertSame('Application description.', $requestForm->description);
$this->assertSame('http://example.com', $requestForm->websiteUrl);
$this->assertSame('http://example.com/oauth/ely', $requestForm->redirectUri);
}
public function testCreateDesktopApplication(): void {
$client = new OauthClient();
$client->type = OauthClient::TYPE_DESKTOP_APPLICATION;
$client->name = 'Application name';
$client->description = 'Application description.';
$client->website_url = 'http://example.com';
/** @var \api\modules\oauth\models\DesktopApplicationType $requestForm */
$requestForm = OauthClientFormFactory::create($client);
$this->assertInstanceOf(DesktopApplicationType::class, $requestForm);
$this->assertSame('Application name', $requestForm->name);
$this->assertSame('Application description.', $requestForm->description);
$this->assertSame('http://example.com', $requestForm->websiteUrl);
}
public function testCreateMinecraftServer(): void {
$client = new OauthClient();
$client->type = OauthClient::TYPE_MINECRAFT_SERVER;
$client->name = 'Server name';
@@ -44,7 +61,7 @@ class OauthClientFormFactoryTest extends TestCase {
$this->expectException(UnsupportedOauthClientType::class);
$client = new OauthClient();
$client->type = 'unknown-type';
$client->type = 'unknown-type'; // @phpstan-ignore assign.propertyType (its alright for tests)
OauthClientFormFactory::create($client);
}

View File

@@ -9,13 +9,13 @@ use api\tests\unit\TestCase;
use common\models\OauthClient;
use common\tasks\ClearOauthSessions;
class OauthClientFormTest extends TestCase {
final class OauthClientFormTest extends TestCase {
public function testSave(): void {
$client = $this->createPartialMock(OauthClient::class, ['save']);
$client->method('save')->willReturn(true);
$client->account_id = 1;
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$client->name = 'Test application';
$form = $this->createPartialMock(OauthClientForm::class, ['getClient', 'isClientExists']);
@@ -39,7 +39,7 @@ class OauthClientFormTest extends TestCase {
$client->id = 'application-id';
$client->secret = 'application_secret';
$client->account_id = 1;
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$client->name = 'Application name';
$client->description = 'Application description';
$client->redirect_uri = 'http://example.com/oauth/ely';
@@ -81,7 +81,7 @@ class OauthClientFormTest extends TestCase {
$client = $this->createPartialMock(OauthClient::class, ['save']);
$client->method('save')->willReturn(true);
$client->id = 'mocked-id';
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$form = new OauthClientForm($client);
$this->assertTrue($form->delete());
@@ -98,7 +98,7 @@ class OauthClientFormTest extends TestCase {
$client->method('save')->willReturn(true);
$client->id = 'mocked-id';
$client->secret = 'initial_secret';
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$form = new OauthClientForm($client);
$this->assertTrue($form->reset());
@@ -115,7 +115,7 @@ class OauthClientFormTest extends TestCase {
$client->method('save')->willReturn(true);
$client->id = 'mocked-id';
$client->secret = 'initial_secret';
$client->type = OauthClient::TYPE_APPLICATION;
$client->type = OauthClient::TYPE_WEB_APPLICATION;
$form = new OauthClientForm($client);
$this->assertTrue($form->reset(true));

View File

@@ -1,14 +1,16 @@
<?php
declare(strict_types=1);
namespace api\tests\unit\modules\oauth\models;
use api\modules\oauth\models\ApplicationType;
use api\modules\oauth\models\WebApplicationType;
use api\tests\unit\TestCase;
use common\models\OauthClient;
class ApplicationTypeTest extends TestCase {
final class WebApplicationTypeTest extends TestCase {
public function testApplyToClient(): void {
$model = new ApplicationType();
$model = new WebApplicationType();
$model->name = 'Application name';
$model->websiteUrl = 'http://example.com';
$model->redirectUri = 'http://example.com/oauth/ely';