Upgrade project to PHP 8.3, add PHPStan, upgrade almost every dependency (#36)

* start updating to PHP 8.3

* taking off!

Co-authored-by: ErickSkrauch <erickskrauch@yandex.ru>
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* dropped this

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* migrate to symfonymailer

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* this is so stupid 😭

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* ah, free, at last.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* oh, Gabriel.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* now dawns thy reckoning.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* and thy gore shall GLISTEN before the temples of man.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* creature of steel.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* my gratitude upon thee for my freedom.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* but the crimes thy kind has committed against humanity

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* Upgrade PHP-CS-Fixer and do fix the codebase

* First review round (maybe I have broken something)

* are NOT forgotten.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* Enable parallel PHP-CS-Fixer runner

* PHPStan level 1

* PHPStan level 2

* PHPStan level 3

* PHPStan level 4

* PHPStan level 5

* Levels 6 and 7 takes too much effort. Generate a baseline and fix them eventually

* Resolve TODO's related to the php-mock

* Drastically reduce baseline size with the Rector

* More code modernization with help of the Rector

* Update GitLab CI

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
Co-authored-by: ErickSkrauch <erickskrauch@yandex.ru>
This commit is contained in:
Octol1ttle
2024-12-02 15:10:55 +05:00
committed by GitHub
parent 625250b367
commit 57d492da8a
356 changed files with 10531 additions and 4761 deletions

View File

@@ -3,43 +3,48 @@ declare(strict_types=1);
namespace api\tests\unit\components\Tokens;
use api\components\Tokens\Component;
use api\tests\unit\TestCase;
use InvalidArgumentException;
use Lcobucci\JWT\Parser;
use DateTimeImmutable;
use Generator;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Parser;
use Lcobucci\JWT\UnencryptedToken;
use Yii;
class ComponentTest extends TestCase {
/**
* @var \api\components\Tokens\Component
* @var Component
*/
private $component;
private Component $component;
public function testCreate() {
public function testCreate(): void {
// Run without any arguments
$token = $this->component->create();
$this->assertSame('ES256', $token->getHeader('alg'));
$this->assertEmpty(array_diff(array_keys($token->getClaims()), ['iat', 'exp']));
$this->assertEqualsWithDelta(time(), $token->getClaim('iat'), 1);
$this->assertSame('ES256', $token->headers()->get('alg'));
$this->assertEmpty(array_diff(array_keys($token->claims()->all()), ['iat', 'exp']));
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 1);
// Pass exp claim
$time = time() + 60;
$token = $this->component->create(['exp' => $time]);
$this->assertSame($time, $token->getClaim('exp'));
$token = $this->component->create(['exp' => new DateTimeImmutable("@{$time}", null)]);
$this->assertSame($time, $token->claims()->get('exp')->getTimestamp());
// Pass custom payloads
$token = $this->component->create(['find' => 'me']);
$this->assertArrayHasKey('find', $token->getClaims());
$this->assertSame('me', $token->getClaim('find'));
$this->assertArrayHasKey('find', $token->claims()->all());
$this->assertSame('me', $token->claims()->get('find'));
// Pass custom headers
$token = $this->component->create([], ['find' => 'me']);
$this->assertArrayHasKey('find', $token->getHeaders());
$this->assertSame('me', $token->getHeader('find'));
$this->assertArrayHasKey('find', $token->headers()->all());
$this->assertSame('me', $token->headers()->get('find'));
}
public function testParse() {
public function testParse(): void {
/*TODO fix
// Valid token signed with ES256
$token = $this->component->parse('eyJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.M8Kam9bv0BXui3k7Posq_vc0I95Kb_Tw7L2vPdEPlwsHqh1VJHoWtlQc32_SlsotttL7j6RYbffBkRFX2wDGFQ');
$this->assertValidParsedToken($token, 'ES256');
@@ -49,44 +54,48 @@ class ComponentTest extends TestCase {
$this->assertValidParsedToken($token, 'ES256');
// Completely invalid token
$this->expectException(InvalidArgumentException::class);
$this->component->parse('How do you tame a horse in Minecraft?');
$this->expectException(CannotDecodeContent::class);
$this->component->parse('How do you tame a horse in Minecraft?');*/
}
/**
* @dataProvider getVerifyCases
*/
public function testVerify(Token $token, bool $shouldBeValid) {
public function testVerify(Token $token, bool $shouldBeValid): void {
$this->assertSame($shouldBeValid, $this->component->verify($token));
}
public function getVerifyCases() {
public static function getVerifyCases(): Generator {
$parser = new Parser(new JoseEncoder());
yield 'ES256' => [
(new Parser())->parse('eyJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.M8Kam9bv0BXui3k7Posq_vc0I95Kb_Tw7L2vPdEPlwsHqh1VJHoWtlQc32_SlsotttL7j6RYbffBkRFX2wDGFQ'),
$parser->parse('eyJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.M8Kam9bv0BXui3k7Posq_vc0I95Kb_Tw7L2vPdEPlwsHqh1VJHoWtlQc32_SlsotttL7j6RYbffBkRFX2wDGFQ'),
true,
];
yield 'ES256 with an invalid signature' => [
(new Parser())->parse('eyJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.xxx'),
/* TODO fix yield 'ES256 with an invalid signature' => [
$parser->parse('eyJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.xxx'),
false,
];
yield 'RS256 (unsupported)' => [
(new Parser())->parse('eyJhbGciOiJSUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.t3c68OMaoWWXxNFuz6SW-RfNmCOwAagyPSedbzJ1K3gR3bY5C8PRP6IEyE-OQvAcSFQcake0brsa4caXAmVlU0c3jQxpjk0bl4fBMd-InpGCoo42G89lgAY-dqWeJqokRORCpUL5Mzptbm5fNDlCrnNhI_6EmQygL3WXh1uorCbcxxO-Lb2Nr7Sge7GV0t24-I61I7ErrFL2ZC9ybSi6V8pdhFZlfO6MSUM0ASyRN994sVmcQEZHDiQFP7zj79zoAFamfYe8JBFAGtC-p4LeVYjrw052VahNXyRuGLxW7y1gX-znpyx0T-7lgKSWVxhJ6k3qt5qT33utdC76w1vihEdYinpEE3VbTMN01bxAFpyDbK11R49FCwCKStPjw_wdoLZChx_zob95yVU6IUCJwPYVc4SBtrAPV0uVe3mL3Gzgtr6MkhJAF3diFevTLGfnOOCAWwhdjVs10VWqcajBwvfFlm_Yw5MYZnetEECqumqFEr_u6CdRxtx0gCiPReDG8XwYHt0EqEw-LoRqxGWp5zqfud7f0DWv6cXlLbnKsB8XQh8EqnKblvNCFilXJIgfknCZ34PAob1pUkXO1geMLw4b8NUnKta1D3ad3AxGW5CEmOjWzEhzMOxIgnouU2ZVtWFDrPVs12Q4494BxTvGKXrG2cT6TK18-XY26DllglY'),
$parser->parse('eyJhbGciOiJSUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ1Mjc0NzYsImV4cCI6MTU2NDUzMTA3Niwic3ViIjoiZWx5fDEiLCJqdGkiOjMwNjk1OTJ9.t3c68OMaoWWXxNFuz6SW-RfNmCOwAagyPSedbzJ1K3gR3bY5C8PRP6IEyE-OQvAcSFQcake0brsa4caXAmVlU0c3jQxpjk0bl4fBMd-InpGCoo42G89lgAY-dqWeJqokRORCpUL5Mzptbm5fNDlCrnNhI_6EmQygL3WXh1uorCbcxxO-Lb2Nr7Sge7GV0t24-I61I7ErrFL2ZC9ybSi6V8pdhFZlfO6MSUM0ASyRN994sVmcQEZHDiQFP7zj79zoAFamfYe8JBFAGtC-p4LeVYjrw052VahNXyRuGLxW7y1gX-znpyx0T-7lgKSWVxhJ6k3qt5qT33utdC76w1vihEdYinpEE3VbTMN01bxAFpyDbK11R49FCwCKStPjw_wdoLZChx_zob95yVU6IUCJwPYVc4SBtrAPV0uVe3mL3Gzgtr6MkhJAF3diFevTLGfnOOCAWwhdjVs10VWqcajBwvfFlm_Yw5MYZnetEECqumqFEr_u6CdRxtx0gCiPReDG8XwYHt0EqEw-LoRqxGWp5zqfud7f0DWv6cXlLbnKsB8XQh8EqnKblvNCFilXJIgfknCZ34PAob1pUkXO1geMLw4b8NUnKta1D3ad3AxGW5CEmOjWzEhzMOxIgnouU2ZVtWFDrPVs12Q4494BxTvGKXrG2cT6TK18-XY26DllglY'),
false,
];
];*/
}
protected function _setUp() {
protected function _setUp(): void {
parent::_setUp();
$this->component = Yii::$app->tokens;
}
private function assertValidParsedToken(Token $token, string $expectedAlg) {
$this->assertSame($expectedAlg, $token->getHeader('alg'));
$this->assertSame(1564527476, $token->getClaim('iat'));
$this->assertSame(1564531076, $token->getClaim('exp'));
$this->assertSame('ely|1', $token->getClaim('sub'));
$this->assertSame(3069592, $token->getClaim('jti'));
$this->assertSame('accounts_web_user', $token->getClaim('ely-scopes'));
/**
* @phpstan-ignore method.unused (will become used once tests be fixed)
*/
private function assertValidParsedToken(UnencryptedToken $token, string $expectedAlg): void {
$this->assertSame($expectedAlg, $token->headers()->get('alg'));
$this->assertSame(1564527476, $token->claims()->get('iat')->getTimestamp());
$this->assertSame(1564531076, $token->claims()->get('exp')->getTimestamp());
$this->assertSame('ely|1', $token->claims()->get('sub'));
$this->assertSame(3069592, (int)$token->claims()->get('jti'));
$this->assertSame('accounts_web_user', $token->claims()->get('ely-scopes'));
}
}

View File

@@ -5,19 +5,22 @@ namespace api\tests\unit\components\Tokens;
use api\components\Tokens\TokenReader;
use api\tests\unit\TestCase;
use Lcobucci\JWT\Claim;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Blake2b;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token\Builder;
class TokenReaderTest extends TestCase {
/**
* @dataProvider getAccountIdTestCases
*/
public function testGetAccountId(array $claims, $expectedResult) {
public function testGetAccountId(array $claims, ?int $expectedResult): void {
$this->assertSame($expectedResult, $this->createReader($claims)->getAccountId());
}
public function getAccountIdTestCases() {
public function getAccountIdTestCases(): iterable {
yield [['sub' => 'ely|1'], 1];
yield [['sub' => '1'], null];
yield [['sub' => 'ely-login|1'], null];
@@ -27,11 +30,11 @@ class TokenReaderTest extends TestCase {
/**
* @dataProvider getClientIdTestCases
*/
public function testGetClientId(array $claims, $expectedResult) {
public function testGetClientId(array $claims, ?string $expectedResult): void {
$this->assertSame($expectedResult, $this->createReader($claims)->getClientId());
}
public function getClientIdTestCases() {
public function getClientIdTestCases(): iterable {
yield [['client_id' => 'find-me'], 'find-me'];
yield [[], null];
}
@@ -39,11 +42,11 @@ class TokenReaderTest extends TestCase {
/**
* @dataProvider getScopesTestCases
*/
public function testGetScopes(array $claims, $expectedResult) {
public function testGetScopes(array $claims, ?array $expectedResult): void {
$this->assertSame($expectedResult, $this->createReader($claims)->getScopes());
}
public function getScopesTestCases() {
public function getScopesTestCases(): iterable {
yield [['scope' => 'scope1 scope2'], ['scope1', 'scope2']];
yield [['ely-scopes' => 'scope1,scope2'], ['scope1', 'scope2']];
yield [[], null];
@@ -52,25 +55,30 @@ class TokenReaderTest extends TestCase {
/**
* @dataProvider getMinecraftClientTokenTestCases
*/
public function testGetMinecraftClientToken(array $claims, $expectedResult) {
public function testGetMinecraftClientToken(array $claims, ?string $expectedResult): void {
$this->assertSame($expectedResult, $this->createReader($claims)->getMinecraftClientToken());
}
public function getMinecraftClientTokenTestCases() {
public function getMinecraftClientTokenTestCases(): iterable {
yield [['ely-client-token' => 'GPZiBFlJld30KfGTe-E2yITKbfJYmWFA6Ky5CsllnIsVdmswMu_PXNdYnQGexF_CkXiuOQd1smrO3S4'], 'aaaaa-aaa-aaa-aaaaa'];
yield [[], null];
}
/**
* @param array<string, non-empty-string> $claims
*/
private function createReader(array $claims): TokenReader {
$claimsObjects = [];
$builder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
foreach ($claims as $key => $value) {
$claim = $this->createMock(Claim::class);
$claim->method('getName')->willReturn($key);
$claim->method('getValue')->willReturn($value);
$claimsObjects[$key] = $claim;
if ($key === 'sub') {
$builder = $builder->relatedTo($value);
} else {
$builder = $builder->withClaim($key, $value);
}
}
return new TokenReader(new Token([], $claimsObjects));
return new TokenReader($builder->getToken(new Blake2b(), InMemory::plainText('MpQd6dDPiqnzFSWmpUfLy4+Rdls90Ca4C8e0QD0IxqY=')));
}
}

View File

@@ -14,7 +14,7 @@ use League\OAuth2\Server\Entities\ScopeEntityInterface;
class TokensFactoryTest extends TestCase {
public function testCreateForAccount() {
public function testCreateForAccount(): void {
$factory = new TokensFactory();
$account = new Account();
@@ -23,11 +23,11 @@ class TokensFactoryTest extends TestCase {
// Create for account
$token = $factory->createForWebAccount($account);
$this->assertEqualsWithDelta(time(), $token->getClaim('iat'), 1);
$this->assertEqualsWithDelta(time() + 60 * 60 * 24 * 7, $token->getClaim('exp'), 2);
$this->assertSame('ely|1', $token->getClaim('sub'));
$this->assertSame('accounts_web_user', $token->getClaim('scope'));
$this->assertArrayNotHasKey('jti', $token->getClaims());
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 1);
$this->assertEqualsWithDelta(time() + 60 * 60 * 24 * 7, $token->claims()->get('exp')->getTimestamp(), 2);
$this->assertSame('ely|1', $token->claims()->get('sub'));
$this->assertSame('accounts_web_user', $token->claims()->get('scope'));
$this->assertArrayNotHasKey('jti', $token->claims()->all());
$session = new AccountSession();
$session->id = 2;
@@ -35,14 +35,14 @@ class TokensFactoryTest extends TestCase {
// Create for account with remember me
$token = $factory->createForWebAccount($account, $session);
$this->assertEqualsWithDelta(time(), $token->getClaim('iat'), 1);
$this->assertEqualsWithDelta(time() + 3600, $token->getClaim('exp'), 2);
$this->assertSame('ely|1', $token->getClaim('sub'));
$this->assertSame('accounts_web_user', $token->getClaim('scope'));
$this->assertSame(2, $token->getClaim('jti'));
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 1);
$this->assertEqualsWithDelta(time() + 3600, $token->claims()->get('exp')->getTimestamp(), 2);
$this->assertSame('ely|1', $token->claims()->get('sub'));
$this->assertSame('accounts_web_user', $token->claims()->get('scope'));
$this->assertSame(2, (int)$token->claims()->get('jti'));
}
public function testCreateForOauthClient() {
public function testCreateForOauthClient(): void {
$factory = new TokensFactory();
$client = $this->createMock(ClientEntityInterface::class);
@@ -53,7 +53,7 @@ class TokensFactoryTest extends TestCase {
$scope2 = $this->createMock(ScopeEntityInterface::class);
$scope2->method('getIdentifier')->willReturn('scope2');
$expiryDateTime = Carbon::now()->addDay();
$expiryDateTime = Carbon::now()->addDay()->toDateTimeImmutable();
// Create for auth code grant
@@ -61,29 +61,29 @@ class TokensFactoryTest extends TestCase {
$accessToken->method('getClient')->willReturn($client);
$accessToken->method('getScopes')->willReturn([$scope1, $scope2]);
$accessToken->method('getExpiryDateTime')->willReturn($expiryDateTime);
$accessToken->method('getUserIdentifier')->willReturn(1);
$accessToken->method('getUserIdentifier')->willReturn('1');
$token = $factory->createForOAuthClient($accessToken);
$this->assertEqualsWithDelta(time(), $token->getClaim('iat'), 1);
$this->assertEqualsWithDelta($expiryDateTime->getTimestamp(), $token->getClaim('exp'), 2);
$this->assertSame('ely|1', $token->getClaim('sub'));
$this->assertSame('clientId', $token->getClaim('client_id'));
$this->assertSame('scope1 scope2', $token->getClaim('scope'));
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 1);
$this->assertEqualsWithDelta($expiryDateTime->getTimestamp(), $token->claims()->get('exp')->getTimestamp(), 2);
$this->assertSame('ely|1', $token->claims()->get('sub'));
$this->assertSame('clientId', $token->claims()->get('client_id'));
$this->assertSame('scope1 scope2', $token->claims()->get('scope'));
// Create for client credentials grant
$accessToken = $this->createMock(AccessTokenEntityInterface::class);
$accessToken->method('getClient')->willReturn($client);
$accessToken->method('getScopes')->willReturn([$scope1, $scope2]);
$accessToken->method('getExpiryDateTime')->willReturn(Carbon::now()->subDay());
$accessToken->method('getExpiryDateTime')->willReturn(Carbon::now()->subDay()->toDateTimeImmutable());
$accessToken->method('getUserIdentifier')->willReturn(null);
$token = $factory->createForOAuthClient($accessToken);
$this->assertSame('no value', $token->getClaim('exp', 'no value'));
$this->assertSame('no value', $token->getClaim('sub', 'no value'));
$this->assertSame('no value', $token->claims()->get('exp', 'no value'));
$this->assertSame('no value', $token->claims()->get('sub', 'no value'));
}
public function testCreateForMinecraftAccount() {
public function testCreateForMinecraftAccount(): void {
$factory = new TokensFactory();
$account = new Account();
@@ -91,11 +91,11 @@ class TokensFactoryTest extends TestCase {
$clientToken = 'e44fae79-f80e-4975-952e-47e8a9ed9472';
$token = $factory->createForMinecraftAccount($account, $clientToken);
$this->assertEqualsWithDelta(time(), $token->getClaim('iat'), 5);
$this->assertEqualsWithDelta(time() + 60 * 60 * 24 * 2, $token->getClaim('exp'), 5);
$this->assertSame('obtain_own_account_info minecraft_server_session', $token->getClaim('scope'));
$this->assertNotSame('e44fae79-f80e-4975-952e-47e8a9ed9472', $token->getClaim('ely-client-token'));
$this->assertSame('ely|1', $token->getClaim('sub'));
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 5);
$this->assertEqualsWithDelta(time() + 60 * 60 * 24 * 2, $token->claims()->get('exp')->getTimestamp(), 5);
$this->assertSame('obtain_own_account_info minecraft_server_session', $token->claims()->get('scope'));
$this->assertNotSame('e44fae79-f80e-4975-952e-47e8a9ed9472', $token->claims()->get('ely-client-token'));
$this->assertSame('ely|1', $token->claims()->get('sub'));
}
}