#20 Quick implementation of the https://api.minecraftservices.com/minecraft/profile endpoint [deploy dev]

This commit is contained in:
ErickSkrauch 2022-01-21 21:09:46 +01:00
parent 5b8be60867
commit 31febd5606
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
4 changed files with 123 additions and 1 deletions

View File

@ -46,6 +46,7 @@ return [
'/mojang/profiles/<username>' => 'mojang/api/uuid-by-username', '/mojang/profiles/<username>' => 'mojang/api/uuid-by-username',
'/mojang/profiles/<uuid>/names' => 'mojang/api/usernames-by-uuid', '/mojang/profiles/<uuid>/names' => 'mojang/api/usernames-by-uuid',
'POST /mojang/profiles' => 'mojang/api/uuids-by-usernames', 'POST /mojang/profiles' => 'mojang/api/uuids-by-usernames',
'GET /mojang/services/minecraft/profile' => 'mojang/services/profile',
// authlib-injector // authlib-injector
'/authlib-injector/authserver/<action>' => 'authserver/authentication/<action>', '/authlib-injector/authserver/<action>' => 'authserver/authentication/<action>',

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace api\modules\mojang\controllers;
use api\controllers\Controller;
use api\rbac\Permissions;
use common\components\SkinsSystemApi;
use Exception;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use function Ramsey\Uuid\v3;
final class ServicesController extends Controller {
public function behaviors(): array {
return ArrayHelper::merge(parent::behaviors(), [
'access' => [
'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;
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace api\tests\functional\mojang;
use api\tests\FunctionalTester;
class ProfileCest {
public function getProfile(FunctionalTester $I): void {
$I->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
}

View File

@ -20,7 +20,7 @@ use const common\LATEST_RULES_VERSION;
/** /**
* Fields: * Fields:
* @property int $id * @property int $id
* @property string $uuid * @property string $uuid UUID with dashes
* @property string $username * @property string $username
* @property string $email * @property string $email
* @property string $password_hash * @property string $password_hash