Lines
100.00%
30 / 30
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| WorkstationOAuth | 100.00% | 30 / 30 | 100.00% | 2 / 2 | 5 | 100.00% | 1 / 1 | |||
| readResponse | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 4 | |||||
| getLoggedInWorkstationByOidc | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Workstation\Api; | |
| 4 | ||
| 5 | use BO\Slim\Render; | |
| 6 | use BO\Mellon\Validator; | |
| 7 | use BO\Zmsentities\Useraccount as UseraccountEntity; | |
| 8 | ||
| 9 | /** | |
| 10 | * @SuppressWarnings(Coupling) | |
| 11 | */ | |
| 12 | class 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 | } |