Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of scopes in sessions and auth, adding CurrentRequest #1080

Merged
merged 28 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5147e08
Using HTTP scopes
msmakouz Feb 17, 2024
e5c84ec
Apply fixes from StyleCI
StyleCIBot Feb 17, 2024
0dba8cb
Improve test
msmakouz Feb 17, 2024
dae1870
Fix CS
msmakouz Feb 17, 2024
1752d6d
Apply fixes from StyleCI
StyleCIBot Feb 17, 2024
f378299
Merge remote-tracking branch 'origin/feature/scopes' into feature/htt…
msmakouz Feb 20, 2024
2f9c1e0
Add PaginationFactory binding
msmakouz Feb 20, 2024
79d18b7
Simplify unit tests
msmakouz Feb 20, 2024
a54de6b
Deprecate SectionScope
msmakouz Feb 20, 2024
07b2f37
Remove unused code
msmakouz Feb 20, 2024
35649c9
Add for CookieQueue in scope
msmakouz Feb 20, 2024
0440963
Remove unused code
msmakouz Feb 20, 2024
23bea6d
Add http scope to Session, SessionFactory
msmakouz Feb 20, 2024
b7e48f4
Add unit test for SessionFactoryInterface binding in root scope
msmakouz Feb 20, 2024
83c195e
Add exceptions test
msmakouz Feb 20, 2024
aefe526
Add http.request scope to InputManager and InputScope
msmakouz Feb 20, 2024
cb6df86
Add http.request scope to CookieManager
msmakouz Feb 20, 2024
69fe624
Add proxy AuthContextInterface binding
msmakouz Feb 26, 2024
66b3b0e
Fix InputInterface binding
msmakouz Feb 26, 2024
9a77110
Fix InputInterface binding and add test
msmakouz Feb 26, 2024
8a6d12d
Merge branch 'feature/scopes' into feature/http-scopes
roxblnfk Mar 11, 2024
48ecd28
Remove the second Container parameter from the HTTP Pipeline
roxblnfk Mar 11, 2024
90b366c
Improve exceptions when CookieQueue is resolved
roxblnfk Mar 12, 2024
0dbf436
Improve exceptions when SessionInterface is resolved
roxblnfk Mar 12, 2024
6d5422f
Cleanup; move `CookieQueue` into `http` scope
roxblnfk Mar 12, 2024
816620a
Add ServerRequestInterface binding
msmakouz Mar 13, 2024
466ffc5
Http Pipeline: fix CurrentRequest hydration
roxblnfk Mar 13, 2024
db49785
Http class: move event dispatcher into constructor
roxblnfk Mar 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/AuthHttp/src/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
final class AuthMiddleware implements MiddlewareInterface
{
public const ATTRIBUTE = 'authContext';
public const TOKEN_STORAGE_ATTRIBUTE = 'tokenStorage';

/**
* @param ScopeInterface $scope. Deprecated, will be removed in v4.0.
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
*/
public function __construct(
private readonly ScopeInterface $scope,
private readonly ActorProviderInterface $actorProvider,
private readonly TokenStorageInterface $tokenStorage,
private readonly TransportRegistry $transportRegistry,
private readonly ?EventDispatcherInterface $eventDispatcher = null
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {
}

Expand All @@ -39,12 +43,10 @@ public function process(Request $request, RequestHandlerInterface $handler): Res
{
$authContext = $this->initContext($request, new AuthContext($this->actorProvider, $this->eventDispatcher));

$response = $this->scope->runScope(
[
AuthContextInterface::class => $authContext,
TokenStorageInterface::class => $this->tokenStorage,
],
static fn () => $handler->handle($request->withAttribute(self::ATTRIBUTE, $authContext))
$response = $handler->handle(
$request
->withAttribute(self::ATTRIBUTE, $authContext)
->withAttribute(self::TOKEN_STORAGE_ATTRIBUTE, $this->tokenStorage),
);

return $this->closeContext($request, $response, $authContext);
Expand Down Expand Up @@ -85,15 +87,15 @@ private function closeContext(Request $request, Response $response, AuthContextI
return $transport->removeToken(
$request,
$response,
$authContext->getToken()->getID()
$authContext->getToken()->getID(),
);
}

return $transport->commitToken(
$request,
$response,
$authContext->getToken()->getID(),
$authContext->getToken()->getExpiresAt()
$authContext->getToken()->getExpiresAt(),
);
}
}
3 changes: 3 additions & 0 deletions src/AuthHttp/src/Middleware/AuthTransportMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class AuthTransportMiddleware implements MiddlewareInterface
{
private readonly AuthMiddleware $authMiddleware;

/**
* @param ScopeInterface $scope. Deprecated, will be removed in v4.0.
*/
public function __construct(
string $transportName,
ScopeInterface $scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class AuthTransportWithStorageMiddleware implements MiddlewareInterface
{
private readonly MiddlewareInterface $authMiddleware;

/**
* @param ScopeInterface $scope. Deprecated, will be removed in v4.0.
*/
public function __construct(
string $transportName,
ScopeInterface $scope,
Expand Down
69 changes: 38 additions & 31 deletions src/AuthHttp/tests/AuthTransportWithStorageMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Spiral\Tests\Auth;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -19,54 +17,63 @@
use Spiral\Auth\TransportRegistry;
use Spiral\Core\ScopeInterface;

final class AuthTransportWithStorageMiddlewareTest extends TestCase
final class AuthTransportWithStorageMiddlewareTest extends ScopedTestCase
{
public function testProcessMiddlewareWithTokenStorageProvider(): void
{
$serverRequest = m::mock(ServerRequestInterface::class);
$storageProvider = m::mock(TokenStorageProviderInterface::class);
$scope = m::mock(ScopeInterface::class);
$response = m::mock(ResponseInterface::class);
$storageProvider = $this->createMock(TokenStorageProviderInterface::class);
$storageProvider
->expects($this->once())
->method('getStorage')
->with('session')
->willReturn($tokenStorage = $this->createMock(TokenStorageInterface::class));

$storageProvider->shouldReceive('getStorage')->once()->with('session')->andReturn(
$tokenStorage = m::mock(TokenStorageInterface::class)
);
$matcher = $this->exactly(2);
$request = $this->createMock(ServerRequestInterface::class);
$request
->expects($this->exactly(2))
->method('withAttribute')
->willReturnCallback(function (string $key, string $value) use ($matcher, $tokenStorage) {
match ($matcher->numberOfInvocations()) {
1 => $this->assertInstanceOf(AuthContextInterface::class, $value),
2 => $this->assertSame($tokenStorage, $value),
};
})
->willReturnSelf();

$response = $this->createMock(ResponseInterface::class);

$registry = new TransportRegistry();
$registry->setTransport('header', $transport = m::mock(HttpTransportInterface::class));
$registry->setTransport('header', $transport = $this->createMock(HttpTransportInterface::class));

$transport->shouldReceive('fetchToken')->once()->with($serverRequest)->andReturn('fooToken');
$transport->shouldReceive('commitToken')->once()->with($serverRequest, $response, '123', null)
->andReturn($response);
$transport->expects($this->once())->method('fetchToken')->with($request)->willReturn('fooToken');
$transport->expects($this->once())->method('commitToken')->with($request, $response, '123', null)
->willReturn($response);

$tokenStorage->shouldReceive('load')->once()->with('fooToken')->andReturn(
$token = m::mock(TokenInterface::class)
);
$tokenStorage
->expects($this->once())
->method('load')
->with('fooToken')
->willReturn($token = $this->createMock(TokenInterface::class));

$scope
->shouldReceive('runScope')
->once()
->withArgs(
fn(array $bindings, callable $callback) => $bindings[AuthContextInterface::class]
->getToken() instanceof $token
)
->andReturn($response);

$token->shouldReceive('getID')->once()->andReturn('123');
$token->shouldReceive('getExpiresAt')->once()->andReturnNull();
$token->expects($this->once())->method('getID')->willReturn('123');
$token->expects($this->once())->method('getExpiresAt')->willReturn(null);

$middleware = new AuthTransportWithStorageMiddleware(
'header',
$scope,
m::mock(ActorProviderInterface::class),
$this->createMock(ScopeInterface::class),
$this->createMock(ActorProviderInterface::class),
$storageProvider,
$registry,
storage: 'session'
);

$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')->with($request)->willReturn($response);

$this->assertSame(
$response,
$middleware->process($serverRequest, m::mock(RequestHandlerInterface::class))
$middleware->process($request, $handler)
);
}
}
4 changes: 2 additions & 2 deletions src/AuthHttp/tests/CookieTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Spiral\Tests\Auth\Stub\TestAuthHttpStorage;
use Spiral\Tests\Auth\Stub\TestAuthHttpToken;

class CookieTransportTest extends BaseTestCase
final class CookieTransportTest extends ScopedTestCase
{
public function testCookieToken(): void
{
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function getCore(HttpTransportInterface $transport): Http

$http = new Http(
$config,
new Pipeline($this->container),
new Pipeline($this->container, $this->container),
new ResponseFactory($config),
$this->container
);
Expand Down
16 changes: 2 additions & 14 deletions src/AuthHttp/tests/HeaderTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,23 @@

namespace Spiral\Tests\Auth;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Auth\HttpTransportInterface;
use Spiral\Auth\Middleware\AuthMiddleware;
use Spiral\Auth\Transport\HeaderTransport;
use Spiral\Auth\TransportRegistry;
use Spiral\Core\Container;
use Spiral\Http\Config\HttpConfig;
use Spiral\Http\Http;
use Spiral\Http\Pipeline;
use Spiral\Telemetry\NullTracer;
use Spiral\Telemetry\TracerInterface;
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
use Nyholm\Psr7\ServerRequest;
use Spiral\Tests\Auth\Stub\TestAuthHttpProvider;
use Spiral\Tests\Auth\Stub\TestAuthHttpStorage;
use Spiral\Tests\Auth\Stub\TestAuthHttpToken;

class HeaderTransportTest extends TestCase
final class HeaderTransportTest extends ScopedTestCase
{
private Container $container;

public function setUp(): void
{
$this->container = new Container();
$this->container->bind(TracerInterface::class, new NullTracer($this->container));
}

public function testHeaderToken(): void
{
$http = $this->getCore(new HeaderTransport());
Expand Down Expand Up @@ -168,7 +156,7 @@ protected function getCore(HttpTransportInterface $transport): Http

$http = new Http(
$config,
new Pipeline($this->container),
new Pipeline($this->container, $this->container),
new ResponseFactory($config),
$this->container
);
Expand Down
6 changes: 3 additions & 3 deletions src/AuthHttp/tests/Middleware/AuthMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
use Spiral\Http\Config\HttpConfig;
use Spiral\Http\Http;
use Spiral\Http\Pipeline;
use Spiral\Tests\Auth\BaseTestCase;
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
use Nyholm\Psr7\ServerRequest;
use Spiral\Tests\Auth\ScopedTestCase;
use Spiral\Tests\Auth\Stub\TestAuthHttpProvider;
use Spiral\Tests\Auth\Stub\TestAuthHttpStorage;

class AuthMiddlewareTest extends BaseTestCase
final class AuthMiddlewareTest extends ScopedTestCase
{
public function testAttributeRead(): void
{
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function getCore(array $middleware = []): Http

return new Http(
$config,
new Pipeline($this->container),
new Pipeline($this->container, $this->container),
new ResponseFactory($config),
$this->container
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
use Spiral\Http\Config\HttpConfig;
use Spiral\Http\Http;
use Spiral\Http\Pipeline;
use Spiral\Tests\Auth\BaseTestCase;
use Spiral\Tests\Auth\Diactoros\ResponseFactory;
use Spiral\Tests\Auth\ScopedTestCase;
use Spiral\Tests\Auth\Stub\TestAuthHttpProvider;
use Spiral\Tests\Auth\Stub\TestAuthHttpStorage;

abstract class BaseFirewallTestCase extends BaseTestCase
abstract class BaseFirewallTestCase extends ScopedTestCase
{
protected function getCore(AbstractFirewall $firewall, HttpTransportInterface $transport): Http
{
Expand All @@ -30,7 +30,7 @@ protected function getCore(AbstractFirewall $firewall, HttpTransportInterface $t

$http = new Http(
$config,
new Pipeline($this->container),
new Pipeline($this->container, $this->container),
new ResponseFactory($config),
$this->container
);
Expand Down
19 changes: 19 additions & 0 deletions src/AuthHttp/tests/ScopedTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Auth;

use Spiral\Core\Container;
use Spiral\Core\Scope;

abstract class ScopedTestCase extends BaseTestCase
{
protected function runTest(): mixed
{
return $this->container->runScope(new Scope('http'), function (Container $container): mixed {
$this->container = $container;
return parent::runTest();
});
}
}
3 changes: 3 additions & 0 deletions src/Cookies/src/CookieQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Spiral\Cookies;

use Spiral\Core\Attribute\Scope;

#[Scope('http.request')]
final class CookieQueue
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
{
public const ATTRIBUTE = 'cookieQueue';
Expand Down
Loading