Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
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 / 70
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 / 70
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * @package Zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic;
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 = \BO\Zmsclient\Auth::getKey();
26
27            \App::$log->info('OIDC state validation', [
28                'event' => 'oauth_state_validation',
29                'timestamp' => date('c'),
30                'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
31                'application' => 'zmsstatistic',
32                'state_match' => ($state == $authKey)
33            ]);
34
35            if ($state == $authKey) {
36                try {
37                    $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
38                    $username = $workstation->getUseraccount()->id . '@' . \BO\Zmsclient\Auth::getOidcProvider();
39
40                    \App::$log->info('OIDC workstation access', [
41                        'event' => 'oauth_workstation_access',
42                        'timestamp' => date('c'),
43                        'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
44                        'application' => 'zmsstatistic',
45                        'username' => $username,
46                        'workstation_id' => $workstation->id ?? 'unknown'
47                    ]);
48
49                    $departmentCount = $workstation->getUseraccount()->getDepartmentList()->count();
50
51                    // Log department check with username
52                    \App::$log->info('OIDC department check', [
53                        'event' => 'oauth_department_check',
54                        'timestamp' => date('c'),
55                        'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
56                        'application' => 'zmsstatistic',
57                        'username' => $username,
58                        'department_count' => $departmentCount,
59                        'has_departments' => ($departmentCount > 0)
60                    ]);
61
62                    if (0 == $departmentCount) {
63                        return \BO\Slim\Render::redirect(
64                            'index',
65                            [],
66                            [
67                                'oidclogin' => true
68                            ]
69                        );
70                    }
71                    return \BO\Slim\Render::redirect(
72                        'workstationSelect',
73                        [],
74                        []
75                    );
76                } catch (\Exception $e) {
77                    \App::$log->error('OIDC workstation error', [
78                        'event' => 'oauth_workstation_error',
79                        'timestamp' => date('c'),
80                        'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
81                        'application' => 'zmsstatistic',
82                        'error' => $e->getMessage(),
83                        'code' => $e->getCode()
84                    ]);
85                    throw $e;
86                }
87            }
88
89            \App::$log->error('OIDC invalid state', [
90                'event' => 'oauth_invalid_state',
91                'timestamp' => date('c'),
92                'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
93                'application' => 'zmsstatistic'
94            ]);
95
96            throw new \BO\Slim\Exception\OAuthInvalid();
97        } catch (\Exception $e) {
98            \App::$log->error('OIDC error', [
99            'event' => 'oauth_error',
100            'timestamp' => date('c'),
101            'provider' => \BO\Zmsclient\Auth::getOidcProvider(),
102            'application' => 'zmsstatistic',
103            'error' => $e->getMessage(),
104            'code' => $e->getCode()
105            ]);
106            throw $e;
107        }
108    }
109}