Skip to content

Commit

Permalink
refactor remember me cookie close #260
Browse files Browse the repository at this point in the history
upgrade security
rename it from authtoken to rememberme
  • Loading branch information
vincent-peugnet committed Dec 24, 2023
1 parent 1664d3b commit 7ed78aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/class/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function setuser()
Logger::warning("Deleted session using non existing user : '$sessionuser'");
$this->servicesession->empty(); // empty the session as a non existing user was set
}
} elseif (!empty($_COOKIE['authtoken'])) {
} elseif (!empty($_COOKIE['rememberme'])) {
try {
$modelconnect = new Modelconnect();
$datas = $modelconnect->checkcookie();
Expand Down
2 changes: 1 addition & 1 deletion app/class/Controllerhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function deepsearch(): array
protected function listquery(): void
{
if (isset($_POST['listquery']) && $this->user->iseditor()) {
$datas = array_merge($_POST, $_SESSION['opt']);
$datas = array_merge($_POST, $this->servicesession->getopt());
$this->optlist = new Optlist($datas);
if (!empty($this->optlist->bookmark())) {
$this->optlist->resetall();
Expand Down
18 changes: 13 additions & 5 deletions app/class/Modelconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ public function createauthcookie(string $userid, string $wsession, int $conserva
throw new RuntimeException("Secret Key not set");
}
$jwt = JWT::encode($datas, Config::secretkey());
$cookie = setcookie('authtoken', $jwt, time() + $conservation * 24 * 3600, '/' . Config::basepath(), "", false, true);
$options = [
'expires' => time() + $conservation * 24 * 3600,
'path' => '/' . Config::basepath(),
'domain' => '',
'secure' => Config::issecure(),
'httponly' => true,
'samesite' => 'Strict'
];
$cookie = setcookie('rememberme', $jwt, $options);
if (!$cookie) {
throw new RuntimeException("Cant be send");
throw new RuntimeException("Remember me cookie cannot be created");
}
}

Expand All @@ -37,8 +45,8 @@ public function createauthcookie(string $userid, string $wsession, int $conserva
*/
public function checkcookie(): array
{
if (!empty($_COOKIE['authtoken'])) {
$datas = JWT::decode($_COOKIE['authtoken'], Config::secretkey(), ['HS256']);
if (!empty($_COOKIE['rememberme'])) {
$datas = JWT::decode($_COOKIE['rememberme'], Config::secretkey(), ['HS256']);
return get_object_vars($datas);
} else {
throw new RuntimeException('Auth cookie is unset');
Expand All @@ -50,6 +58,6 @@ public function checkcookie(): array
*/
public function deleteauthcookie(): void
{
$_COOKIE['authtoken'] = [];
$_COOKIE['rememberme'] = [];
}
}

0 comments on commit 7ed78aa

Please sign in to comment.