1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ncryptf\Tests; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use ncryptf\Authorization; |
7
|
|
|
use ncryptf\Token; |
8
|
|
|
use ncryptf\middleware\AbstractAuthentication; |
9
|
|
|
use ncryptf\Tests\AbstractTest; |
10
|
|
|
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
use Middlewares\Utils\Dispatcher; |
14
|
|
|
use Middlewares\Utils\Factory; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
18
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
19
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
20
|
|
|
|
21
|
|
|
final class MockAuthentication extends AbstractAuthentication |
22
|
|
|
{ |
23
|
|
|
protected function getTokenFromAccessToken(string $accessToken) :? Token |
24
|
|
|
{ |
25
|
|
|
// Return a fixed token |
26
|
|
|
return new Token( |
27
|
|
|
'x2gMeJ5Np0CcKpZav+i9iiXeQBtaYMQ/yeEtcOgY3J', |
28
|
|
|
'LRSEe5zHb1aq20Hr9te2sQF8sLReSkO8bS1eD/9LDM8', |
29
|
|
|
\base64_decode('f2mTaH9vkZZQyF7SxVeXDlOSDbVwjUzhdXv2T/YYO8k='), |
30
|
|
|
\base64_decode('waWBMawHD1zpAFRcX7e45L1aqsA3mEeSOwXqq4l1i3I='), |
31
|
|
|
\strtotime('+4 hours') |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getRequestBody(ServerRequestInterface $request) : string |
36
|
|
|
{ |
37
|
|
|
return $request->getBody()->getContents(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
final class AuthenticationTest extends AbstractTest |
42
|
|
|
{ |
43
|
|
|
public function testSuccessfulLogin() |
44
|
|
|
{ |
45
|
|
|
foreach ($this->testCases as $k => $params) { |
46
|
|
|
$auth = new Authorization($params[0], $params[1], $this->token, new DateTime, $params[2]); |
47
|
|
|
$response = Dispatcher::run([ |
48
|
|
|
new MockAuthentication, |
49
|
|
|
function ($request, $next) { |
50
|
|
|
$this->assertInstanceOf('\ncryptf\Token', $request->getAttribute('ncryptf-token')); |
51
|
|
|
return $next->handle($request); |
52
|
|
|
} |
53
|
|
|
], |
54
|
|
|
Factory::createServerRequest($params[0], $params[1]) |
55
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
56
|
|
|
->withBody((function() use ($params) { |
57
|
|
|
$stream = fopen('php://memory','r+'); |
58
|
|
|
fwrite($stream, \is_array($params[2]) ? \json_encode($params[2]): $params[2]); |
|
|
|
|
59
|
|
|
rewind($stream); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return new \Zend\Diactoros\Stream($stream); |
|
|
|
|
62
|
|
|
})()) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->assertSame(200, $response->getStatusCode()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testError() |
70
|
|
|
{ |
71
|
|
|
$auth = new Authorization('GET', '/api/v1/user/index', $this->token, new DateTime, '{"foo":"bar"}'); |
72
|
|
|
$response = Dispatcher::run([ |
73
|
|
|
new MockAuthentication |
74
|
|
|
], |
75
|
|
|
Factory::createServerRequest('GET', '/api/v1/user/index') |
76
|
|
|
->withHeader('Authorization', $auth->getHeader()) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertSame(401, $response->getStatusCode()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|