From 31febd5606943de575a6d7648c735e3a325f5313 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 21 Jan 2022 21:09:46 +0100 Subject: [PATCH] #20 Quick implementation of the https://api.minecraftservices.com/minecraft/profile endpoint [deploy dev] --- api/config/routes.php | 1 + .../mojang/controllers/ServicesController.php | 87 +++++++++++++++++++ api/tests/functional/mojang/ProfileCest.php | 34 ++++++++ common/models/Account.php | 2 +- 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 api/modules/mojang/controllers/ServicesController.php create mode 100644 api/tests/functional/mojang/ProfileCest.php diff --git a/api/config/routes.php b/api/config/routes.php index ffb4222..9eccb63 100644 --- a/api/config/routes.php +++ b/api/config/routes.php @@ -46,6 +46,7 @@ return [ '/mojang/profiles/' => 'mojang/api/uuid-by-username', '/mojang/profiles//names' => 'mojang/api/usernames-by-uuid', 'POST /mojang/profiles' => 'mojang/api/uuids-by-usernames', + 'GET /mojang/services/minecraft/profile' => 'mojang/services/profile', // authlib-injector '/authlib-injector/authserver/' => 'authserver/authentication/', diff --git a/api/modules/mojang/controllers/ServicesController.php b/api/modules/mojang/controllers/ServicesController.php new file mode 100644 index 0000000..71654e2 --- /dev/null +++ b/api/modules/mojang/controllers/ServicesController.php @@ -0,0 +1,87 @@ + [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'allow' => true, + 'actions' => ['profile'], + 'roles' => [Permissions::OBTAIN_ACCOUNT_INFO], + 'roleParams' => function(): array { + $account = Yii::$app->user->identity->getAccount(); + + return [ + 'accountId' => $account ? $account->id : -1, + ]; + }, + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'profile' => ['GET'], + ], + ], + ]); + } + + public function actionProfile(SkinsSystemApi $skinsSystemApi): array { + /** @var \common\models\Account $account at this point null value isn't possible */ + $account = Yii::$app->user->identity->getAccount(); + + try { + $textures = $skinsSystemApi->textures($account->username); + } catch (Exception $e) { + Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage()); + $textures = []; + } + + $response = [ + 'id' => str_replace('-', '', $account->uuid), + 'name' => $account->username, + 'skins' => [], + 'capes' => [], + ]; + + if (isset($textures['SKIN'])) { + $response['skins'][] = [ + 'id' => v3(Uuid::NAMESPACE_URL, $textures['SKIN']['url']), + 'state' => 'ACTIVE', + 'url' => $textures['SKIN']['url'], + 'variant' => isset($textures['SKIN']['metadata']['model']) ? 'SLIM' : 'CLASSIC', + 'alias' => '', + ]; + } + + if (isset($textures['CAPE'])) { + $response['capes'][] = [ + 'id' => v3(Uuid::NAMESPACE_URL, $textures['CAPE']['url']), + 'state' => 'ACTIVE', + 'url' => $textures['CAPE']['url'], + 'alias' => '', + ]; + } + + return $response; + } + +} diff --git a/api/tests/functional/mojang/ProfileCest.php b/api/tests/functional/mojang/ProfileCest.php new file mode 100644 index 0000000..6925a4e --- /dev/null +++ b/api/tests/functional/mojang/ProfileCest.php @@ -0,0 +1,34 @@ +amAuthenticated(); + $I->sendGet('/api/mojang/services/minecraft/profile'); + $I->canSeeResponseCodeIs(200); + $I->canSeeResponseIsJson(); + $I->canSeeResponseContainsJson([ + 'id' => 'df936908b2e1544d96f82977ec213022', + 'name' => 'Admin', + 'skins' => [ + [ + 'id' => '1794a784-2d87-32f0-b233-0b2fd5682444', + 'state' => 'ACTIVE', + 'url' => 'http://localhost/skin.png', + 'variant' => 'CLASSIC', + 'alias' => '', + ], + ], + 'capes' => [], + ]); + } + + // TODO: add cases for unauthenticated user + // TODO: add cases for authenticated as a service account user + +} diff --git a/common/models/Account.php b/common/models/Account.php index 31ab1e6..74493c3 100644 --- a/common/models/Account.php +++ b/common/models/Account.php @@ -20,7 +20,7 @@ use const common\LATEST_RULES_VERSION; /** * Fields: * @property int $id - * @property string $uuid + * @property string $uuid UUID with dashes * @property string $username * @property string $email * @property string $password_hash