Skip to content

Commit

Permalink
Fix configuration replacement for auth in HttpAuthBootloader (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
laxity7 authored Nov 25, 2024
1 parent f45e89c commit 67783c4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
17 changes: 8 additions & 9 deletions src/Framework/Bootloader/Auth/HttpAuthBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,22 @@ public function defineSingletons(): array
];
}

public function init(AbstractKernel $kernel, EnvironmentInterface $env): void
public function init(EnvironmentInterface $env): void
{
$this->config->setDefaults(
AuthConfig::CONFIG,
[
'defaultTransport' => $env->get('AUTH_TOKEN_TRANSPORT', 'cookie'),
'defaultStorage' => $env->get('AUTH_TOKEN_STORAGE', 'session'),
'transports' => [],
'storages' => [],
'transports' => [
'cookie' => $this->createDefaultCookieTransport(),
'header' => new HeaderTransport(header: 'X-Auth-Token'),
],
'storages' => [
'session' => SessionTokenStorage::class,
],
]
);

$kernel->booting(function () {
$this->addTransport('cookie', $this->createDefaultCookieTransport());
$this->addTransport('header', new HeaderTransport('X-Auth-Token'));
$this->addTokenStorage('session', SessionTokenStorage::class);
});
}

/**
Expand Down
83 changes: 76 additions & 7 deletions tests/Framework/Bootloader/Http/HttpAuthBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@
use Spiral\Auth\AuthContext;
use Spiral\Auth\AuthContextInterface;
use Spiral\Auth\Config\AuthConfig;
use Spiral\Auth\Session\TokenStorage;
use Spiral\Auth\Session\TokenStorage as SessionTokenStorage;
use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\TokenStorageProvider;
use Spiral\Auth\TokenStorageProviderInterface;
use Spiral\Auth\Transport\CookieTransport;
use Spiral\Auth\Transport\HeaderTransport;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Bootloader\Auth\HttpAuthBootloader;
use Spiral\Config\LoaderInterface;
use Spiral\Config\ConfigManager;
use Spiral\Config\LoaderInterface;
use Spiral\Framework\Spiral;
use Spiral\Http\CurrentRequest;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Tests\Framework\BaseTestCase;

use function PHPUnit\Framework\assertInstanceOf;

final class HttpAuthBootloaderTest extends BaseTestCase
{
public function testTokenStorageProviderInterfaceBinding(): void
Expand All @@ -31,7 +35,7 @@ public function testTokenStorageProviderInterfaceBinding(): void

public function testTokenStorageInterfaceBinding(): void
{
$this->assertContainerBoundAsSingleton(TokenStorageInterface::class, TokenStorage::class);
$this->assertContainerBoundAsSingleton(TokenStorageInterface::class, SessionTokenStorage::class);
}

public function testProxyAuthContextInterfaceBinding(): void
Expand All @@ -57,12 +61,77 @@ public function testAuthContextInterfaceBinding(): void
public function testConfig(): void
{
$this->assertConfigHasFragments(AuthConfig::CONFIG, [
'defaultStorage' => 'session',
'defaultStorage' => 'session',
'defaultTransport' => 'cookie',
'storages' => [
'session' => SessionTokenStorage::class,
],
'storages' => ['session' => SessionTokenStorage::class],
]);

/** @var array{header: HeaderTransport, cookie: CookieTransport} $config */
$config = $this->getContainer()->get(AuthConfig::class)['transports'] ?? [];
self::assertArrayHasKey('cookie', $config);
self::assertArrayHasKey('header', $config);

self::assertInstanceOf(HeaderTransport::class, $config['header']);
self::assertInstanceOf(CookieTransport::class, $config['cookie']);
}

public function testCorrectDefaultTransports(): void
{
$configs = new ConfigManager($this->createMock(LoaderInterface::class));

$bootloader = new HttpAuthBootloader($configs, $this->getContainer());
$bootloader->init($this->getContainer()->get(EnvironmentInterface::class));

/** @var HeaderTransport $headerTransport */
$headerTransport = $configs->getConfig(AuthConfig::CONFIG)['transports']['header'];
self::assertInstanceOf(HeaderTransport::class, $headerTransport);
$header = (new \ReflectionClass($headerTransport))->getProperty('header');
$this->assertSame('X-Auth-Token', $header->getValue($headerTransport));

/** @var CookieTransport $cookieTransport */
$cookieTransport = $configs->getConfig(AuthConfig::CONFIG)['transports']['cookie'];
self::assertInstanceOf(CookieTransport::class, $cookieTransport);
$cookie = (new \ReflectionClass($cookieTransport))->getProperty('cookie');
$this->assertSame('token', $cookie->getValue($cookieTransport));
}

public function testCorrectConfigurableTransports(): void
{
$loader = new class() implements LoaderInterface {
public function has(string $section): bool
{
return $section === AuthConfig::CONFIG;
}

public function load(string $section): array
{
return [
'defaultTransport' => 'header',
'defaultStorage' => 'session',
'storages' => ['session' => SessionTokenStorage::class],
'transports' => [
'header' => new HeaderTransport(header: 'X-Auth-Token-test'),
'cookie' => new CookieTransport(cookie: 'token-test', basePath: '/'),
],
];
}
};
$configs = new ConfigManager($loader);

$bootloader = new HttpAuthBootloader($configs, $this->getContainer());
$bootloader->init($this->getContainer()->get(EnvironmentInterface::class));

/** @var HeaderTransport $headerTransport */
$headerTransport = $configs->getConfig(AuthConfig::CONFIG)['transports']['header'];
self::assertInstanceOf(HeaderTransport::class, $headerTransport);
$header = (new \ReflectionClass($headerTransport))->getProperty('header');
$this->assertSame('X-Auth-Token-test', $header->getValue($headerTransport));

/** @var CookieTransport $cookieTransport */
$cookieTransport = $configs->getConfig(AuthConfig::CONFIG)['transports']['cookie'];
self::assertInstanceOf(CookieTransport::class, $cookieTransport);
$cookie = (new \ReflectionClass($cookieTransport))->getProperty('cookie');
$this->assertSame('token-test', $cookie->getValue($cookieTransport));
}

public function testAddTokenStorage(): void
Expand Down

0 comments on commit 67783c4

Please sign in to comment.