2019-12-13 16:25:09 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\tests\unit\components\Tokens;
|
|
|
|
|
|
|
|
use api\components\Tokens\TokenReader;
|
|
|
|
use api\tests\unit\TestCase;
|
2024-12-02 15:40:55 +05:30
|
|
|
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;
|
2019-12-13 16:25:09 +05:30
|
|
|
|
|
|
|
class TokenReaderTest extends TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAccountIdTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetAccountId(array $claims, ?int $expectedResult): void {
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertSame($expectedResult, $this->createReader($claims)->getAccountId());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getAccountIdTestCases(): iterable {
|
2019-12-13 16:25:09 +05:30
|
|
|
yield [['sub' => 'ely|1'], 1];
|
|
|
|
yield [['sub' => '1'], null];
|
|
|
|
yield [['sub' => 'ely-login|1'], null];
|
|
|
|
yield [[], null];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getClientIdTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetClientId(array $claims, ?string $expectedResult): void {
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertSame($expectedResult, $this->createReader($claims)->getClientId());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getClientIdTestCases(): iterable {
|
2019-12-13 16:25:09 +05:30
|
|
|
yield [['client_id' => 'find-me'], 'find-me'];
|
|
|
|
yield [[], null];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getScopesTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetScopes(array $claims, ?array $expectedResult): void {
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertSame($expectedResult, $this->createReader($claims)->getScopes());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getScopesTestCases(): iterable {
|
2019-12-13 16:25:09 +05:30
|
|
|
yield [['scope' => 'scope1 scope2'], ['scope1', 'scope2']];
|
|
|
|
yield [['ely-scopes' => 'scope1,scope2'], ['scope1', 'scope2']];
|
|
|
|
yield [[], null];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getMinecraftClientTokenTestCases
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetMinecraftClientToken(array $claims, ?string $expectedResult): void {
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertSame($expectedResult, $this->createReader($claims)->getMinecraftClientToken());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getMinecraftClientTokenTestCases(): iterable {
|
2019-12-13 16:25:09 +05:30
|
|
|
yield [['ely-client-token' => 'GPZiBFlJld30KfGTe-E2yITKbfJYmWFA6Ky5CsllnIsVdmswMu_PXNdYnQGexF_CkXiuOQd1smrO3S4'], 'aaaaa-aaa-aaa-aaaaa'];
|
|
|
|
yield [[], null];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/**
|
|
|
|
* @param array<string, non-empty-string> $claims
|
|
|
|
*/
|
2019-12-13 16:25:09 +05:30
|
|
|
private function createReader(array $claims): TokenReader {
|
2024-12-02 15:40:55 +05:30
|
|
|
$builder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
|
|
|
|
2019-12-13 16:25:09 +05:30
|
|
|
foreach ($claims as $key => $value) {
|
2024-12-02 15:40:55 +05:30
|
|
|
if ($key === 'sub') {
|
|
|
|
$builder = $builder->relatedTo($value);
|
|
|
|
} else {
|
|
|
|
$builder = $builder->withClaim($key, $value);
|
|
|
|
}
|
2019-12-13 16:25:09 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
return new TokenReader($builder->getToken(new Blake2b(), InMemory::plainText('MpQd6dDPiqnzFSWmpUfLy4+Rdls90Ca4C8e0QD0IxqY=')));
|
2019-12-13 16:25:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|