Lines
100.00%
42 / 42
Functions and Methods
100.00%
8 / 8
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Auth | 100.00% | 42 / 42 | 100.00% | 8 / 8 | 23 | 100.00% | 1 / 1 | |||
| setKey | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 4 | |||||
| getKey | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| removeKey | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 6 | |||||
| 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% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| 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 string $cookieName = 'X-AuthKey'; | |
| 13 | ||
| 14 | /** | |
| 15 | * @SuppressWarnings(Superglobals) | |
| 16 | */ | |
| 17 | public static function setKey(string $authKey, int $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 | $cookieName = self::getCookieName(); | |
| 43 | if ($cookieName !== '' && array_key_exists($cookieName, $_COOKIE)) { | |
| 44 | return $_COOKIE[$cookieName]; | |
| 45 | } | |
| 46 | return null; | |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * @SuppressWarnings(Superglobals) | |
| 51 | */ | |
| 52 | public static function removeKey(): void | |
| 53 | { | |
| 54 | $cookieName = self::getCookieName(); | |
| 55 | if ($cookieName !== '' && array_key_exists($cookieName, $_COOKIE)) { | |
| 56 | $oldKey = $_COOKIE[$cookieName]; | |
| 57 | if (class_exists('App') && isset(App::$log)) { | |
| 58 | $sessionHash = hash('sha256', $oldKey); | |
| 59 | App::$log->info('Auth session removed', [ | |
| 60 | 'event' => 'auth_session_removed', | |
| 61 | 'timestamp' => date('c'), | |
| 62 | 'hashed_session_token' => $sessionHash | |
| 63 | ]); | |
| 64 | } | |
| 65 | unset($_COOKIE[$cookieName]); | |
| 66 | if (!headers_sent()) { | |
| 67 | setcookie($cookieName, '', time() - 3600, '/'); | |
| 68 | } | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | public static function getCookieName(): string | |
| 73 | { | |
| 74 | return self::$cookieName; | |
| 75 | } | |
| 76 | ||
| 77 | protected static function getOidcName(): string | |
| 78 | { | |
| 79 | return 'OIDC'; | |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * @SuppressWarnings(Superglobals) | |
| 84 | */ | |
| 85 | public static function setOidcProvider(string $provider): void | |
| 86 | { | |
| 87 | $_COOKIE[self::getOidcName()] = $provider; // for access in the same process | |
| 88 | if (!headers_sent()) { | |
| 89 | setcookie(self::getOidcName(), $provider, 0, '/', '', true, true); | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * @SuppressWarnings(Superglobals) | |
| 95 | * | |
| 96 | * @return false|string | |
| 97 | */ | |
| 98 | public static function getOidcProvider(): string|false | |
| 99 | { | |
| 100 | $cookieName = self::getOidcName(); | |
| 101 | if ($cookieName !== '' && array_key_exists($cookieName, $_COOKIE)) { | |
| 102 | return $_COOKIE[$cookieName]; | |
| 103 | } | |
| 104 | return false; | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * @SuppressWarnings(Superglobals) | |
| 109 | */ | |
| 110 | public static function removeOidcProvider(): void | |
| 111 | { | |
| 112 | if (array_key_exists(self::getOidcName(), $_COOKIE)) { | |
| 113 | unset($_COOKIE[self::getOidcName()]); | |
| 114 | if (!headers_sent()) { | |
| 115 | setcookie(self::getOidcName(), '', time() - 3600, '/'); | |
| 116 | } | |
| 117 | } | |
| 118 | } | |
| 119 | } |