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