Lines 97.77% 44 / 45
Functions and Methods 88.88% 8 / 9
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Auth 97.77% 44 / 45 88.88% 8 / 9 25 0.00% 0 / 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
 singleLine 66.66% 2 / 3 0.00% 0 / 1 2.15
 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
3namespace BO\Zmsclient;
4
5use App;
6
7/**
8 * Session handler for mysql
9 */
10class 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(), self::singleLine($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    /**
78     * @psalm-taint-escape cookie
79     */
80    private static function singleLine(string $value): string
81    {
82        if (strpbrk($value, "\r\n\0") !== false) {
83            return '';
84        }
85
86        return $value;
87    }
88
89    protected static function getOidcName(): string
90    {
91        return 'OIDC';
92    }
93
94    /**
95     * @SuppressWarnings(Superglobals)
96     */
97    public static function setOidcProvider(string $provider): void
98    {
99        $_COOKIE[self::getOidcName()] = $provider; // for access in the same process
100        if (!headers_sent()) {
101            setcookie(self::getOidcName(), $provider, 0, '/', '', true, true);
102        }
103    }
104
105     /**
106     * @SuppressWarnings(Superglobals)
107     *
108     * @return false|string
109     */
110    public static function getOidcProvider(): string|false
111    {
112        $cookieName = self::getOidcName();
113        if ($cookieName !== '' && array_key_exists($cookieName, $_COOKIE)) {
114            return $_COOKIE[$cookieName];
115        }
116        return false;
117    }
118
119    /**
120     * @SuppressWarnings(Superglobals)
121     */
122    public static function removeOidcProvider(): void
123    {
124        if (array_key_exists(self::getOidcName(), $_COOKIE)) {
125            unset($_COOKIE[self::getOidcName()]);
126            if (!headers_sent()) {
127                setcookie(self::getOidcName(), '', time() - 3600, '/');
128            }
129        }
130    }
131}