-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from PennyDreadfulMTG/sessions
Better session handling, (many) fewer logouts
- Loading branch information
Showing
7 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Gatherling\Auth; | ||
|
||
use Gatherling\Data\DB; | ||
|
||
class Session | ||
{ | ||
private static $LIFETIME = 60 * 60 * 24 * 60; | ||
|
||
public static function start(): void | ||
{ | ||
session_start(); | ||
self::init(); | ||
register_shutdown_function([self::class, 'save']); | ||
} | ||
|
||
private static function init(): void | ||
{ | ||
if (!empty($_SESSION)) { | ||
return; | ||
} | ||
if (!isset($_COOKIE['remember_me'])) { | ||
return; | ||
} | ||
$token = $_COOKIE['remember_me']; | ||
$_SESSION = self::load($token); | ||
} | ||
|
||
private static function load(string $token): array | ||
{ | ||
$sql = ' | ||
SELECT | ||
details | ||
FROM | ||
sessions | ||
WHERE | ||
token = :token | ||
AND | ||
expiry >= NOW()'; | ||
$args = ['token' => $token]; | ||
$details = DB::value($sql, $args); | ||
return $details ? json_decode($details, true) : []; | ||
} | ||
|
||
private static function save(): void | ||
{ | ||
if (isset($_COOKIE['remember_me'])) { | ||
$token = $_COOKIE['remember_me']; | ||
} else { | ||
$token = bin2hex(random_bytes(32)); | ||
} | ||
// Force to object so that we get '{}' instead of '[]' when empty | ||
$details = json_encode((object) $_SESSION); | ||
$expiry = time() + self::$LIFETIME; | ||
$sql = ' | ||
INSERT INTO | ||
sessions (token, details, expiry) | ||
VALUES | ||
(:token, :details, FROM_UNIXTIME(:expiry)) | ||
ON DUPLICATE KEY UPDATE | ||
details = :details, | ||
expiry = FROM_UNIXTIME(:expiry)'; | ||
$args = [ | ||
'token' => $token, | ||
'details' => $details, | ||
'expiry' => $expiry, | ||
]; | ||
DB::execute($sql, $args); | ||
setcookie('remember_me', $token, $expiry, '/'); | ||
$sql = 'DELETE FROM sessions WHERE expiry < NOW()'; | ||
DB::execute($sql); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS sessions ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
token VARCHAR(64) UNIQUE NOT NULL, | ||
details TEXT NOT NULL, | ||
expiry TIMESTAMP NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
function main(): void | ||
{ | ||
Setup::setupDatabase(); | ||
Setup::setupTestDatabase(); | ||
echo 'done'; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters