セッションタイムアウトとなったsession_id を取得したい

実現したいこと

セッションの有効期限が切れてリクエストされたセッションのsessionidを取得したい。
(もしかして、ブラウザ側でsessionの有効期限を判定してクッキー情報からlaravel_sessionを削除するので、有効期限が切れたセッションidは取得できないですか?)

前提

laravelのmiddlewareに標準で存在するsessionが生きているかの判定について調査中。

以下ファイルが大きく関わっている可能性。
特にSessionGuard.phpにてリクエストされたセッションidが有効であるかを判定していると考えています。
vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php
vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php
vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php

発生している問題・エラーメッセージ

取得できるsession_idが、セッションタイムアウト前に生成されたsession_idではなく
既に新しく生成されたsession_idとなっていた。

該当のソースコード

vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php protected function authenticate($request, array $guards) { if (empty($guards)) { $guards = [null]; } foreach ($guards as $guard) { if ($this->auth->guard($guard)->check()) { return $this->auth->shouldUse($guard); } } $this->unauthenticated($request, $guards); }
vendor/laravel/framework/src/Illuminate/Auth/GuardHelpers.php /** * Determine if the current user is authenticated. * * @return bool */ public function check() { return ! is_null($this->user()); }
vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php /** * Get the currently authenticated user. * * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function user() { if ($this->loggedOut) { return; } // If we've already retrieved the user for the current request we can just // return it back immediately. We do not want to fetch the user data on // every call to this method because that would be tremendously slow. if (! is_null($this->user)) { return $this->user; } $id = $this->session->get($this->getName()); // First we will try to load the user using the identifier in the session if // one exists. Otherwise we will check for a "remember me" cookie in this // request, and if one exists, attempt to retrieve the user using that. if (! is_null($id) && $this->user = $this->provider->retrieveById($id)) { $this->fireAuthenticatedEvent($this->user); } // If the user is null, but we decrypt a "recaller" cookie we can attempt to // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. if (is_null($this->user) && ! is_null($recaller = $this->recaller())) { $this->user = $this->userFromRecaller($recaller); if ($this->user) { $this->updateSession($this->user->getAuthIdentifier()); $this->fireLoginEvent($this->user, true); } } return $this->user; }

試したこと

上記3ファイルにてリクエストされたセッションidをログに出力する処理を実施。
・セッションタイムアウト前のセッションidを取得し、手元にメモを残す。
・セッションタイムアウト後のセッションidを取得し、比較する。

あくまでもリクエストされるセッションidはlaravelで設定しているタイムアウト時間に関わらず、ブラウザが保持しているsession_idがリクエストされると想定していた。
リクエストされたsession_idがlaravelで設定した有効期限より上回っている場合に
laravel側で新しくセッションidを生成し、ブラウザ側にセッションidをレスポンスで渡すと考えていた。
ただ、今回の結果ではそもそもリクエストされているsession_idが新しく生成されたものとなっていた。
ブラウザ側でlaravelが設定した有効期限を検知して、有効期限が切れている場合に新たにセッションidを生成してサーバに送信する挙動はないと認識しているので、期待している結果と異なっている。

補足情報(FW/ツールのバージョンなど)

laravel 9
session.php にて'lifetime' => env('SESSION_LIFETIME', 1 ) を設定
(テストの為、1分でセッションが切れるように。)

コメントを投稿

0 コメント