Tokens now store a hashed row in micropub_tokens instead of being self-contained signed JWTs, so a leaked or unwanted token can actually be revoked. Since revocation already requires a DB lookup on every request, JWT's stateless-verification benefit was gone anyway, so this also drops the lcobucci/jwt dependency entirely. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014625MfkGZ7GVdbqKme4a8L
74 lines
2.1 KiB
PHP
74 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\MicropubToken;
|
|
use App\Services\TokenService;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class TokenServiceTest extends TestCase
|
|
{
|
|
/**
|
|
* Given the token is dependent on a random value and stored only as a
|
|
* hash, to test, we shall create a token, and then verify it.
|
|
*/
|
|
#[Test]
|
|
public function tokenservice_creates_valid_tokens(): void
|
|
{
|
|
$tokenService = new TokenService;
|
|
$data = [
|
|
'me' => 'https://example.org',
|
|
'client_id' => 'https://quill.p3k.io',
|
|
'scope' => 'post',
|
|
];
|
|
$token = $tokenService->getNewToken($data);
|
|
|
|
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer '.$token]);
|
|
|
|
$response->assertJson([
|
|
'response' => 'token',
|
|
'token' => [
|
|
'me' => $data['me'],
|
|
'client_id' => $data['client_id'],
|
|
'scope' => $data['scope'],
|
|
],
|
|
]);
|
|
}
|
|
|
|
#[Test]
|
|
public function unknown_tokens_are_not_valid(): void
|
|
{
|
|
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer '.bin2hex(random_bytes(32))]);
|
|
|
|
$response->assertJson([
|
|
'response' => 'error',
|
|
'error' => 'invalid_token',
|
|
'error_description' => 'The provided token did not pass validation',
|
|
]);
|
|
}
|
|
|
|
#[Test]
|
|
public function revoked_tokens_are_not_valid(): void
|
|
{
|
|
$tokenService = new TokenService;
|
|
$data = [
|
|
'me' => 'https://example.org',
|
|
'client_id' => 'https://quill.p3k.io',
|
|
'scope' => 'post',
|
|
];
|
|
$token = $tokenService->getNewToken($data);
|
|
|
|
MicropubToken::where('token_hash', hash('sha256', $token))->firstOrFail()->revoke();
|
|
|
|
$response = $this->get('/api/post', ['HTTP_Authorization' => 'Bearer '.$token]);
|
|
|
|
$response->assertJson([
|
|
'response' => 'error',
|
|
'error' => 'invalid_token',
|
|
'error_description' => 'The provided token did not pass validation',
|
|
]);
|
|
}
|
|
}
|