Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
HandleOptionalToken | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__invoke | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getOptionalTokenFromHeader | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | // TODO unit test |
5 | |
6 | use Psr\Http\Server\RequestHandlerInterface as RequestHandler; |
7 | use Slim\Http\ServerRequest as Request; |
8 | |
9 | class HandleOptionalToken { |
10 | protected array $requiredTypes = []; |
11 | |
12 | public function __construct(string ...$requiredTypes) { |
13 | $this->requiredTypes = $requiredTypes; |
14 | } |
15 | |
16 | function __invoke(Request $request, RequestHandler $handler) { |
17 | $tokenString = $this->getOptionalTokenFromHeader($request); |
18 | |
19 | if (is_null($tokenString)) { |
20 | return $handler->handle($request); |
21 | } |
22 | |
23 | $sessionDAO = new SessionDAO(); |
24 | $token = $sessionDAO->getToken($tokenString, $this->requiredTypes); |
25 | $request = $request->withAttribute('AuthToken', $token); |
26 | return $handler->handle($request); |
27 | } |
28 | |
29 | private function getOptionalTokenFromHeader(Request $request): ?string { |
30 | if (!$request->hasHeader('AuthToken')) { |
31 | return null; |
32 | } |
33 | |
34 | $authToken = $request->getHeaderLine('AuthToken'); |
35 | |
36 | if (!$authToken) { |
37 | return null; |
38 | } |
39 | |
40 | return $authToken; |
41 | } |
42 | } |