Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Oidc
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 readResponse
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Zmsclient\Auth;
11
12class Oidc extends BaseController
13{
14    /**
15     * @SuppressWarnings(Param)
16     * @return \Psr\Http\Message\ResponseInterface
17     */
18    public function readResponse(
19        \Psr\Http\Message\RequestInterface $request,
20        \Psr\Http\Message\ResponseInterface $response,
21        array $args
22    ) {
23        try {
24            $state = $request->getParam("state");
25            $authKey = Auth::getKey();
26            $sessionHash = hash('sha256', $authKey);
27
28            \App::$log->info('OIDC Login state validation', [
29                'event' => 'oauth_login_state_validation',
30                'timestamp' => date('c'),
31                'provider' => Auth::getOidcProvider(),
32                'application' => 'zmsadmin',
33                'state_match' => ($state == $authKey),
34                'hashed_session_token' => $sessionHash
35            ]);
36
37            if ($state == $authKey) {
38                try {
39                    $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
40                    $username = $workstation->getUseraccount()->id;
41                    $sessionHash = hash('sha256', $workstation->authkey);
42
43                    \App::$log->info('OIDC Login workstation access', [
44                        'event' => 'oauth_login_workstation_access',
45                        'timestamp' => date('c'),
46                        'provider' => Auth::getOidcProvider(),
47                        'application' => 'zmsadmin',
48                        'username' => $username,
49                        'workstation_id' => $workstation->id ?? 'unknown',
50                        'hashed_session_token' => $sessionHash
51                    ]);
52
53                    $departmentCount = $workstation->getUseraccount()->getDepartmentList()->count();
54
55                    \App::$log->info('OIDC Login department check', [
56                        'event' => 'oauth_login_department_check',
57                        'timestamp' => date('c'),
58                        'provider' => Auth::getOidcProvider(),
59                        'application' => 'zmsadmin',
60                        'username' => $username,
61                        'department_count' => $departmentCount,
62                        'has_departments' => ($departmentCount > 0),
63                        'hashed_session_token' => $sessionHash
64                    ]);
65
66                    if (0 == $departmentCount) {
67                        return \BO\Slim\Render::redirect(
68                            'index',
69                            [],
70                            [
71                                'oidclogin' => true
72                            ]
73                        );
74                    }
75                    return \BO\Slim\Render::redirect(
76                        'workstationSelect',
77                        [],
78                        []
79                    );
80                } catch (\Exception $e) {
81                    \App::$log->error('OIDC Login workstation error', [
82                        'event' => 'oauth_login_workstation_error',
83                        'timestamp' => date('c'),
84                        'provider' => Auth::getOidcProvider(),
85                        'application' => 'zmsadmin',
86                        'error' => $e->getMessage(),
87                        'code' => $e->getCode()
88                    ]);
89                    throw $e;
90                }
91            }
92
93            \App::$log->error('OIDC Login invalid state', [
94                'event' => 'oauth_login_invalid_state',
95                'timestamp' => date('c'),
96                'provider' => Auth::getOidcProvider(),
97                'application' => 'zmsadmin'
98            ]);
99
100            throw new \BO\Slim\Exception\OAuthInvalid();
101        } catch (\Exception $e) {
102            \App::$log->error('OIDC Login error', [
103                'event' => 'oauth_login_error',
104                'timestamp' => date('c'),
105                'provider' => Auth::getOidcProvider(),
106                'application' => 'zmsadmin',
107                'error' => $e->getMessage(),
108                'code' => $e->getCode()
109            ]);
110            throw $e;
111        }
112    }
113}