fix sanctum access token wrapper closes #5591

This commit is contained in:
robert dennis
2026-06-21 18:52:23 -04:00
parent 380e5c5605
commit 01bf7b44e7
2 changed files with 60 additions and 10 deletions
@@ -3,21 +3,38 @@
namespace Pterodactyl\Extensions\Laravel\Sanctum;
use Pterodactyl\Models\ApiKey;
use Laravel\Sanctum\NewAccessToken as SanctumAccessToken;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
/**
* @property ApiKey $accessToken
*/
class NewAccessToken extends SanctumAccessToken
class NewAccessToken implements Arrayable, Jsonable
{
/**
* NewAccessToken constructor.
*
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(ApiKey $accessToken, string $plainTextToken)
public function __construct(public ApiKey $accessToken, public string $plainTextToken)
{
$this->accessToken = $accessToken;
$this->plainTextToken = $plainTextToken;
}
/**
* Get the instance as an array.
*
* @return array<string, ApiKey|string>
*/
public function toArray()
{
return [
'accessToken' => $this->accessToken,
'plainTextToken' => $this->plainTextToken,
];
}
/**
* Convert the object to its JSON representation.
*
* @param int $options
*/
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}
}
@@ -0,0 +1,33 @@
<?php
namespace Pterodactyl\Tests\Unit\Extensions\Laravel\Sanctum;
use Pterodactyl\Models\ApiKey;
use Pterodactyl\Tests\TestCase;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Laravel\Sanctum\NewAccessToken as SanctumAccessToken;
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
class NewAccessTokenTest extends TestCase
{
/**
* Test that the access token wrapper keeps supporting Sanctum's DTO contract.
*/
public function testAccessTokenWrapperSupportsSanctumDtoContract()
{
$apiKey = ApiKey::factory()->make();
$token = new NewAccessToken($apiKey, 'plain-text-token');
$this->assertInstanceOf(Arrayable::class, $token);
$this->assertInstanceOf(Jsonable::class, $token);
$this->assertNotInstanceOf(SanctumAccessToken::class, $token);
$this->assertSame($apiKey, $token->accessToken);
$this->assertSame('plain-text-token', $token->plainTextToken);
$this->assertSame([
'accessToken' => $apiKey,
'plainTextToken' => 'plain-text-token',
], $token->toArray());
$this->assertSame(json_encode($token->toArray()), $token->toJson());
}
}