1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ncryptf\Tests; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use ncryptf\Token; |
7
|
|
|
use ncryptf\Request; |
8
|
|
|
use ncryptf\Authorization; |
9
|
|
|
use Middlewares\JsonPayload; |
|
|
|
|
10
|
|
|
use Middlewares\Utils\Factory; |
11
|
|
|
use WildWolf\Psr16MemoryCache; |
12
|
|
|
use ncryptf\Tests\AbstractTest; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
use Middlewares\Utils\Dispatcher; |
17
|
|
|
use ncryptf\Tests\mock\EncryptionKey; |
18
|
|
|
use ncryptf\middleware\RequestParser; |
19
|
|
|
|
20
|
|
|
use ncryptf\Tests\mock\Authentication; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
24
|
|
|
|
25
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
26
|
|
|
use Lcobucci\ContentNegotiation\Formatter\Json; |
27
|
|
|
use Lcobucci\ContentNegotiation\Formatter\StringCast; |
28
|
|
|
|
29
|
|
|
use Lcobucci\ContentNegotiation\ContentTypeMiddleware; |
30
|
|
|
|
31
|
|
|
final class AuthenticationTest extends AbstractTest |
32
|
|
|
{ |
33
|
|
|
public function testSuccessfulLogin() |
34
|
|
|
{ |
35
|
|
|
foreach ($this->testCases as $k => $params) { |
36
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
37
|
|
|
$response = Dispatcher::run( |
38
|
|
|
[ |
39
|
|
|
new Authentication, |
40
|
|
|
function ($request, $next) { |
41
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
42
|
|
|
$this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user'))); |
43
|
|
|
return $next->handle($request); |
44
|
|
|
} |
45
|
|
|
], |
46
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
47
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
48
|
|
|
->withHeader('Content-Type', 'application/json') |
49
|
|
|
->withHeader('Accept', 'application/json') |
50
|
|
|
->withBody((function () use ($params) { |
51
|
|
|
$stream = fopen('php://memory', 'r+'); |
52
|
|
|
fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]); |
|
|
|
|
53
|
|
|
rewind($stream); |
|
|
|
|
54
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
55
|
|
|
})()) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testV2EncryptedRequestWithPlaintextResponse() |
63
|
|
|
{ |
64
|
|
|
foreach ($this->testCases as $k => $params) { |
65
|
|
|
$serverKey = EncryptionKey::generate(); |
66
|
|
|
$myKey = EncryptionKey::generate(); |
67
|
|
|
$cache = Psr16MemoryCache::instance(); |
68
|
|
|
$cache->set($serverKey->getHashIdentifier(), $serverKey); |
69
|
|
|
|
70
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
71
|
|
|
|
72
|
|
|
$response = Dispatcher::run( |
73
|
|
|
[ |
74
|
|
|
new RequestParser($cache), |
75
|
|
|
new Authentication, |
76
|
|
|
function ($request, $next) { |
77
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
78
|
|
|
$this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user'))); |
79
|
|
|
return $next->handle($request); |
80
|
|
|
} |
81
|
|
|
], |
82
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
83
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
84
|
|
|
->withHeader('Content-Type', 'application/vnd.ncryptf+json') |
85
|
|
|
->withHeader('Accept', 'application/json') |
86
|
|
|
->withHeader('X-HashId', $serverKey->getHashIdentifier()) |
87
|
|
|
->withBody((function () use ($params, $serverKey, $myKey) { |
88
|
|
|
$data = \is_array($params[2]) ? \json_encode($params[2]): $params[2]; |
89
|
|
|
|
90
|
|
|
$request = new Request( |
91
|
|
|
$myKey->getBoxSecretKey(), |
92
|
|
|
$myKey->getSigningSecretKey() |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$encryptedData = $request->encrypt( |
96
|
|
|
$data, |
97
|
|
|
$serverKey->getBoxPublicKey() |
98
|
|
|
); |
99
|
|
|
$stream = fopen('php://memory', 'r+'); |
100
|
|
|
fwrite($stream, $data === '' ? '' : \base64_encode($encryptedData)); |
|
|
|
|
101
|
|
|
rewind($stream); |
|
|
|
|
102
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
103
|
|
|
})()) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testV1EncryptedRequestWithPlaintextResponse() |
111
|
|
|
{ |
112
|
|
|
foreach ($this->testCases as $k => $params) { |
113
|
|
|
$serverKey = EncryptionKey::generate(); |
114
|
|
|
$myKey = EncryptionKey::generate(); |
115
|
|
|
$nonce = \random_bytes(24); |
116
|
|
|
$cache = Psr16MemoryCache::instance(); |
117
|
|
|
$cache->set($serverKey->getHashIdentifier(), $serverKey); |
118
|
|
|
|
119
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
120
|
|
|
|
121
|
|
|
$response = Dispatcher::run( |
122
|
|
|
[ |
123
|
|
|
new RequestParser($cache), |
124
|
|
|
new Authentication, |
125
|
|
|
function ($request, $next) { |
126
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
127
|
|
|
$this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user'))); |
128
|
|
|
return $next->handle($request); |
129
|
|
|
} |
130
|
|
|
], |
131
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
132
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
133
|
|
|
->withHeader('Content-Type', 'application/vnd.25519+json') |
134
|
|
|
->withHeader('Accept', 'application/json') |
135
|
|
|
->withHeader('X-HashId', $serverKey->getHashIdentifier()) |
136
|
|
|
->withHeader('X-Nonce', \base64_encode($nonce)) |
137
|
|
|
->withHeader('X-PubKey', \base64_encode($myKey->getBoxPublicKey())) |
138
|
|
|
->withBody((function () use ($params, $serverKey, $myKey, $nonce) { |
139
|
|
|
$data = \is_array($params[2]) ? \json_encode($params[2]): $params[2]; |
140
|
|
|
|
141
|
|
|
$request = new Request( |
142
|
|
|
$myKey->getBoxSecretKey(), |
143
|
|
|
$myKey->getSigningSecretKey() |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$encryptedData = $request->encrypt( |
147
|
|
|
$data, |
148
|
|
|
$serverKey->getBoxPublicKey(), |
149
|
|
|
1, |
150
|
|
|
$nonce |
151
|
|
|
); |
152
|
|
|
$stream = fopen('php://memory', 'r+'); |
153
|
|
|
fwrite($stream, $data === '' ? '' : \base64_encode($encryptedData)); |
|
|
|
|
154
|
|
|
rewind($stream); |
|
|
|
|
155
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
156
|
|
|
})()) |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testError() |
164
|
|
|
{ |
165
|
|
|
$auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}'); |
166
|
|
|
$response = Dispatcher::run( |
167
|
|
|
[ |
168
|
|
|
new Authentication |
169
|
|
|
], |
170
|
|
|
Factory::createServerRequest('GET', '/api/v1/user/index') |
171
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
172
|
|
|
->withHeader('Content-Type', 'application/json') |
173
|
|
|
->withHeader('Accept', 'application/json') |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths