PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Session / SessionFingerprint.php
matomo / app / core / Session Last commit date
SaveHandler 3 months ago SessionAuth.php 4 months ago SessionFingerprint.php 7 months ago SessionInitializer.php 1 year ago SessionNamespace.php 1 year ago
SessionFingerprint.php
147 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,
76 #[\SensitiveParameter]
77 $tokenAuth, $isRemembered = \false, $time = null)
78 {
79 $time = $time ?: Date::now()->getTimestampUTC();
80 $_SESSION[self::USER_NAME_SESSION_VAR_NAME] = $userName;
81 $_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED] = 0;
82 $_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH] = $tokenAuth;
83 $_SESSION[self::SESSION_INFO_SESSION_VAR_NAME] = ['ts' => $time, 'remembered' => $isRemembered, 'expiration' => $this->getExpirationTimeFromNow($time)];
84 }
85 public function clear()
86 {
87 if (isset($_SESSION[self::USER_NAME_SESSION_VAR_NAME])) {
88 // may not be available during tests
89 unset($_SESSION[self::USER_NAME_SESSION_VAR_NAME]);
90 }
91 if (isset($_SESSION[self::SESSION_INFO_SESSION_VAR_NAME])) {
92 // may not be available during tests
93 unset($_SESSION[self::SESSION_INFO_SESSION_VAR_NAME]);
94 }
95 if (isset($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED])) {
96 // may not be available during tests
97 unset($_SESSION[self::SESSION_INFO_TWO_FACTOR_AUTH_VERIFIED]);
98 }
99 if (isset($_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH])) {
100 // may not be available during tests
101 unset($_SESSION[self::SESSION_INFO_TEMP_TOKEN_AUTH]);
102 }
103 }
104 public function getSessionStartTime()
105 {
106 $userInfo = $this->getUserInfo();
107 if (empty($userInfo) || empty($userInfo['ts'])) {
108 return null;
109 }
110 return $userInfo['ts'];
111 }
112 public function getExpirationTime()
113 {
114 $userInfo = $this->getUserInfo();
115 if (empty($userInfo) || empty($userInfo['expiration'])) {
116 return null;
117 }
118 return $userInfo['expiration'];
119 }
120 public function isRemembered()
121 {
122 $userInfo = $this->getUserInfo();
123 return !empty($userInfo['remembered']);
124 }
125 public function updateSessionExpirationTime()
126 {
127 $_SESSION[self::SESSION_INFO_SESSION_VAR_NAME]['expiration'] = $this->getExpirationTimeFromNow();
128 }
129 private function getExpirationTimeFromNow($time = null)
130 {
131 $time = $time ?: Date::now()->getTimestampUTC();
132 $general = Config::getInstance()->General;
133 if (!isset($general['login_session_not_remembered_idle_timeout']) || (int) $general['login_session_not_remembered_idle_timeout'] <= 0) {
134 $nonRememberedSessionExpireTime = self::DEFAULT_IDLE_TIMEOUT;
135 } else {
136 $nonRememberedSessionExpireTime = (int) $general['login_session_not_remembered_idle_timeout'];
137 }
138 $sessionCookieLifetime = $general['login_cookie_expire'];
139 if ($this->isRemembered()) {
140 $expireDuration = $sessionCookieLifetime;
141 } else {
142 $expireDuration = $nonRememberedSessionExpireTime;
143 }
144 return $time + $expireDuration;
145 }
146 }
147