1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Server\Tests; |
38
|
|
|
|
39
|
|
|
use Apparat\Dev\Tests\AbstractTest; |
40
|
|
|
use Apparat\Server\Ports\Authenticator\Bearer; |
41
|
|
|
use Apparat\Server\Ports\Facade\ServerFacade; |
42
|
|
|
use Apparat\Server\Ports\Route\RouteFactory; |
43
|
|
|
use Apparat\Server\Ports\View\TYPO3FluidView; |
44
|
|
|
use Psr\Http\Message\ResponseInterface; |
45
|
|
|
use Zend\Diactoros\ServerRequest; |
46
|
|
|
use Zend\Diactoros\Uri; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Authenticator test |
50
|
|
|
* |
51
|
|
|
* @package Apparat\Server |
52
|
|
|
* @subpackage Apparat\Server\Tests |
53
|
|
|
*/ |
54
|
|
|
class AuthenticatorTest extends AbstractTest |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* Test an invalid authenticator |
58
|
|
|
* |
59
|
|
|
* @expectedException \Apparat\Server\Ports\Authenticator\InvalidArgumentException |
60
|
|
|
* @expectedExceptionCode 1471206157 |
61
|
|
|
*/ |
62
|
|
|
public static function testInvalidAuthenticator() |
63
|
|
|
{ |
64
|
|
|
// Register a static route and add the bearer token authenticator |
65
|
|
|
$bearerRoute = RouteFactory::createStaticRoute('/bearer', 'Test/Bearer'); |
66
|
|
|
$bearerRoute->setAuth([new \stdClass()]); |
67
|
|
|
ServerFacade::registerRoute($bearerRoute); |
68
|
|
|
|
69
|
|
|
// Test authorization header |
70
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
71
|
|
|
$request = new ServerRequest(); |
72
|
|
|
$request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer'); |
73
|
|
|
ServerFacade::dispatchRequest($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Tears down the fixture |
78
|
|
|
*/ |
79
|
|
|
public function tearDown() |
80
|
|
|
{ |
81
|
|
|
parent::tearDown(); |
82
|
|
|
|
83
|
|
|
ServerFacade::reset(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test the bearer token |
88
|
|
|
*/ |
89
|
|
|
public function testBearerToken() |
90
|
|
|
{ |
91
|
|
|
// Register custom view resources |
92
|
|
|
$noneRepoPath = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR; |
93
|
|
|
ServerFacade::setViewResources([ |
94
|
|
|
TYPO3FluidView::LAYOUTS => $noneRepoPath.'Layouts'.DIRECTORY_SEPARATOR, |
95
|
|
|
TYPO3FluidView::TEMPLATES => $noneRepoPath.'Templates'.DIRECTORY_SEPARATOR, |
96
|
|
|
TYPO3FluidView::PARTIALS => $noneRepoPath.'Partials'.DIRECTORY_SEPARATOR, |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$bearerToken = md5(microtime(true)); |
100
|
|
|
$bearerAuthenticator = new Bearer(function ($currentToken) use ($bearerToken) { |
101
|
|
|
return $currentToken === $bearerToken; |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
// Register a static route and add the bearer token authenticator |
105
|
|
|
$bearerRoute = RouteFactory::createStaticRoute('/bearer', 'Test/Bearer'); |
106
|
|
|
$bearerRoute->setAuth([$bearerAuthenticator]); |
107
|
|
|
ServerFacade::registerRoute($bearerRoute); |
108
|
|
|
|
109
|
|
|
// Test authorization header |
110
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
111
|
|
|
$request = new ServerRequest(); |
112
|
|
|
$request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer '.$bearerToken); |
113
|
|
|
$response = ServerFacade::dispatchRequest($request); |
114
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
115
|
|
|
$this->assertEquals('[(bearer)]', trim($response->getBody())); |
116
|
|
|
|
117
|
|
|
// Test "access_token" body parameter |
118
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
119
|
|
|
$request = new ServerRequest(); |
120
|
|
|
$request = $request->withUri($uri)->withParsedBody(['access_token' => $bearerToken]); |
121
|
|
|
$response = ServerFacade::dispatchRequest($request); |
122
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
123
|
|
|
$this->assertEquals('[(bearer)]', trim($response->getBody())); |
124
|
|
|
|
125
|
|
|
// Test "access_token" query parameter |
126
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
127
|
|
|
$request = new ServerRequest(); |
128
|
|
|
$request = $request->withUri($uri)->withQueryParams(['access_token' => $bearerToken]); |
129
|
|
|
$response = ServerFacade::dispatchRequest($request); |
130
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
131
|
|
|
$this->assertEquals('[(bearer)]', trim($response->getBody())); |
132
|
|
|
|
133
|
|
|
// Test invalid authorization header |
134
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
135
|
|
|
$request = new ServerRequest(); |
136
|
|
|
$request = $request->withUri($uri)->withAddedHeader('Authorization', 'Bearer'); |
137
|
|
|
$response = ServerFacade::dispatchRequest($request); |
138
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
139
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
140
|
|
|
|
141
|
|
|
// Test with basic authorization header |
142
|
|
|
$uri = new Uri('http://apparat/blog/bearer'); |
143
|
|
|
$request = new ServerRequest(); |
144
|
|
|
$request = $request->withUri($uri)->withAddedHeader('Authorization', 'Basic '.base64_encode("user:pass")); |
145
|
|
|
$response = ServerFacade::dispatchRequest($request); |
146
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
147
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|