Update to Laravel 8
Co-authored-by: Matthew Penner <me@matthewp.io>
This commit is contained in:
parent
028921b42a
commit
a043071e3c
211 changed files with 4394 additions and 2933 deletions
|
@ -2,12 +2,13 @@
|
|||
|
||||
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Lcobucci\JWT\Parser;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Http\Response;
|
||||
use Lcobucci\JWT\Configuration;
|
||||
use Pterodactyl\Models\Permission;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||
use Lcobucci\JWT\Signer\Key\InMemory;
|
||||
use Lcobucci\JWT\Validation\Constraint\SignedWith;
|
||||
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
||||
|
||||
class WebsocketControllerTest extends ClientApiIntegrationTestCase
|
||||
|
@ -32,8 +33,6 @@ class WebsocketControllerTest extends ClientApiIntegrationTestCase
|
|||
*/
|
||||
public function testJwtAndWebsocketUrlAreReturnedForServerOwner()
|
||||
{
|
||||
CarbonImmutable::setTestNow(Carbon::now());
|
||||
|
||||
/** @var \Pterodactyl\Models\User $user */
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
|
@ -51,22 +50,33 @@ class WebsocketControllerTest extends ClientApiIntegrationTestCase
|
|||
$this->assertStringStartsWith('wss://', $connection, 'Failed asserting that websocket connection address has expected "wss://" prefix.');
|
||||
$this->assertStringEndsWith("/api/servers/{$server->uuid}/ws", $connection, 'Failed asserting that websocket connection address uses expected Wings endpoint.');
|
||||
|
||||
$token = (new Parser)->parse($response->json('data.token'));
|
||||
$config = Configuration::forSymmetricSigner(new Sha256, $key = InMemory::plainText($server->node->getDecryptedKey()));
|
||||
$config->setValidationConstraints(new SignedWith(new Sha256, $key));
|
||||
/** @var \Lcobucci\JWT\Token\Plain $token */
|
||||
$token = $config->parser()->parse($response->json('data.token'));
|
||||
|
||||
$this->assertTrue(
|
||||
$token->verify(new Sha256, $server->node->getDecryptedKey()),
|
||||
$config->validator()->validate($token, ...$config->validationConstraints()),
|
||||
'Failed to validate that the JWT data returned was signed using the Node\'s secret key.'
|
||||
);
|
||||
|
||||
// The way we generate times for the JWT will truncate the microseconds from the
|
||||
// time, but CarbonImmutable::now() will include them, thus causing test failures.
|
||||
//
|
||||
// This little chunk of logic just strips those out by generating a new CarbonImmutable
|
||||
// instance from the current timestamp, which is how the JWT works. We also need to
|
||||
// switch to UTC here for consistency.
|
||||
$expect = CarbonImmutable::createFromTimestamp(CarbonImmutable::now()->getTimestamp())->timezone('UTC');
|
||||
|
||||
// Check that the claims are generated correctly.
|
||||
$this->assertSame(config('app.url'), $token->getClaim('iss'));
|
||||
$this->assertSame($server->node->getConnectionAddress(), $token->getClaim('aud'));
|
||||
$this->assertSame(CarbonImmutable::now()->getTimestamp(), $token->getClaim('iat'));
|
||||
$this->assertSame(CarbonImmutable::now()->subMinutes(5)->getTimestamp(), $token->getClaim('nbf'));
|
||||
$this->assertSame(CarbonImmutable::now()->addMinutes(10)->getTimestamp(), $token->getClaim('exp'));
|
||||
$this->assertSame($user->id, $token->getClaim('user_id'));
|
||||
$this->assertSame($server->uuid, $token->getClaim('server_uuid'));
|
||||
$this->assertSame(['*'], $token->getClaim('permissions'));
|
||||
$this->assertTrue($token->hasBeenIssuedBy(config('app.url')));
|
||||
$this->assertTrue($token->isPermittedFor($server->node->getConnectionAddress()));
|
||||
$this->assertEquals($expect, $token->claims()->get('iat'));
|
||||
$this->assertEquals($expect->subMinutes(5), $token->claims()->get('nbf'));
|
||||
$this->assertEquals($expect->addMinutes(10), $token->claims()->get('exp'));
|
||||
$this->assertSame($user->id, $token->claims()->get('user_id'));
|
||||
$this->assertSame($server->uuid, $token->claims()->get('server_uuid'));
|
||||
$this->assertSame(['*'], $token->claims()->get('permissions'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,14 +95,17 @@ class WebsocketControllerTest extends ClientApiIntegrationTestCase
|
|||
$response->assertOk();
|
||||
$response->assertJsonStructure(['data' => ['token', 'socket']]);
|
||||
|
||||
$token = (new Parser)->parse($response->json('data.token'));
|
||||
$config = Configuration::forSymmetricSigner(new Sha256, $key = InMemory::plainText($server->node->getDecryptedKey()));
|
||||
$config->setValidationConstraints(new SignedWith(new Sha256, $key));
|
||||
/** @var \Lcobucci\JWT\Token\Plain $token */
|
||||
$token = $config->parser()->parse($response->json('data.token'));
|
||||
|
||||
$this->assertTrue(
|
||||
$token->verify(new Sha256, $server->node->getDecryptedKey()),
|
||||
$config->validator()->validate($token, ...$config->validationConstraints()),
|
||||
'Failed to validate that the JWT data returned was signed using the Node\'s secret key.'
|
||||
);
|
||||
|
||||
// Check that the claims are generated correctly.
|
||||
$this->assertSame($permissions, $token->getClaim('permissions'));
|
||||
$this->assertSame($permissions, $token->claims()->get('permissions'));
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue