Skip to content

Commit b232143

Browse files
Merge pull request #429 from matomo-org/fix-webserver-auth-token-reload
Creating session fingerprint on successful web server authentication
2 parents 7a38323 + 461b822 commit b232143

4 files changed

Lines changed: 64 additions & 2 deletions

File tree

Auth/WebServerAuth.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Piwik\Plugins\LoginLdap\Model\LdapUsers;
1919
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;
2020
use Piwik\Plugins\UsersManager\Model as UserModel;
21+
use Piwik\Session;
22+
use Piwik\Session\SessionFingerprint;
2123

2224
/**
2325
* Auth implementation that assumes the web server that hosts Piwik has authenticated
@@ -85,7 +87,10 @@ public function authenticate()
8587
));
8688
}
8789

88-
return $this->makeSuccessLogin($this->getUserForLogin());
90+
$result = $this->makeSuccessLogin($this->getUserForLogin());
91+
$this->initializeSessionFingerprint($result);
92+
93+
return $result;
8994
}
9095
} catch (ConnectionException $ex) {
9196
throw $ex;
@@ -157,6 +162,28 @@ private function synchronizeLoggedInUser()
157162
$this->synchronizeLdapUser($ldapUser);
158163
}
159164

165+
private function initializeSessionFingerprint(AuthResult $authResult): void
166+
{
167+
if (!Session::isSessionStarted() || !Session::isWritable()) {
168+
return;
169+
}
170+
171+
$sessionFingerprint = new SessionFingerprint();
172+
$isSameUser = $sessionFingerprint->getUser() === $authResult->getIdentity();
173+
$hasVerifiedTwoFactor = $isSameUser && $sessionFingerprint->hasVerifiedTwoFactor();
174+
175+
Session::regenerateId();
176+
177+
$sessionFingerprint->initialize(
178+
$authResult->getIdentity(),
179+
$authResult->getTokenAuth()
180+
);
181+
182+
if ($hasVerifiedTwoFactor) {
183+
$sessionFingerprint->setTwoFactorAuthenticationVerified();
184+
}
185+
}
186+
160187
/**
161188
* Returns a WebServerAuth instance configured with INI config.
162189
*

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# LoginLdap Changelog
22

3+
#### LoginLdap 5.1.8 - 2026-03-30
4+
* Updated API documentation
5+
36
#### LoginLdap 5.1.7 - 2026-03-02
47
* Updated API documentation
58

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "LoginLdap",
3-
"version": "5.1.7",
3+
"version": "5.1.8",
44
"description": "LDAP authentication and synchronization for Matomo.",
55
"theme": false,
66
"keywords": ["ldap", "login", "authentication", "active", "directory", "kerberos", "sso"],

tests/Integration/WebServerAuthTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Piwik\Config;
1515
use Piwik\Plugins\LoginLdap\Auth\WebServerAuth;
1616
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;
17+
use Piwik\Session;
18+
use Piwik\Session\SessionAuth;
19+
use Piwik\Session\SessionFingerprint;
1720

1821
/**
1922
* @group LoginLdap
@@ -166,4 +169,33 @@ public function test_WebServerAuth_Fails_IfDomainNotStrippedCorrectly()
166169
$authResult = $ldapAuth->authenticate();
167170
$this->assertEquals(AuthResult::FAILURE, $authResult->getCode());
168171
}
172+
173+
/**
174+
* @runInSeparateProcess
175+
*/
176+
public function test_WebServerAuth_CreatesReusableAuthenticatedSession()
177+
{
178+
Config::getInstance()->LoginLdap['use_webserver_auth'] = 1;
179+
180+
Session::start();
181+
$originalSessionId = session_id();
182+
183+
$_SERVER['REMOTE_USER'] = self::TEST_LOGIN;
184+
185+
$ldapAuth = WebServerAuth::makeConfigured();
186+
$authResult = $ldapAuth->authenticate();
187+
188+
$this->assertEquals(AuthResult::SUCCESS, $authResult->getCode());
189+
$this->assertNotSame($originalSessionId, session_id());
190+
191+
$sessionFingerprint = new SessionFingerprint();
192+
$this->assertEquals(self::TEST_LOGIN, $sessionFingerprint->getUser());
193+
$this->assertSame($authResult->getTokenAuth(), $sessionFingerprint->getSessionTokenAuth());
194+
195+
$sessionAuth = new SessionAuth(null, false);
196+
$sessionAuthResult = $sessionAuth->authenticate();
197+
198+
$this->assertEquals(AuthResult::SUCCESS, $sessionAuthResult->getCode());
199+
$this->assertSame(self::TEST_LOGIN, $sessionAuthResult->getIdentity());
200+
}
169201
}

0 commit comments

Comments
 (0)