SaveHandler
1 year ago
SessionAuth.php
1 year ago
SessionFingerprint.php
1 year ago
SessionInitializer.php
1 year ago
SessionNamespace.php
1 year ago
SessionFingerprint.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Session; |
| 10 | |
| 11 | use Piwik\Config; |
| 12 | use Piwik\Date; |
| 13 | /** |
| 14 | * Manages session information that is used to identify who the session |
| 15 | * is for. |
| 16 | * |
| 17 | * Once a session is authenticated using either a user name & password or |
| 18 | * token auth, some information about the user is stored in the session. |
| 19 | * This info includes the user name and the user agent |
| 20 | * string of the user's client, and a random session secret. |
| 21 | * |
| 22 | * In subsequent requests that use this session, we use the above information |
| 23 | * to verify that the session is allowed to be used by the person sending the |
| 24 | * request. |
| 25 | * |
| 26 | * This is accomplished by checking the request's user agent |
| 27 | * against what is stored in the session. If it doesn't then this is a |
| 28 | * session hijacking attempt. |
| 29 | * |
| 30 | * We also check that a hash in the matomo_auth cookie matches the hash |
| 31 | * of the time the user last changed their password + the session secret. |
| 32 | * If they don't match, the password has been changed since this session |
| 33 | * started, and is no longer valid. |
| 34 | */ |
| 35 | class SessionFingerprint |
| 36 | { |
| 37 | // used in case the global.ini.php becomes corrupt or doesn't update properly |
| 38 | public const DEFAULT_IDLE_TIMEOUT = 3600; |
| 39 | public const USER_NAME_SESSION_VAR_NAME = 'user.name'; |
| 40 | public const SESSION_INFO_SESSION_VAR_NAME = 'session.info'; |
| 41 | public const SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED = 'twofactorauth.verified'; |
| 42 | public const SESSION_INFO_TEMP_TOKEN_AUTH = 'user.token_auth_temp'; |
| 43 | public function getUser() |
| 44 | { |
| 45 | if (isset($_SESSION[self::USER_NAME_SESSION_VAR_NAME])) { |
| 46 | return $_SESSION[self::USER_NAME_SESSION_VAR_NAME]; |
| 47 | } |
| 48 | return null; |
| 49 | } |
| 50 | public function getUserInfo() |
| 51 | { |
| 52 | if (isset($_SESSION[self::SESSION_INFO_SESSION_VAR_NAME])) { |
| 53 | return $_SESSION[self::SESSION_INFO_SESSION_VAR_NAME]; |
| 54 | } |
| 55 | return null; |
| 56 | } |
| 57 | public function getSessionTokenAuth() |
| 58 | { |
| 59 | if (!empty($_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH])) { |
| 60 | return $_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH]; |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | public function hasVerifiedTwoFactor() |
| 65 | { |
| 66 | if (isset($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED])) { |
| 67 | return !empty($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED]); |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | public function setTwoFactorAuthenticationVerified() |
| 72 | { |
| 73 | $_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED] = 1; |
| 74 | } |
| 75 | public function initialize($userName, $tokenAuth, $isRemembered = \false, $time = null) |
| 76 | { |
| 77 | $time = $time ?: Date::now()->getTimestampUTC(); |
| 78 | $_SESSION[self::USER_NAME_SESSION_VAR_NAME] = $userName; |
| 79 | $_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED] = 0; |
| 80 | $_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH] = $tokenAuth; |
| 81 | $_SESSION[self::SESSION_INFO_SESSION_VAR_NAME] = ['ts' => $time, 'remembered' => $isRemembered, 'expiration' => $this->getExpirationTimeFromNow($time)]; |
| 82 | } |
| 83 | public function clear() |
| 84 | { |
| 85 | if (isset($_SESSION[self::USER_NAME_SESSION_VAR_NAME])) { |
| 86 | // may not be available during tests |
| 87 | unset($_SESSION[self::USER_NAME_SESSION_VAR_NAME]); |
| 88 | } |
| 89 | if (isset($_SESSION[self::SESSION_INFO_SESSION_VAR_NAME])) { |
| 90 | // may not be available during tests |
| 91 | unset($_SESSION[self::SESSION_INFO_SESSION_VAR_NAME]); |
| 92 | } |
| 93 | if (isset($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED])) { |
| 94 | // may not be available during tests |
| 95 | unset($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED]); |
| 96 | } |
| 97 | if (isset($_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH])) { |
| 98 | // may not be available during tests |
| 99 | unset($_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH]); |
| 100 | } |
| 101 | } |
| 102 | public function getSessionStartTime() |
| 103 | { |
| 104 | $userInfo = $this->getUserInfo(); |
| 105 | if (empty($userInfo) || empty($userInfo['ts'])) { |
| 106 | return null; |
| 107 | } |
| 108 | return $userInfo['ts']; |
| 109 | } |
| 110 | public function getExpirationTime() |
| 111 | { |
| 112 | $userInfo = $this->getUserInfo(); |
| 113 | if (empty($userInfo) || empty($userInfo['expiration'])) { |
| 114 | return null; |
| 115 | } |
| 116 | return $userInfo['expiration']; |
| 117 | } |
| 118 | public function isRemembered() |
| 119 | { |
| 120 | $userInfo = $this->getUserInfo(); |
| 121 | return !empty($userInfo['remembered']); |
| 122 | } |
| 123 | public function updateSessionExpirationTime() |
| 124 | { |
| 125 | $_SESSION[self::SESSION_INFO_SESSION_VAR_NAME]['expiration'] = $this->getExpirationTimeFromNow(); |
| 126 | } |
| 127 | private function getExpirationTimeFromNow($time = null) |
| 128 | { |
| 129 | $time = $time ?: Date::now()->getTimestampUTC(); |
| 130 | $general = Config::getInstance()->General; |
| 131 | if (!isset($general['login_session_not_remembered_idle_timeout']) || (int) $general['login_session_not_remembered_idle_timeout'] <= 0) { |
| 132 | $nonRememberedSessionExpireTime = self::DEFAULT_IDLE_TIMEOUT; |
| 133 | } else { |
| 134 | $nonRememberedSessionExpireTime = (int) $general['login_session_not_remembered_idle_timeout']; |
| 135 | } |
| 136 | $sessionCookieLifetime = $general['login_cookie_expire']; |
| 137 | if ($this->isRemembered()) { |
| 138 | $expireDuration = $sessionCookieLifetime; |
| 139 | } else { |
| 140 | $expireDuration = $nonRememberedSessionExpireTime; |
| 141 | } |
| 142 | return $time + $expireDuration; |
| 143 | } |
| 144 | } |
| 145 |