Skip to content

Commit

Permalink
Merge pull request #66 from spiral/hotfix/token-storage-registration
Browse files Browse the repository at this point in the history
Redesigned auth token registration.
  • Loading branch information
butschster committed Feb 21, 2023
2 parents e430219 + 1d59120 commit 2068c36
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -27,7 +27,7 @@
"cycle/schema-renderer": "^1.2",
"doctrine/inflector": "^1.4 || ^2.0",
"spiral/attributes": "^2.10 || ^3.0",
"spiral/framework": "^3.0",
"spiral/framework": "^3.3",
"spiral/reactor": "^3.0",
"spiral/scaffolder": "^3.0",
"spiral/prototype": "^3.0",
Expand Down
35 changes: 27 additions & 8 deletions src/Bootloader/AuthTokensBootloader.php
Expand Up @@ -4,28 +4,47 @@

namespace Spiral\Cycle\Bootloader;

use Spiral\Auth\Config\AuthConfig;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Set;
use Spiral\Cycle\Auth\Token;
use Spiral\Cycle\Auth\TokenStorage as CycleStorage;
use Spiral\Auth\TokenStorageInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Auth\HttpAuthBootloader;
use Spiral\Tokenizer\Bootloader\TokenizerBootloader;

final class AuthTokensBootloader extends Bootloader
{
private const TOKEN_STORAGE_NAME = 'cycle';

protected const DEPENDENCIES = [
HttpAuthBootloader::class,
CycleOrmBootloader::class,
AnnotatedBootloader::class,
];

protected const SINGLETONS = [
TokenStorageInterface::class => CycleStorage::class,
];
public function __construct(
private readonly ConfiguratorInterface $config,
) {
}

public function init(
TokenizerBootloader $tokenizer,
HttpAuthBootloader $bootloader,
EnvironmentInterface $env
): void {
$bootloader->addTokenStorage(self::TOKEN_STORAGE_NAME, CycleStorage::class);

// This is a temporary fix and backward compatibility for case when AUTH_TOKEN_STORAGE is not set.
// TODO: Will be removed after release of spiral/framework 4.0.0
if ($env->get('AUTH_TOKEN_STORAGE') === null) {
$this->config->modify(
AuthConfig::CONFIG,
new Set('defaultStorage', self::TOKEN_STORAGE_NAME),
);
}

public function init(TokenizerBootloader $tokenizer): void
{
$tokenClass = new \ReflectionClass(Token::class);
$tokenizer->addDirectory(dirname($tokenClass->getFileName()));
$tokenizer->addDirectory(\dirname($tokenClass->getFileName()));
}
}
3 changes: 2 additions & 1 deletion tests/src/Auth/TokenStorageTest.php
Expand Up @@ -6,6 +6,7 @@

use Spiral\Auth\TokenInterface;
use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\TokenStorageProviderInterface;
use Spiral\Cycle\Auth\TokenStorage;
use Spiral\Tests\BaseTest;

Expand All @@ -17,7 +18,7 @@ protected function setUp(): void
{
parent::setUp();

$this->storage = $this->getContainer()->get(TokenStorageInterface::class);
$this->storage = $this->getContainer()->get(TokenStorageProviderInterface::class)->getStorage('cycle');
}

public function testTokenShouldBeCreatedWithoutExpiration()
Expand Down
7 changes: 4 additions & 3 deletions tests/src/Bootloader/AuthTokensBootloaderTest.php
Expand Up @@ -4,16 +4,17 @@

namespace Spiral\Tests\Bootloader;

use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\Config\AuthConfig;
use Spiral\Cycle\Auth\Token;
use Spiral\Cycle\Auth\TokenStorage as CycleStorage;
use Spiral\Cycle\Auth\TokenStorage;
use Spiral\Tests\BaseTest;

final class AuthTokensBootloaderTest extends BaseTest
{
public function testGetsTokenStorage(): void
{
$this->assertContainerBoundAsSingleton(TokenStorageInterface::class, CycleStorage::class);
$storages = $this->getConfig(AuthConfig::CONFIG)['storages'];
$this->assertSame(TokenStorage::class, $storages['cycle']);
}

public function testTokenEntityShouldBeRegisterInTokenizer(): void
Expand Down

0 comments on commit 2068c36

Please sign in to comment.