Tests / PHP 8.4 (push) Failing after 1m26s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Narrow ArrayGateway::count() to int, matching what the fixture actually returns. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Controllers\Tests\Unit\Security;
|
|
|
|
use Camelia\Controllers\Exception\BadRequest;
|
|
use Camelia\Controllers\JsonRequest;
|
|
use Camelia\Controllers\Request;
|
|
use Camelia\Controllers\Response;
|
|
use Camelia\Controllers\Router;
|
|
use Camelia\Controllers\Security\BearerAuth;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class BearerAuthTest extends TestCase
|
|
{
|
|
private static function passThrough(Request $request): Response
|
|
{
|
|
return new Response(['ok' => true]);
|
|
}
|
|
|
|
public function testMissingHeaderIs401(): void
|
|
{
|
|
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
|
|
|
$response = $auth->process(new JsonRequest(), self::passThrough(...));
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertSame('unauthorized', $response->body()['code']);
|
|
self::assertSame('Bearer', $response->headers()['WWW-Authenticate']);
|
|
}
|
|
|
|
public function testNonBearerSchemeIs401(): void
|
|
{
|
|
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
|
|
|
$response = $auth->process(
|
|
new JsonRequest(headers: ['authorization' => 'Basic dXNlcjpwYXNz']),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertStringContainsString('Bearer scheme', $response->body()['message']);
|
|
}
|
|
|
|
public function testInvalidTokenIs401(): void
|
|
{
|
|
$auth = new BearerAuth(static fn(string $token): ?array => null);
|
|
|
|
$response = $auth->process(
|
|
new JsonRequest(headers: ['authorization' => 'Bearer expired']),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertStringContainsString('invalid or expired', $response->body()['message']);
|
|
}
|
|
|
|
public function testValidTokenAttachesIdentity(): void
|
|
{
|
|
$auth = new BearerAuth(static fn(string $token): array => [
|
|
'id' => 7,
|
|
'roles' => ['admin'],
|
|
]);
|
|
|
|
$response = $auth->process(
|
|
new JsonRequest(headers: ['authorization' => 'Bearer good-token']),
|
|
static function (Request $request): Response {
|
|
return new Response($request->attribute(BearerAuth::ATTRIBUTE));
|
|
},
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
self::assertSame(['id' => 7, 'roles' => ['admin']], $response->body());
|
|
}
|
|
|
|
public function testSchemeIsCaseInsensitive(): void
|
|
{
|
|
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
|
|
|
$response = $auth->process(
|
|
new JsonRequest(headers: ['authorization' => 'bearer abc']),
|
|
self::passThrough(...),
|
|
);
|
|
|
|
self::assertSame(200, $response->status());
|
|
}
|
|
|
|
public function testRouterRejectsBeforeReachingController(): void
|
|
{
|
|
$router = new Router()->withMiddleware(
|
|
new BearerAuth(static fn(string $token): ?array => null),
|
|
);
|
|
$router->get('/x', static fn(): Response => new Response(['leaked' => true]));
|
|
|
|
$response = $router->dispatch(new JsonRequest(path: '/x'));
|
|
|
|
self::assertSame(401, $response->status());
|
|
self::assertStringNotContainsString('leaked', $response->toJson());
|
|
}
|
|
|
|
public function testRouterAllowsValidToken(): void
|
|
{
|
|
$router = new Router()->withMiddleware(new BearerAuth(static fn(string $token): array => [
|
|
'id' => 1,
|
|
]));
|
|
$router->get(
|
|
'/x',
|
|
static fn(Request $request): Response => new Response($request->attribute(
|
|
BearerAuth::ATTRIBUTE,
|
|
)),
|
|
);
|
|
|
|
$response = $router->dispatch(new JsonRequest(path: '/x', headers: [
|
|
'authorization' => 'Bearer t',
|
|
]));
|
|
|
|
self::assertSame(200, $response->status());
|
|
self::assertSame(['id' => 1], $response->body());
|
|
}
|
|
|
|
public function testEmptyRoleListIsRejectedAtConstruction(): void
|
|
{
|
|
$this->expectException(BadRequest::class);
|
|
|
|
new \Camelia\Controllers\Security\RequireRole([]);
|
|
}
|
|
}
|