Limits of automatic session invalidation for expired credentials in CodeIgniter 4 Shield
0 reputation · 28 Apr 2024, 15:54 UTC
0 reputation · 28 Apr 2024, 15:54 UTC
When a user's password is marked as expired in the CodeIgniter 4 Shield authentication module, the login attempt fails unless the application explicitly checks the expiration flag and prompts a reset. However, any existing authenticated session remains active, allowing continued access until the session naturally expires or is manually destroyed. This behavior leaves a gap in least‑privilege enforcement because privileged sessions are not automatically revoked when credentials become stale.
What configuration or extension point should Shield provide to automatically invalidate sessions on password expiration? How can developers implement session invalidation without disrupting legitimate user workflows? What security trade‑offs arise from retaining sessions after credential expiration?
CodeIgniter 4 Shield’s Authenticator::attempt method checks the user’s isPasswordExpired flag and rejects the login attempt, but it does **not** destroy any existing authenticated session. Consequently, a session created before the password was marked expired remains valid until it expires naturally or is manually destroyed.
Shield does not emit an event when a password expires, and there is no built‑in configuration flag to trigger session invalidation. The expiration check is performed only during authentication, so existing sessions are untouched.
app/Http/Middleware/CheckPasswordExpiry.php).isPasswordExpired. If true, destroy the session and redirect to a password‑reset route.<?php
namespace App\Http\Middleware;
use CodeIgniter\Shield\Authentication\Authenticator;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\URI;
class CheckPasswordExpiry
{
protected $authenticator;
public function __construct(Authenticator $authenticator)
{
$this->authenticator = $authenticator;
}
public function process(RequestInterface $request, $next): ResponseInterface
{
$user = $this->authenticator->currentUser();
if ($user && $user->isPasswordExpired()) {
// Destroy the session and force a password reset
session()->destroy();
return redirect()->to('/auth/reset-password')->with('warning', 'Your password has expired. Please set a new one.');
}
return $next($request);
}
}
If your Shield version includes an auth.passwordExpired event, you can listen for it and call session()->destroy() or auth()->logout() inside the listener. Check the Shield Events\AuthEvents class for availability.
To confirm whether you can use the event‑listener approach, please specify the exact version of CodeIgniter 4 Shield you are using (e.g., 4.0.0, 4.1.2, etc.).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.