Skip to content

Commit a229e6f

Browse files
committed
Enable apply middleware to get transport from value container
1 parent 9472d6e commit a229e6f

6 files changed

+203
-24
lines changed

composer.lock

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Middleware/ApplyAuthenticationMiddleware.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace ActiveCollab\Authentication\Middleware;
1010

1111
use ActiveCollab\Authentication\AuthenticationResult\Transport\TransportInterface;
12+
use ActiveCollab\Authentication\ValueContainer\RequestValueContainerInterface;
13+
use ActiveCollab\ValueContainer\ValueContainerInterface;
1214
use Psr\Http\Message\ResponseInterface;
1315
use Psr\Http\Message\ServerRequestInterface;
1416

@@ -18,22 +20,22 @@
1820
class ApplyAuthenticationMiddleware
1921
{
2022
/**
21-
* @var string
23+
* @var ValueContainerInterface
2224
*/
23-
private $request_attribute_name;
25+
private $value_container;
2426

2527
/**
2628
* @var bool
2729
*/
2830
private $apply_on_exit;
2931

3032
/**
31-
* @param string $request_attribute_name
32-
* @param bool $apply_on_exit
33+
* @param ValueContainerInterface $value_container
34+
* @param bool $apply_on_exit
3335
*/
34-
public function __construct($request_attribute_name = '', $apply_on_exit = false)
36+
public function __construct(ValueContainerInterface $value_container, $apply_on_exit = false)
3537
{
36-
$this->request_attribute_name = $request_attribute_name;
38+
$this->value_container = $value_container;
3739
$this->apply_on_exit = (bool) $apply_on_exit;
3840
}
3941

@@ -92,6 +94,10 @@ private function apply(ServerRequestInterface $request, ResponseInterface $respo
9294
*/
9395
protected function getTransportFrom(ServerRequestInterface $request)
9496
{
95-
return $this->request_attribute_name ? $request->getAttribute($this->request_attribute_name) : null;
97+
if ($this->value_container instanceof RequestValueContainerInterface) {
98+
$this->value_container->setRequest($request);
99+
}
100+
101+
return $this->value_container->getValue();
96102
}
97103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication\ValueContainer;
10+
11+
use LogicException;
12+
use Psr\Http\Message\ServerRequestInterface;
13+
14+
/**
15+
* @package ActiveCollab\Authentication\ValueContainer
16+
*/
17+
class RequestValueContainer implements RequestValueContainerInterface
18+
{
19+
/**
20+
* @var ServerRequestInterface
21+
*/
22+
private $request;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $attribute_name;
28+
29+
/**
30+
* RequestValueContainer constructor.
31+
*
32+
* @param string $attribute_name
33+
*/
34+
public function __construct($attribute_name)
35+
{
36+
$this->attribute_name = $attribute_name;
37+
}
38+
39+
/**
40+
* @return ServerRequestInterface
41+
*/
42+
public function getRequest()
43+
{
44+
return $this->request;
45+
}
46+
47+
/**
48+
* @param ServerRequestInterface $request
49+
* @return $this
50+
*/
51+
public function &setRequest(ServerRequestInterface $request)
52+
{
53+
$this->request = $request;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function hasValue()
62+
{
63+
if (!$this->getRequest()) {
64+
throw new LogicException('Request not set.');
65+
}
66+
67+
return array_key_exists($this->attribute_name, $this->getRequest()->getAttributes());
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getValue()
74+
{
75+
if (!$this->getRequest()) {
76+
throw new LogicException('Request not set.');
77+
}
78+
79+
return $this->request->getAttribute($this->attribute_name);
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication\ValueContainer;
10+
11+
use ActiveCollab\ValueContainer\ValueContainerInterface;
12+
use Psr\Http\Message\ServerRequestInterface;
13+
14+
/**
15+
* @package ActiveCollab\Authentication\ValueContainer
16+
*/
17+
interface RequestValueContainerInterface extends ValueContainerInterface
18+
{
19+
/**
20+
* @param ServerRequestInterface $request
21+
* @return $this
22+
*/
23+
public function &setRequest(ServerRequestInterface $request);
24+
}

test/src/ApplyAuthenticationMiddlewareTest.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use ActiveCollab\Authentication\Test\Session\Repository as SessionRepository;
1717
use ActiveCollab\Authentication\Test\Session\Session;
1818
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
19-
use ActiveCollab\Cookies\Adapter\Adapter;
19+
use ActiveCollab\Authentication\ValueContainer\RequestValueContainer;
2020
use ActiveCollab\Cookies\Cookies;
2121
use ActiveCollab\Cookies\CookiesInterface;
22+
use ActiveCollab\ValueContainer\ValueContainer;
23+
use ActiveCollab\ValueContainer\ValueContainerInterface;
24+
use Guzzle\Http\Message\Request;
2225
use Psr\Http\Message\ResponseInterface;
2326
use Psr\Http\Message\ServerRequestInterface;
2427

@@ -32,14 +35,20 @@ class ApplyAuthenticationMiddlewareTest extends RequestResponseTestCase
3235
*/
3336
private $cookies;
3437

38+
/**
39+
* @var ValueContainerInterface
40+
*/
41+
private $value_container;
42+
3543
/**
3644
* {@inheritdoc}
3745
*/
3846
public function setUp()
3947
{
4048
parent::setUp();
4149

42-
$this->cookies = new Cookies(new Adapter());
50+
$this->cookies = new Cookies();
51+
$this->value_container = new ValueContainer();
4352
}
4453

4554
/**
@@ -61,7 +70,7 @@ public function testUserIsAuthenticated()
6170
/** @var ServerRequestInterface $request */
6271
$request = $this->request->withAttribute('test_transport', new AuthorizationTransport($session_adapter, $user, $session, [1, 2, 3]));
6372

64-
$middleware = new ApplyAuthenticationMiddleware('test_transport');
73+
$middleware = new ApplyAuthenticationMiddleware(new RequestValueContainer('test_transport'));
6574
$this->assertFalse($middleware->applyOnExit());
6675

6776
/** @var ResponseInterface $response */
@@ -82,7 +91,7 @@ public function testUserIsAuthenticated()
8291
public function testNextIsCalled()
8392
{
8493
/** @var ResponseInterface $response */
85-
$response = call_user_func(new ApplyAuthenticationMiddleware('test_transport'), $this->request, $this->response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {
94+
$response = call_user_func(new ApplyAuthenticationMiddleware(new RequestValueContainer('test_transport')), $this->request, $this->response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {
8695
$response = $response->withHeader('X-Test', 'Yes, found!');
8796

8897
if ($next) {
@@ -115,7 +124,7 @@ public function testUserIsAuthentiatedOnExit()
115124
/** @var ServerRequestInterface $request */
116125
$request = $this->request->withAttribute('test_transport', new AuthorizationTransport($session_adapter, $user, $session, [1, 2, 3]));
117126

118-
$middleware = new ApplyAuthenticationMiddleware('test_transport', true);
127+
$middleware = new ApplyAuthenticationMiddleware(new RequestValueContainer('test_transport'), true);
119128
$this->assertTrue($middleware->applyOnExit());
120129

121130
/** @var ResponseInterface $response */
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication\Test;
10+
11+
use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase;
12+
use ActiveCollab\Authentication\ValueContainer\RequestValueContainer;
13+
14+
class RequestValueContainerTest extends RequestResponseTestCase
15+
{
16+
/**
17+
* @expectedException \LogicException
18+
* @expectedExceptionMessage Request not set.
19+
*/
20+
public function testHasValueWithNoRequest()
21+
{
22+
(new RequestValueContainer('test_key'))->hasValue();
23+
}
24+
25+
public function testHasValue()
26+
{
27+
$container = new RequestValueContainer('test_key');
28+
$container->setRequest($this->request);
29+
30+
$this->assertFalse($container->hasValue());
31+
32+
$this->request = $this->request->withAttribute('test_key', 123);
33+
$container->setRequest($this->request);
34+
35+
$this->assertTrue($container->hasValue());
36+
}
37+
38+
/**
39+
* @expectedException \LogicException
40+
* @expectedExceptionMessage Request not set.
41+
*/
42+
public function testGetValueWithNoRequest()
43+
{
44+
(new RequestValueContainer('test_key'))->getValue();
45+
}
46+
47+
public function testGetValue()
48+
{
49+
$container = new RequestValueContainer('test_key');
50+
$container->setRequest($this->request);
51+
52+
$this->assertNull($container->getValue());
53+
54+
$this->request = $this->request->withAttribute('test_key', 123);
55+
$container->setRequest($this->request);
56+
57+
$this->assertSame(123, $container->getValue());
58+
}
59+
}

0 commit comments

Comments
 (0)