Resolves #2. Implemented authlib-injector support

This commit is contained in:
ErickSkrauch
2021-03-03 15:04:42 +01:00
parent 10ab237d1d
commit 4856695940
17 changed files with 505 additions and 87 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace common\components;
use GuzzleHttp\ClientInterface;
use Webmozart\Assert\Assert;
use Yii;
// TODO: convert to complete Chrly client library
@@ -11,8 +12,7 @@ class SkinsSystemApi {
private const BASE_DOMAIN = 'http://skinsystem.ely.by';
/** @var ClientInterface */
private $client;
private ?ClientInterface $client = null;
/**
* @param string $username
@@ -29,12 +29,47 @@ class SkinsSystemApi {
return json_decode($response->getBody()->getContents(), true);
}
/**
* @param string $username
*
* @return array|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function profile(string $username, bool $signed = false): ?array {
$url = "/profile/{$username}";
if ($signed) {
$url .= '?unsigned=false';
}
$response = $this->getClient()->request('GET', $this->buildUrl($url));
if ($response->getStatusCode() !== 200) {
return null;
}
return json_decode($response->getBody()->getContents(), true);
}
/**
* @param 'pem'|'der' $format
*
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getSignatureVerificationKey(string $format = 'pem'): string {
Assert::inArray($format, ['pem', 'der']);
return $this->getClient()
->request('GET', $this->buildUrl("/signature-verification-key.{$format}"))
->getBody()
->getContents();
}
public function setClient(ClientInterface $client): void {
$this->client = $client;
}
private function buildUrl(string $url): string {
return self::BASE_DOMAIN . $url;
return static::BASE_DOMAIN . $url;
}
private function getClient(): ClientInterface {

View File

@@ -16,7 +16,7 @@ return [
'supportEmail' => 'support@ely.by',
],
'container' => [
'definitions' => [
'singletons' => [
GuzzleHttp\ClientInterface::class => GuzzleHttp\Client::class,
Ely\Mojang\Api::class => Ely\Mojang\Api::class,
common\components\SkinsSystemApi::class => common\components\SkinsSystemApi::class,

View File

@@ -3,95 +3,79 @@ declare(strict_types=1);
namespace common\models;
use common\components\SkinsSystemApi as SkinSystemApi;
use DateInterval;
use DateTime;
use Carbon\Carbon;
use common\components\SkinsSystemApi;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use Yii;
class Textures {
public $displayElyMark = true;
/**
* @var Account
*/
protected $account;
protected Account $account;
public function __construct(Account $account) {
$this->account = $account;
}
public function getMinecraftResponse(): array {
$response = [
'name' => $this->account->username,
'id' => str_replace('-', '', $this->account->uuid),
'properties' => [
[
'name' => 'textures',
'signature' => 'Cg==',
'value' => $this->getTexturesValue(),
public function getMinecraftResponse(bool $signed = false): array {
$uuid = str_replace('-', '', $this->account->uuid);
$profile = $this->getProfile($signed);
if ($profile === null) {
// This case shouldn't happen at all, but until we find out how it'll actually behave,
// provide for a fallback solution
Yii::warning("By some reasons there is no profile for \"{$this->account->username}\".");
$profile = [
'name' => $this->account->username,
'id' => $uuid,
'properties' => [
[
'name' => 'textures',
'value' => base64_encode(json_encode([
'timestamp' => Carbon::now()->getPreciseTimestamp(3),
'profileId' => $uuid,
'profileName' => $this->account->username,
'textures' => [],
])),
],
[
'name' => 'ely',
'value' => 'but why are you asking?',
],
],
],
];
];
if ($this->displayElyMark) {
$response['ely'] = true;
if ($signed) {
// I don't remember why this value has been used, but it was, so keep the same behavior until
// figure out why it was made in this way
$profile['properties'][0]['signature'] = 'Cg==';
}
} elseif ($profile['id'] !== $uuid) {
// Also a case that shouldn't happen, but is technically possible
Yii::warning("By an unknown reason username \"{$this->account->username}\" has an invalid id from chrly");
$profile['id'] = $uuid;
}
return $response;
return $profile;
}
public function getTexturesValue($encrypted = true) {
$array = [
'timestamp' => (new DateTime())->add(new DateInterval('P2D'))->getTimestamp(),
'profileId' => str_replace('-', '', $this->account->uuid),
'profileName' => $this->account->username,
'textures' => $this->getTextures(),
];
if ($this->displayElyMark) {
$array['ely'] = true;
}
if (!$encrypted) {
return $array;
}
return static::encrypt($array);
}
public function getTextures(): array {
/** @var SkinSystemApi $api */
$api = Yii::$container->get(SkinSystemApi::class);
private function getProfile(bool $signed): ?array {
/** @var SkinsSystemApi $api */
$api = Yii::$container->get(SkinsSystemApi::class);
if (YII_ENV_PROD) {
$api->setClient(new \GuzzleHttp\Client([
$api->setClient(new GuzzleHttpClient([
'connect_timeout' => 2,
'decode_content' => false,
'read_timeout' => 5,
'stream' => true,
'timeout' => 5,
'read_timeout' => 7,
]));
}
try {
$textures = $api->textures($this->account->username);
} catch (RequestException $e) {
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
return $api->profile($this->account->username, $signed);
} catch (GuzzleException $e) {
Yii::warning($e);
Yii::warning('Cannot get textures from skinsystem.ely.by. Exception message is ' . $e->getMessage());
}
return $textures ?? [];
}
public static function encrypt(array $data): string {
return base64_encode(stripcslashes(json_encode($data)));
}
public static function decrypt($string, $assoc = true) {
return json_decode(base64_decode($string), $assoc);
return null;
}
}