Lines 95.65% 22 / 23
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 createCurrentSessionKey 100.00% 1 / 1 100.00% 1 / 1 1
 getCurrentSessionKey 100.00% 2 / 2 100.00% 1 / 1 2
 setCurrentSessionKey 75.00% 3 / 4 0.00% 0 / 1 2.06
 setCookie 100.00% 10 / 10 100.00% 1 / 1 3
 getCookie 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Session\AbstractSession] get 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Session\AbstractSession] set 100.00% 1 / 1 100.00% 1 / 1 1
29class EntraIdSession extends AbstractSession
30{
31    /** @var string EntraID session key */
32    final public const string ENTRA_ID_SESSION_KEY = 'pmf-entra-id-session-key';
33
34    final public const string ENTRA_ID_OAUTH_VERIFIER = 'pmf-entra-id-oauth-verifier';
35
36    final public const string ENTRA_ID_JWT = 'pmf-entra-id-jwt';
37
38    private string $currentSessionKey;
39
40    public function __construct(
41        private readonly Configuration $configuration,
42        private readonly Session $session,
43    ) {
44        parent::__construct($session);
45
46        $this->createCurrentSessionKey();
47    }
48
49    /**
50     * Creates the current UUID session key
51     */
52    public function createCurrentSessionKey(): void
53    {
54        $this->currentSessionKey = Uuid::v4()->toRfc4122();
55    }
56
57    /**
58     * Returns the current UUID session key
59     */
60    public function getCurrentSessionKey(): ?string
61    {
62        $sessionKey = $this->currentSessionKey ?? $this->session->get(self::ENTRA_ID_SESSION_KEY);
63
64        return $sessionKey === null ? null : (string) $sessionKey;
65    }
66
67    /**
68     * Sets the current UUID session key
69     *
70     * @throws Exception
71     */
72    public function setCurrentSessionKey(): EntraIdSession
73    {
74        /* @mago-expect lint:no-isset - typed property may be uninitialized */
75        if (!isset($this->currentSessionKey)) {
76            $this->createCurrentSessionKey();
77        }
78
79        $this->session->set(self::ENTRA_ID_SESSION_KEY, $this->currentSessionKey);
80
81        return $this;
82    }
83
84    /**
85     * Store the Session ID into a persistent cookie expiring
86     * 3600 seconds after the page request.
87     *
88     * @param string          $name Cookie name
89     * @param int|string|null $sessionId Session ID
90     * @param int             $timeout Cookie timeout
91     */
92    public function setCookie(string $name, int|string|null $sessionId, int $timeout = 3600, bool $strict = true): void
93    {
94        $request = Request::createFromGlobals();
95
96        $cookieDomain = parse_url($this->configuration->getDefaultUrl(), PHP_URL_HOST);
97
98        setcookie($name, (string) ($sessionId ?? ''), [
99            'expires' => (int) $request->server->get('REQUEST_TIME') + $timeout,
100            'path' => dirname((string) $request->server->get('SCRIPT_NAME')),
101            'domain' => is_string($cookieDomain) ? $cookieDomain : '',
102            'secure' => $request->isSecure(),
103            'httponly' => true,
104            // 'lax' so the cookie survives the top-level redirect back from login.microsoftonline.com
105            'samesite' => $strict ? 'strict' : 'lax',
106        ]);
107    }
108
109    /**
110     * Returns the value of a cookie.
111     *
112     * @param string $name Cookie name
113     */
114    public function getCookie(string $name): string
115    {
116        $request = Request::createFromGlobals();
117        return $request->cookies->get($name, '');
118    }
119}

Inherited from phpMyFAQ\Session\AbstractSession

31    public function get(string $key): mixed
32    {
33        return $this->session->get($key);
34    }
36    public function set(string $key, mixed $value): void
37    {
38        $this->session->set($key, $value);
39    }