Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
WorkstationOAuth | |
0.00% |
0 / 47 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
readResponse | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
getLoggedInWorkstationByOidc | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
writeOAuthWorkstation | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 |
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 | $workstation = $this->writeOAuthWorkstation($entity, $state, $resolveReferences); |
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 | |
69 | protected function writeOAuthWorkstation(UseraccountEntity $entity, $state, $resolveReferences) |
70 | { |
71 | $useraccount = (new Useraccount())->writeEntity($entity); |
72 | $query = new Workstation(); |
73 | $workstation = $query->writeEntityLoginByName( |
74 | $useraccount->getId(), |
75 | $entity->password, |
76 | \App::getNow(), |
77 | (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION), |
78 | $resolveReferences |
79 | ); |
80 | $workstation = $query->updateEntityAuthkey( |
81 | $useraccount->getId(), |
82 | $entity->password, |
83 | $state, |
84 | (new \DateTime())->setTimestamp(time() + \App::SESSION_DURATION), |
85 | $resolveReferences |
86 | ); |
87 | return $workstation; |
88 | } |
89 | |
90 | /* |
91 | private function writeNewUseraccount(UseraccountEntity $entity, $resolveReferences) |
92 | { |
93 | Helper\User::checkRights('useraccount'); |
94 | Helper\User::testWorkstationAccessRights($entity); |
95 | $useraccount = (new Useraccount)->writeEntity($entity); |
96 | return $useraccount; |
97 | } |
98 | |
99 | private function loginSuperuser($resolveReferences){ |
100 | Helper\User::$workstation = (new Workstation)->writeEntityLoginByName( |
101 | \App::ZMS_AUTHORIZATION_SUPERUSER_USERNAME, |
102 | \App::ZMS_AUTHORIZATION_SUPERUSER_PASSWORD, |
103 | \App::getNow(), |
104 | $resolveReferences |
105 | ); |
106 | } |
107 | |
108 | private function logoutSuperuser(){ |
109 | (new Workstation)->writeEntityLogoutByName(\App::ZMS_AUTHORIZATION_SUPERUSER_USERNAME); |
110 | } |
111 | */ |
112 | } |