Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.37% |
37 / 38 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SessionCookieService | |
97.37% |
37 / 38 |
|
80.00% |
4 / 5 |
13 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| initializeSessionCookie | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| init | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
4.00 | |||
| getSessionCookieParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCookieParams | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020-2022 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\session\Business\Service; |
| 26 | |
| 27 | use common_http_Request as Request; |
| 28 | use oat\tao\model\service\InjectionAwareService; |
| 29 | use oat\tao\model\session\Business\Contract\SessionCookieAttributesFactoryInterface; |
| 30 | use oat\tao\model\session\Business\Contract\SessionCookieServiceInterface; |
| 31 | use oat\tao\model\session\Business\Domain\SessionCookieAttribute; |
| 32 | use tao_helpers_Uri as UriHelper; |
| 33 | |
| 34 | class SessionCookieService extends InjectionAwareService implements SessionCookieServiceInterface |
| 35 | { |
| 36 | /** @var SessionCookieAttributesFactoryInterface */ |
| 37 | private $sessionCookieAttributesFactory; |
| 38 | private $sessionCookieParams = []; |
| 39 | |
| 40 | public function __construct(SessionCookieAttributesFactoryInterface $sessionCookieAttributesFactory) |
| 41 | { |
| 42 | parent::__construct(); |
| 43 | $this->sessionCookieAttributesFactory = $sessionCookieAttributesFactory; |
| 44 | } |
| 45 | |
| 46 | public function initializeSessionCookie(): void |
| 47 | { |
| 48 | //call it here not in the constructor, to keep original logic |
| 49 | //of unit test |
| 50 | $this->init(); |
| 51 | session_set_cookie_params($this->getSessionCookieParams()); |
| 52 | session_name(GENERIS_SESSION_NAME); |
| 53 | |
| 54 | if (isset($_COOKIE[GENERIS_SESSION_NAME])) { |
| 55 | // Resume the session |
| 56 | session_start(); |
| 57 | |
| 58 | //cookie keep alive, if lifetime is not 0 |
| 59 | if ($this->sessionCookieParams['lifetime'] !== 0) { |
| 60 | setcookie( |
| 61 | GENERIS_SESSION_NAME, |
| 62 | session_id(), |
| 63 | $this->getCookieParams() |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private function init(): void |
| 70 | { |
| 71 | $sessionCookieAttributeCollection = $this->sessionCookieAttributesFactory->create(); |
| 72 | |
| 73 | $sessionParams = session_get_cookie_params(); |
| 74 | $cookieDomain = UriHelper::isValidAsCookieDomain(ROOT_URL) |
| 75 | ? UriHelper::getDomain(ROOT_URL) |
| 76 | : $sessionParams['domain']; |
| 77 | $isSecureFlag = Request::isHttps(); |
| 78 | |
| 79 | if (isset($sessionParams['lifetime'])) { |
| 80 | $sessionCookieAttributeCollection = $sessionCookieAttributeCollection->add( |
| 81 | new SessionCookieAttribute('lifetime', $sessionParams['lifetime']) |
| 82 | ); |
| 83 | } |
| 84 | $sessionCookieAttributeCollection = $sessionCookieAttributeCollection |
| 85 | ->add(new SessionCookieAttribute('domain', $cookieDomain)) |
| 86 | ->add(new SessionCookieAttribute('secure', $isSecureFlag)) |
| 87 | ->add(new SessionCookieAttribute('httponly', true)); |
| 88 | |
| 89 | foreach ($sessionCookieAttributeCollection as $attribute) { |
| 90 | $this->sessionCookieParams[$attribute->getName()] = $attribute->getValue(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private function getSessionCookieParams(): array |
| 95 | { |
| 96 | return $this->sessionCookieParams; |
| 97 | } |
| 98 | |
| 99 | private function getCookieParams(): array |
| 100 | { |
| 101 | $cookieParams = []; |
| 102 | if ($this->sessionCookieParams['lifetime'] !== 0) { |
| 103 | $expires = $this->sessionCookieParams['lifetime'] + time(); |
| 104 | foreach ($this->sessionCookieParams as $key => $value) { |
| 105 | if ($key === 'lifetime') { |
| 106 | $cookieParams['expires'] = $expires; |
| 107 | } else { |
| 108 | $cookieParams[$key] = $value; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return $cookieParams; |
| 113 | } |
| 114 | } |