-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[spiral/auth-http] Adding TokenStorageScope (#931)
- Loading branch information
Showing
7 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Auth; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use Spiral\Auth\Exception\TokenStorageException; | ||
use Spiral\Core\Container\SingletonInterface; | ||
use Spiral\Core\Exception\ScopeException; | ||
|
||
final class TokenStorageScope implements TokenStorageInterface, SingletonInterface | ||
{ | ||
public function __construct( | ||
private readonly ContainerInterface $container | ||
) { | ||
} | ||
|
||
/** | ||
* Load token by id, must return null if token not found. | ||
* | ||
* @throws TokenStorageException | ||
*/ | ||
public function load(string $id): ?TokenInterface | ||
{ | ||
return $this->getTokenStorage()->load($id); | ||
} | ||
|
||
/** | ||
* Create token based on the payload provided by actor provider. | ||
* | ||
* @throws TokenStorageException | ||
*/ | ||
public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface | ||
{ | ||
return $this->getTokenStorage()->create($payload, $expiresAt); | ||
} | ||
|
||
/** | ||
* Delete token from the persistent storage. | ||
* | ||
* @throws TokenStorageException | ||
*/ | ||
public function delete(TokenInterface $token): void | ||
{ | ||
$this->getTokenStorage()->delete($token); | ||
} | ||
|
||
/** | ||
* @throws ScopeException | ||
*/ | ||
private function getTokenStorage(): TokenStorageInterface | ||
{ | ||
try { | ||
return $this->container->get(TokenStorageInterface::class); | ||
} catch (NotFoundExceptionInterface $e) { | ||
throw new ScopeException('Unable to resolve token storage, invalid scope', $e->getCode(), $e); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/Framework/Auth/AuthConfigTest.php → .../Framework/Auth/Config/AuthConfigTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\Auth; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Spiral\Auth\TokenInterface; | ||
use Spiral\Auth\TokenStorageInterface; | ||
use Spiral\Auth\TokenStorageScope; | ||
use Spiral\Core\Container; | ||
|
||
final class TokenStorageScopeTest extends TestCase | ||
{ | ||
public function testLoad(): void | ||
{ | ||
$storage = $this->createMock(TokenStorageInterface::class); | ||
$storage | ||
->expects($this->once()) | ||
->method('load') | ||
->with('foo') | ||
->willReturn($token = $this->createMock(TokenInterface::class)); | ||
|
||
$container = new Container(); | ||
$container->bind(TokenStorageInterface::class, $storage); | ||
|
||
$scope = new TokenStorageScope($container); | ||
|
||
$this->assertSame($token, $scope->load('foo')); | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
$expiresAt = new \DateTimeImmutable(); | ||
|
||
$storage = $this->createMock(TokenStorageInterface::class); | ||
$storage | ||
->expects($this->once()) | ||
->method('create') | ||
->with(['foo' => 'bar'], $expiresAt) | ||
->willReturn($token = $this->createMock(TokenInterface::class)); | ||
|
||
$container = new Container(); | ||
$container->bind(TokenStorageInterface::class, $storage); | ||
|
||
$scope = new TokenStorageScope($container); | ||
|
||
$this->assertSame($token, $scope->create(['foo' => 'bar'], $expiresAt)); | ||
} | ||
|
||
public function testDelete(): void | ||
{ | ||
$token = $this->createMock(TokenInterface::class); | ||
|
||
$storage = $this->createMock(TokenStorageInterface::class); | ||
$storage | ||
->expects($this->once()) | ||
->method('delete') | ||
->with($token); | ||
|
||
$container = new Container(); | ||
$container->bind(TokenStorageInterface::class, $storage); | ||
|
||
$scope = new TokenStorageScope($container); | ||
|
||
$scope->delete($token); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Framework/AuthHttp/Middleware/AuthMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Tests\AuthHttp\Middleware; | ||
|
||
use Spiral\Auth\Middleware\AuthMiddleware; | ||
use Spiral\Auth\TokenStorageInterface; | ||
use Spiral\Auth\TokenStorageScope; | ||
use Spiral\Core\Container\Autowire; | ||
use Spiral\Tests\Framework\HttpTestCase; | ||
|
||
final class AuthMiddlewareTest extends HttpTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->enableMiddlewares(); | ||
} | ||
|
||
public function testTokenStorageInterfaceShouldBeBound(): void | ||
{ | ||
$storage = $this->createMock(TokenStorageInterface::class); | ||
$this->getContainer()->bind( | ||
AuthMiddleware::class, | ||
new Autowire(AuthMiddleware::class, ['tokenStorage' => $storage]) | ||
); | ||
$this->setHttpHandler(function () use ($storage): void { | ||
$scope = $this->getContainer()->get(TokenStorageScope::class); | ||
$ref = new \ReflectionMethod($scope, 'getTokenStorage'); | ||
|
||
$this->assertInstanceOf($storage::class, $ref->invoke($scope)); | ||
$this->assertSame($storage, $ref->invoke($scope)); | ||
}); | ||
|
||
$scope = $this->getContainer()->get(TokenStorageScope::class); | ||
$ref = new \ReflectionMethod($scope, 'getTokenStorage'); | ||
$this->assertNotInstanceOf($storage::class, $ref->invoke($scope)); | ||
|
||
$this->getHttp()->get('/'); | ||
|
||
$scope = $this->getContainer()->get(TokenStorageScope::class); | ||
$ref = new \ReflectionMethod($scope, 'getTokenStorage'); | ||
$this->assertNotInstanceOf($storage::class, $ref->invoke($scope)); | ||
} | ||
} |