JwtDecoder::decodeJwt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/security-interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Security\Middleware;
10
11
use Daikon\Config\ConfigProviderInterface;
12
use Daikon\Interop\Assertion;
13
use Firebase\JWT\JWT;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use UnexpectedValueException;
19
20
final class JwtDecoder implements MiddlewareInterface
21
{
22
    public const DEFAULT_ATTR_JWT = '__Host-_jwt';
23
    public const DEFAULT_ATTR_XSRF = '__Host-_xsrf';
24
    public const DEFAULT_HEADER_XSRF = 'X-XSRF-TOKEN';
25
26
    private ConfigProviderInterface $config;
27
28 3
    public function __construct(ConfigProviderInterface $config)
29
    {
30 3
        $this->config = $config;
31 3
    }
32
33 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35 3
        $authConfig = $this->config->get('project.authentication', []);
36 3
        $jwtAttribute = $authConfig['cookies']['jwt']['attribute'] ?? self::DEFAULT_ATTR_JWT;
37 3
        $xsrfAttribute = $authConfig['cookies']['xsrf']['attribute'] ?? self::DEFAULT_ATTR_XSRF;
38 3
        $xsrfHeader = $authConfig['cookies']['xsrf']['header'] ?? self::DEFAULT_HEADER_XSRF;
39
40 3
        $cookieParams = $request->getCookieParams();
41 3
        $encodedJwt = $cookieParams[$jwtAttribute] ?? $this->parseAuthHeader($request->getHeaderLine('Authorization'));
42 3
        $xsrfToken = $cookieParams[$xsrfAttribute] ?? $request->getHeaderLine($xsrfHeader);
43
44 3
        $decodedJwt = $encodedJwt ? $this->decodeJwt($encodedJwt) : null;
45
46 3
        return $handler->handle(
47 3
            $request->withAttribute($jwtAttribute, $decodedJwt)->withAttribute($xsrfAttribute, $xsrfToken)
48
        );
49
    }
50
51 2
    private function decodeJwt(string $jwt): ?object
52
    {
53 2
        $secretKey = $this->config->get('project.authentication.cookies.jwt.secret');
54 2
        Assertion::notBlank($secretKey, 'A jwt secret encoding key is required.');
55
56
        try {
57 2
            return JWT::decode($jwt, $secretKey, ['HS256']);
58 1
        } catch (UnexpectedValueException $error) {
59 1
            return null;
60
        }
61
    }
62
63 3
    private function parseAuthHeader(string $header): ?string
64
    {
65 3
        return preg_match('/^Bearer\s+(?<token>[\w\.-]+)$/i', $header, $matches)
66 2
            ? $matches['token']
67 3
            : null;
68
    }
69
}
70