Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
Auth | |
100.00% |
39 / 39 |
|
100.00% |
8 / 8 |
20 | |
100.00% |
1 / 1 |
setKey | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
getKey | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
removeKey | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
getCookieName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOidcName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOidcProvider | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getOidcProvider | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
removeOidcProvider | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace BO\Zmsclient; |
4 | |
5 | use App; |
6 | |
7 | /** |
8 | * Session handler for mysql |
9 | */ |
10 | class Auth |
11 | { |
12 | private static $cookieName = 'X-AuthKey'; |
13 | |
14 | /** |
15 | * |
16 | * @SuppressWarnings(Superglobals) |
17 | * |
18 | */ |
19 | public static function setKey($authKey, $expires = 0) |
20 | { |
21 | $_COOKIE[self::getCookieName()] = $authKey; // for access in the same process |
22 | if (!headers_sent()) { |
23 | if (class_exists('App') && isset(App::$log)) { |
24 | $sessionHash = hash('sha256', $authKey); |
25 | App::$log->info('Auth session set', [ |
26 | 'event' => 'auth_session_set', |
27 | 'timestamp' => date('c'), |
28 | 'hashed_session_token' => $sessionHash, |
29 | 'expires' => date('Y-m-d H:i:s', $expires), |
30 | 'timezone' => date_default_timezone_get() |
31 | ]); |
32 | } |
33 | setcookie(self::getCookieName(), $authKey, $expires, '/', '', true, true); |
34 | } |
35 | } |
36 | |
37 | /** |
38 | * |
39 | * @SuppressWarnings(Superglobals) |
40 | * |
41 | */ |
42 | public static function getKey() |
43 | { |
44 | if (array_key_exists(self::getCookieName(), $_COOKIE)) { |
45 | return $_COOKIE[self::getCookieName()]; |
46 | } |
47 | return null; |
48 | } |
49 | |
50 | /** |
51 | * |
52 | * @SuppressWarnings(Superglobals) |
53 | * |
54 | */ |
55 | public static function removeKey() |
56 | { |
57 | if (array_key_exists(self::getCookieName(), $_COOKIE)) { |
58 | $oldKey = $_COOKIE[self::getCookieName()]; |
59 | if (class_exists('App') && isset(App::$log)) { |
60 | $sessionHash = hash('sha256', $oldKey); |
61 | App::$log->info('Auth session removed', [ |
62 | 'event' => 'auth_session_removed', |
63 | 'timestamp' => date('c'), |
64 | 'hashed_session_token' => $sessionHash |
65 | ]); |
66 | } |
67 | unset($_COOKIE[self::getCookieName()]); |
68 | if (!headers_sent()) { |
69 | setcookie(self::getCookieName(), '', time() - 3600, '/'); |
70 | } |
71 | } |
72 | } |
73 | |
74 | public static function getCookieName() |
75 | { |
76 | return self::$cookieName; |
77 | } |
78 | |
79 | protected static function getOidcName() |
80 | { |
81 | return 'OIDC'; |
82 | } |
83 | |
84 | /** |
85 | * |
86 | * @SuppressWarnings(Superglobals) |
87 | * |
88 | */ |
89 | public static function setOidcProvider($provider) |
90 | { |
91 | $_COOKIE[self::getOidcName()] = $provider; // for access in the same process |
92 | if (!headers_sent()) { |
93 | setcookie(self::getOidcName(), $provider, 0, '/', '', true, true); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * |
99 | * @SuppressWarnings(Superglobals) |
100 | * |
101 | */ |
102 | public static function getOidcProvider() |
103 | { |
104 | if (array_key_exists(self::getOidcName(), $_COOKIE)) { |
105 | return $_COOKIE[self::getOidcName()]; |
106 | } |
107 | return false; |
108 | } |
109 | |
110 | /** |
111 | * |
112 | * @SuppressWarnings(Superglobals) |
113 | * |
114 | */ |
115 | public static function removeOidcProvider() |
116 | { |
117 | if (array_key_exists(self::getOidcName(), $_COOKIE)) { |
118 | unset($_COOKIE[self::getOidcName()]); |
119 | if (!headers_sent()) { |
120 | setcookie(self::getOidcName(), '', time() - 3600, '/'); |
121 | } |
122 | } |
123 | } |
124 | } |