Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOUR-16704: Session Token Not Invalidated on Logout #7810

Open
wants to merge 6 commits into
base: release-2024-fall
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ProcessMaker/Events/SessionStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
Expand Down Expand Up @@ -60,6 +61,14 @@ public function broadcastWith()
? 'Number.MAX_SAFE_INTEGER'
: config('session.lifetime');


// Initialize the session activity control
Cache::put(
'user_' . $this->user->id . '_active_session_' . RequestDevice::getId(),
['active' => true, 'updated_at' => now()],
now()->addMinutes(config('session.lifetime'))
);

return [
'lifetime' => $lifetime,
'device_id' => RequestDevice::getId(),
Expand Down
11 changes: 8 additions & 3 deletions ProcessMaker/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ public function beforeLogout(Request $request)
$userId = Auth::user()->id;
Cache::forget("user_{$userId}_permissions");
Cache::forget("user_{$userId}_project_assets");
Cache::put(
'user_' . $userId . '_active_session_' . $request->cookie('device_id'),
['active' => false, 'updated_at' => now()],
now()->addMinutes(config('session.lifetime'))
);

// Clear the user session
$this->forgetUserSession();
Expand All @@ -261,12 +266,12 @@ public function beforeLogout(Request $request)
// Notify to listeners (package-auth, security logger)
$eventResult = event(new Logout(Auth::user()));

// Perform the logout operation
$this->logout($request);

// Remove the Laravel cookie
Cookie::queue(Cookie::forget(Passport::cookie()));

// Perform the logout operation
$this->logout($request);

// process any redirects generated by the logout event listeners
foreach ($eventResult as $result) {
if (is_array($result) && array_key_exists('redirectTo', $result)) {
Expand Down
1 change: 1 addition & 0 deletions ProcessMaker/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Kernel extends HttpKernel
'session_kill' => Middleware\SessionControlKill::class,
'no-cache' => Middleware\NoCache::class,
'admin' => Middleware\IsAdmin::class,
'verify_active_session' => Middleware\VerifyActiveSession::class,
];

/**
Expand Down
46 changes: 46 additions & 0 deletions ProcessMaker/Http/Middleware/VerifyActiveSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ProcessMaker\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use ProcessMaker\Models\UserSession;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Crypt;

class VerifyActiveSession
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// if the authorization is via token in cookie
if (!$request->hasHeader('Authorization') && $request->hasCookie('laravel_token')) {
$user = \Auth::user();

$cacheKey = 'user_' . $user->id . '_active_session_' . $request->cookie('device_id');
$activeSession = Cache::get($cacheKey);
$isActive = $activeSession ? $activeSession['active'] : false;
if (!$isActive) {
return response()->json(['error' => 'Unauthorized'], 401);
}
else {
$lastActivity = $activeSession ? $activeSession['updated_at'] : now();
// refresh the cache entry's lifetime
if (now()->diffInMinutes($lastActivity) > config('session.lifetime') / 2) {
Cache::put(
$cacheKey,
['active' => true, 'updated_at' => now()],
now()->addMinutes(config('session.lifetime'))
);
}
}
}

return $next($request);
}
}
2 changes: 1 addition & 1 deletion ProcessMaker/Listeners/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace ProcessMaker\Listeners;

use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Cache;
use ProcessMaker\Models\InboxRuleLog;
use ProcessMaker\Models\User;
use ProcessMaker\Notifications\InboxRulesNotification;

class LoginListener
{
/**
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
use ProcessMaker\Http\Controllers\Auth\TwoFactorAuthController;
use ProcessMaker\Http\Controllers\TestStatusController;

Route::middleware('auth:api', 'setlocale', 'bindings', 'sanitize')->prefix('api/1.0')->name('api.')->group(function () {
Route::middleware('auth:api', 'setlocale', 'bindings', 'sanitize', 'verify_active_session')->prefix('api/1.0')->name('api.')->group(function () {
// Users
Route::get('users', [UserController::class, 'index'])->name('users.index'); // Permissions handled in the controller
Route::get('users/{user}', [UserController::class, 'show'])->name('users.show'); // Permissions handled in the controller
Expand Down
Loading