|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Auth; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
17
|
|
|
use Spiral\Auth\HttpTransportInterface; |
|
18
|
|
|
use Spiral\Auth\Middleware\AuthMiddleware; |
|
19
|
|
|
use Spiral\Auth\Transport\HeaderTransport; |
|
20
|
|
|
use Spiral\Auth\TransportRegistry; |
|
21
|
|
|
use Spiral\Core\Container; |
|
22
|
|
|
use Spiral\Http\Config\HttpConfig; |
|
23
|
|
|
use Spiral\Http\Http; |
|
24
|
|
|
use Spiral\Http\Pipeline; |
|
25
|
|
|
use Spiral\Tests\Auth\Diactoros\ResponseFactory; |
|
26
|
|
|
use Laminas\Diactoros\ServerRequest; |
|
27
|
|
|
use Spiral\Tests\Auth\Stub\TestProvider; |
|
|
|
|
|
|
28
|
|
|
use Spiral\Tests\Auth\Stub\TestStorage; |
|
29
|
|
|
use Spiral\Tests\Auth\Stub\TestToken; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class HeaderTransportTest extends TestCase |
|
32
|
|
|
{ |
|
33
|
|
|
private $container; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->container = new Container(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testHeaderToken(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$http = $this->getCore(new HeaderTransport()); |
|
43
|
|
|
|
|
44
|
|
|
$http->setHandler( |
|
45
|
|
|
static function (ServerRequestInterface $request, ResponseInterface $response): void { |
|
|
|
|
|
|
46
|
|
|
if ($request->getAttribute('authContext')->getToken() === null) { |
|
47
|
|
|
echo 'no token'; |
|
48
|
|
|
} else { |
|
49
|
|
|
echo $request->getAttribute('authContext')->getToken()->getID(); |
|
50
|
|
|
echo ':'; |
|
51
|
|
|
echo json_encode($request->getAttribute('authContext')->getToken()->getPayload()); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [ |
|
57
|
|
|
'X-Auth-Token' => 'good-token' |
|
58
|
|
|
])); |
|
59
|
|
|
|
|
60
|
|
|
self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type')); |
|
61
|
|
|
self::assertSame('good-token:{"id":"good-token"}', (string)$response->getBody()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testBadHeaderToken(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$http = $this->getCore(new HeaderTransport()); |
|
67
|
|
|
|
|
68
|
|
|
$http->setHandler( |
|
69
|
|
|
static function (ServerRequestInterface $request, ResponseInterface $response): void { |
|
|
|
|
|
|
70
|
|
|
if ($request->getAttribute('authContext')->getToken() === null) { |
|
71
|
|
|
echo 'no token'; |
|
72
|
|
|
} else { |
|
73
|
|
|
echo $request->getAttribute('authContext')->getToken()->getID(); |
|
74
|
|
|
echo ':'; |
|
75
|
|
|
echo json_encode($request->getAttribute('authContext')->getToken()->getPayload()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [ |
|
81
|
|
|
'X-Auth-Token' => 'bad' |
|
82
|
|
|
])); |
|
83
|
|
|
|
|
84
|
|
|
self::assertSame(['text/html; charset=UTF-8'], $response->getHeader('Content-Type')); |
|
85
|
|
|
self::assertSame('no token', (string)$response->getBody()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testDeleteToken(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$http = $this->getCore(new HeaderTransport()); |
|
91
|
|
|
|
|
92
|
|
|
$http->setHandler( |
|
93
|
|
|
static function (ServerRequestInterface $request, ResponseInterface $response): void { |
|
|
|
|
|
|
94
|
|
|
$request->getAttribute('authContext')->close(); |
|
95
|
|
|
echo 'closed'; |
|
96
|
|
|
} |
|
97
|
|
|
); |
|
98
|
|
|
$response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [ |
|
99
|
|
|
'X-Auth-Token' => 'bad' |
|
100
|
|
|
])); |
|
101
|
|
|
|
|
102
|
|
|
self::assertEmpty($response->getHeader('X-Auth-Token')); |
|
103
|
|
|
self::assertSame('closed', (string)$response->getBody()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testCommitToken(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$http = $this->getCore(new HeaderTransport()); |
|
109
|
|
|
|
|
110
|
|
|
$http->setHandler( |
|
111
|
|
|
static function (ServerRequestInterface $request, ResponseInterface $response): void { |
|
|
|
|
|
|
112
|
|
|
$request->getAttribute('authContext')->start( |
|
113
|
|
|
new TestToken('new-token', ['ok' => 1]) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$response = $http->handle(new ServerRequest([], [], null, 'GET', 'php://input', [])); |
|
119
|
|
|
|
|
120
|
|
|
self::assertSame(['new-token'], $response->getHeader('X-Auth-Token')); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function getCore(HttpTransportInterface $transport): Http |
|
124
|
|
|
{ |
|
125
|
|
|
$config = new HttpConfig([ |
|
126
|
|
|
'basePath' => '/', |
|
127
|
|
|
'headers' => [ |
|
128
|
|
|
'Content-Type' => 'text/html; charset=UTF-8' |
|
129
|
|
|
], |
|
130
|
|
|
'middleware' => [], |
|
131
|
|
|
]); |
|
132
|
|
|
|
|
133
|
|
|
$http = new Http( |
|
134
|
|
|
$config, |
|
135
|
|
|
new Pipeline($this->container), |
|
136
|
|
|
new ResponseFactory($config), |
|
137
|
|
|
$this->container |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$http->getPipeline()->pushMiddleware( |
|
141
|
|
|
new AuthMiddleware( |
|
142
|
|
|
$this->container, |
|
143
|
|
|
new TestProvider(), |
|
144
|
|
|
new TestStorage(), |
|
145
|
|
|
$reg = new TransportRegistry() |
|
146
|
|
|
) |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$reg->setDefaultTransport('transport'); |
|
150
|
|
|
$reg->setTransport('transport', $transport); |
|
151
|
|
|
|
|
152
|
|
|
return $http; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: