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