Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
36 / 37
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckAuthenticatedContextBuilder
97.30% covered (success)
97.30%
36 / 37
50.00% covered (danger)
50.00%
1 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
97.22% covered (success)
97.22%
35 / 36
0.00% covered (danger)
0.00%
0 / 1
11
1<?php
2namespace Apie\Common\ContextBuilders;
3
4use Apie\Common\ContextBuilders\Exceptions\WrongTokenException;
5use Apie\Common\Events\AddAuthenticationCookie;
6use Apie\Common\ValueObjects\DecryptedAuthenticatedUser;
7use Apie\Common\Wrappers\TextEncrypter;
8use Apie\Core\Context\ApieContext;
9use Apie\Core\ContextBuilders\ContextBuilderInterface;
10use Apie\Core\ContextConstants;
11use Apie\Core\Datalayers\ApieDatalayer;
12use Exception;
13use Psr\Http\Message\ServerRequestInterface;
14use Psr\Log\LoggerInterface;
15
16class CheckAuthenticatedContextBuilder implements ContextBuilderInterface
17{
18    public function __construct(
19        private readonly LoggerInterface $logger
20    ) {
21    }
22
23    public function process(ApieContext $context): ApieContext
24    {
25        if (!$context->hasContext(DecryptedAuthenticatedUser::class)) {
26            $textEncrypter = $context->getContext(TextEncrypter::class, false);
27            $request = $context->getContext(ServerRequestInterface::class, false);
28            $datalayer = $context->getContext(ApieDatalayer::class, false);
29            if ($textEncrypter instanceof TextEncrypter
30                && $request instanceof ServerRequestInterface
31                && $datalayer instanceof ApieDatalayer) {
32                $name = $request->getCookieParams()[AddAuthenticationCookie::COOKIE_NAME] ?? null;
33                $this->logger->debug($request->getUri()->__toString() . ' ' . ($name ?? 'no cookie'));
34                if ($name) {
35                    try {
36                        $decryptedUserId = new DecryptedAuthenticatedUser($textEncrypter->decrypt($name));
37                        if ($decryptedUserId->isExpired()) {
38                            throw new \LogicException('Token is expired!');
39                        }
40                        $authenticated = $datalayer->find(
41                            $decryptedUserId->getId(),
42                            $decryptedUserId->getBoundedContextId()
43                        );
44                        $context = $context
45                            ->withContext(ContextConstants::AUTHENTICATED_USER, $authenticated)
46                            ->registerInstance($authenticated)
47                            ->registerInstance($decryptedUserId);
48                    } catch (Exception $error) {
49                        $this->logger->error(
50                            'Error decrypting auth cookie: ' . $error->getMessage(),
51                            ['error' => $error]
52                        );
53
54                        throw new WrongTokenException($error);
55                    }
56                }
57            } else {
58                $this->logger->debug(
59                    sprintf(
60                        'Could not determine authentication cookie as a dependency is missing: encrypter: %s, request: %s, data layer: %s',
61                        $textEncrypter instanceof TextEncrypter ? 'true' : 'false',
62                        $request instanceof ServerRequestInterface ? 'true' : 'false',
63                        $datalayer instanceof ApieDatalayer ? 'true' : 'false'
64                    )
65                );
66            }
67        }
68        return $context;
69    }
70}