Lines 100.00% 33 / 33
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 readResponse 100.00% 18 / 18 100.00% 1 / 1 4
 getLoggedInWorkstationByOidc 100.00% 12 / 12 100.00% 1 / 1 1
 [BO\Zmsbackend\Api\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
12class WorkstationOAuth extends \BO\Zmsbackend\Api\BaseController
13{
14    /**
15     * @SuppressWarnings(Param)
16     * @return \Psr\Http\Message\ResponseInterface
17     */
18    #[\Override]
19    public function readResponse(
20        \Psr\Http\Message\RequestInterface $request,
21        \Psr\Http\Message\ResponseInterface $response,
22        array $args
23    ) {
24        $validator = $request->getAttribute('validator');
25        $resolveReferences = $validator->getParameter('resolveReferences')->isNumber()->setDefault(2)->getValue();
26        $state  = $validator->getParameter('state')->isString()->isSmallerThan(40)->isBiggerThan(30)->getValue();
27        $input = Validator::input()->isJson()->assertValid()->getValue();
28        $entity = (new UseraccountEntity())->createFromOpenIdData($input);
29        $entity->testValid();
30
31        if (null === $state || $request->getHeaderLine('X-Authkey') !== $state) {
32            throw new \BO\Zmsbackend\Workstation\Exception\WorkstationAuthFailed();
33        }
34        \BO\Zmsbackend\Connection\Select::getWriteConnection();
35        if ((new \BO\Zmsbackend\Useraccount\Service\Useraccount())->readIsUserExisting($entity->getId())) {
36            $workstation = $this->getLoggedInWorkstationByOidc($request, $entity, $resolveReferences);
37        } else {
38            throw new \BO\Zmsbackend\Useraccount\Exception\UseraccountNotFound();
39        }
40        \BO\Zmsbackend\Connection\Select::writeCommit();
41
42        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
43        $message->data = $workstation;
44
45        $response = Render::withLastModified($response, time(), '0');
46        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
47        return $response;
48    }
49
50    protected function getLoggedInWorkstationByOidc(\Psr\Http\Message\RequestInterface $request, UseraccountEntity $entity, int $resolveReferences)
51    {
52        \BO\Zmsbackend\Helper\UserAuth::testUseraccountExists($entity->getId());
53
54        $workstation = (new \BO\Zmsbackend\Helper\User($request, $resolveReferences))->readWorkstation();
55        \BO\Zmsbackend\Helper\User::testWorkstationIsOveraged($workstation);
56
57        WorkstationLogin::testLoginHash($workstation);
58        $workstation = (new \BO\Zmsbackend\Workstation\Service\Workstation())->writeEntityLoginByOidc(
59            $entity->id,
60            $request->getHeaderLine('X-Authkey'),
61            \App::getNow(),
62            (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION),
63            $resolveReferences
64        );
65        return $workstation;
66    }
67}

Inherited from BO\Zmsbackend\Api\BaseController

20    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
21    {
22        $request = $this->initRequest($request);
23        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
24        return $this->readResponse($request, $noCacheResponse, $args);
25    }