Skip to content

Commit b78a1f1

Browse files
committed
refactoring session hijacking filter
1 parent 7decb6a commit b78a1f1

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Filters/CheckUserSession.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,35 @@ class CheckUserSession implements FilterInterface
1515
*/
1616
public function before(RequestInterface $request, $arguments = null): RedirectResponse|null
1717
{
18+
$config = config(\Franky5831\CodeIgniter4UserLibrary\Config\App::class);
1819
// Check if the user is logged in
1920
helper('user_helper');
2021
if (!isLoggedIn()) {
2122
return null;
2223
}
2324

2425
// Checks if the client ip and user agent are the same as the ones stored in the session
26+
// If they are not the same then destroy the session and redirect to the login page if not already on the login page
2527
$clientIpMatch = session()->client_ip == getClientIp();
2628
$clientAgentMatch = session()->user_agent == getUserAgent();
29+
$enableMatchIP = $config->sessionHijackingMatchIP;
30+
$enableMatchUserAgent = $config->sessionHijackingMatchUserAgent;
2731

28-
// If they are not the same then destroy the session and redirect to the login page if not already on the login page
29-
if ($clientIpMatch && $clientAgentMatch) {
30-
return null;
31-
}
32-
session()->destroy();
32+
if (
33+
(!$clientIpMatch && $enableMatchIP)
34+
|| (!$clientAgentMatch && $enableMatchUserAgent)
35+
) {
36+
session()->destroy();
3337

34-
$currentUrl = current_url();
35-
$loginUrl = url_to('loginurl');
36-
if ($currentUrl == $loginUrl) {
37-
return null;
38+
$currentUrl = current_url();
39+
$loginUrl = url_to('loginurl');
40+
if ($currentUrl == $loginUrl) {
41+
return null;
42+
}
43+
return redirect()->to($loginUrl);
3844
}
39-
return redirect()->to($loginUrl);
45+
46+
return null;
4047
}
4148

4249
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): ResponseInterface

0 commit comments

Comments
 (0)