1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ncryptf\Tests; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use ncryptf\Authorization; |
7
|
|
|
use ncryptf\Request; |
8
|
|
|
use ncryptf\Token; |
9
|
|
|
use ncryptf\middleware\NcryptfPayload; |
10
|
|
|
use ncryptf\Tests\AbstractTest; |
11
|
|
|
use ncryptf\Tests\mock\Authentication; |
12
|
|
|
use ncryptf\Tests\mock\EncryptionKey; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
use Middlewares\JsonPayload; |
17
|
|
|
use Middlewares\Utils\Dispatcher; |
18
|
|
|
use Middlewares\Utils\Factory; |
19
|
|
|
|
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
23
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
24
|
|
|
|
25
|
|
|
use WildWolf\Psr16MemoryCache; |
26
|
|
|
|
27
|
|
|
final class AuthenticationTest extends AbstractTest |
28
|
|
|
{ |
29
|
|
|
public function testSuccessfulLogin() |
30
|
|
|
{ |
31
|
|
|
foreach ($this->testCases as $k => $params) { |
32
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
33
|
|
|
$response = Dispatcher::run( |
34
|
|
|
[ |
35
|
|
|
new Authentication, |
36
|
|
|
function ($request, $next) { |
37
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
38
|
|
|
$this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user'))); |
39
|
|
|
return $next->handle($request); |
40
|
|
|
} |
41
|
|
|
], |
42
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
43
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
44
|
|
|
->withHeader('Content-Type', 'application/json') |
45
|
|
|
->withHeader('Accept', 'application/json') |
46
|
|
|
->withBody((function () use ($params) { |
47
|
|
|
$stream = fopen('php://memory', 'r+'); |
48
|
|
|
fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]); |
|
|
|
|
49
|
|
|
rewind($stream); |
|
|
|
|
50
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
51
|
|
|
})()) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testEncryptedRequestWithPlaintextResponse() |
59
|
|
|
{ |
60
|
|
|
foreach ($this->testCases as $k => $params) { |
61
|
|
|
$serverKey = EncryptionKey::generate(); |
62
|
|
|
$myKey = EncryptionKey::generate(); |
63
|
|
|
$cache = Psr16MemoryCache::instance(); |
64
|
|
|
$cache->set($serverKey->getHashIdentifier(), $serverKey); |
65
|
|
|
|
66
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
67
|
|
|
|
68
|
|
|
$response = Dispatcher::run( |
69
|
|
|
[ |
70
|
|
|
new NcryptfPayload($cache), |
71
|
|
|
new Authentication, |
72
|
|
|
function ($request, $next) { |
73
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
74
|
|
|
$this->assertEquals(true, \is_array($request->getAttribute('ncryptf-user'))); |
75
|
|
|
return $next->handle($request); |
76
|
|
|
} |
77
|
|
|
], |
78
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
79
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
80
|
|
|
->withHeader('Content-Type', 'application/vnd.ncryptf+json') |
81
|
|
|
->withHeader('Accept', 'application/json') |
82
|
|
|
->withHeader('X-HashId', $serverKey->getHashIdentifier()) |
83
|
|
|
->withBody((function () use ($params, $serverKey, $myKey) { |
84
|
|
|
$data = \is_array($params[2]) ? \json_encode($params[2]): $params[2]; |
85
|
|
|
|
86
|
|
|
$request = new Request( |
87
|
|
|
$myKey->getBoxSecretKey(), |
88
|
|
|
$myKey->getSigningSecretKey() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$encryptedData = $request->encrypt( |
92
|
|
|
$data, |
93
|
|
|
$serverKey->getBoxPublicKey() |
94
|
|
|
); |
95
|
|
|
$stream = fopen('php://memory', 'r+'); |
96
|
|
|
fwrite($stream, $data === '' ? '' : \base64_encode($encryptedData)); |
|
|
|
|
97
|
|
|
rewind($stream); |
|
|
|
|
98
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
99
|
|
|
})()) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testError() |
107
|
|
|
{ |
108
|
|
|
$auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}'); |
109
|
|
|
$response = Dispatcher::run( |
110
|
|
|
[ |
111
|
|
|
new Authentication |
112
|
|
|
], |
113
|
|
|
Factory::createServerRequest('GET', '/api/v1/user/index') |
114
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
115
|
|
|
->withHeader('Content-Type', 'application/json') |
116
|
|
|
->withHeader('Accept', 'application/json') |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|