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 | * @SuppressWarnings(Superglobals) |
| 16 | */ |
| 17 | public static function setKey($authKey, $expires = 0): void |
| 18 | { |
| 19 | $_COOKIE[self::getCookieName()] = $authKey; // for access in the same process |
| 20 | if (!headers_sent()) { |
| 21 | if (class_exists('App') && isset(App::$log)) { |
| 22 | $sessionHash = hash('sha256', $authKey); |
| 23 | App::$log->info('Auth session set', [ |
| 24 | 'event' => 'auth_session_set', |
| 25 | 'timestamp' => date('c'), |
| 26 | 'hashed_session_token' => $sessionHash, |
| 27 | 'expires' => date('Y-m-d H:i:s', $expires), |
| 28 | 'timezone' => date_default_timezone_get() |
| 29 | ]); |
| 30 | } |
| 31 | setcookie(self::getCookieName(), $authKey, $expires, '/', '', true, true); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @SuppressWarnings(Superglobals) |
| 37 | * |
| 38 | * @return null|string |
| 39 | */ |
| 40 | public static function getKey(): string|null |
| 41 | { |
| 42 | if (array_key_exists(self::getCookieName(), $_COOKIE)) { |
| 43 | return $_COOKIE[self::getCookieName()]; |
| 44 | } |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @SuppressWarnings(Superglobals) |
| 50 | */ |
| 51 | public static function removeKey(): void |
| 52 | { |
| 53 | if (array_key_exists(self::getCookieName(), $_COOKIE)) { |
| 54 | $oldKey = $_COOKIE[self::getCookieName()]; |
| 55 | if (class_exists('App') && isset(App::$log)) { |
| 56 | $sessionHash = hash('sha256', $oldKey); |
| 57 | App::$log->info('Auth session removed', [ |
| 58 | 'event' => 'auth_session_removed', |
| 59 | 'timestamp' => date('c'), |
| 60 | 'hashed_session_token' => $sessionHash |
| 61 | ]); |
| 62 | } |
| 63 | unset($_COOKIE[self::getCookieName()]); |
| 64 | if (!headers_sent()) { |
| 65 | setcookie(self::getCookieName(), '', time() - 3600, '/'); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public static function getCookieName() |
| 71 | { |
| 72 | return self::$cookieName; |
| 73 | } |
| 74 | |
| 75 | protected static function getOidcName(): string |
| 76 | { |
| 77 | return 'OIDC'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @SuppressWarnings(Superglobals) |
| 82 | */ |
| 83 | public static function setOidcProvider($provider): void |
| 84 | { |
| 85 | $_COOKIE[self::getOidcName()] = $provider; // for access in the same process |
| 86 | if (!headers_sent()) { |
| 87 | setcookie(self::getOidcName(), $provider, 0, '/', '', true, true); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @SuppressWarnings(Superglobals) |
| 93 | * |
| 94 | * @return false|string |
| 95 | */ |
| 96 | public static function getOidcProvider(): string|false |
| 97 | { |
| 98 | if (array_key_exists(self::getOidcName(), $_COOKIE)) { |
| 99 | return $_COOKIE[self::getOidcName()]; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @SuppressWarnings(Superglobals) |
| 106 | */ |
| 107 | public static function removeOidcProvider(): void |
| 108 | { |
| 109 | if (array_key_exists(self::getOidcName(), $_COOKIE)) { |
| 110 | unset($_COOKIE[self::getOidcName()]); |
| 111 | if (!headers_sent()) { |
| 112 | setcookie(self::getOidcName(), '', time() - 3600, '/'); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |