Skip to content

Commit 92b54eb

Browse files
Enhanced BroadcastAuthDebug middleware for improved error logging and channel information parsing
- Updated BroadcastAuthDebug middleware to log additional details on failed broadcast authentication attempts, including user anonymity and channel type.
1 parent b375796 commit 92b54eb

4 files changed

Lines changed: 59 additions & 17 deletions

File tree

ProcessMaker/Http/Middleware/BroadcastAuthDebug.php

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,67 @@ public function handle($request, Closure $next)
2424
return $response;
2525
}
2626

27+
if ($response->getStatusCode() < 400) {
28+
return $response;
29+
}
30+
2731
$user = Auth::user();
32+
$channelName = $request->input('channel_name');
33+
$channelInfo = $this->parseChannelInfo($channelName);
2834

29-
if ($response->getStatusCode() >= 400) {
30-
Log::error('Broadcast auth failed', [
31-
'status' => $response->getStatusCode(),
32-
'user_id' => $user?->id,
33-
'user_type' => $user ? get_class($user) : null,
34-
'has_session' => $request->hasSession(),
35-
'session_id' => $request->session()?->getId(),
36-
'channel' => $request->input('channel_name'),
37-
'cookie_present' => $request->hasCookie(config('session.cookie')),
38-
'ip' => $request->ip(),
39-
'timestamp' => now()->toIso8601String(),
40-
]);
41-
}
35+
Log::error('Broadcast auth failed', [
36+
'status' => $response->getStatusCode(),
37+
'user_id' => $user?->id,
38+
'user_type' => $user ? get_class($user) : null,
39+
'user_is_anonymous' => $user && method_exists($user, 'isAnonymous') ? $user->isAnonymous : null,
40+
'has_session' => $request->hasSession(),
41+
'session_id' => $request->session()?->getId(),
42+
'channel_name' => $channelName,
43+
'channel_type' => $channelInfo['type'],
44+
'channel_resource_id' => $channelInfo['id'],
45+
'user_channel_mismatch' => $channelInfo['type'] === 'User' && $user && $channelInfo['id']
46+
? (string) $user->id !== (string) $channelInfo['id']
47+
: null,
48+
'cookie_present' => $request->hasCookie(config('session.cookie')),
49+
'ip' => $request->ip(),
50+
'origin' => $request->header('Origin'),
51+
'referer' => $request->header('Referer'),
52+
'user_agent' => $request->userAgent(),
53+
'socket_id' => $request->input('socket_id'),
54+
'response_body' => $this->getResponseBody($response),
55+
'timestamp' => now()->toIso8601String(),
56+
]);
4257

4358
return $response;
4459
}
60+
61+
private function parseChannelInfo(?string $channelName): array
62+
{
63+
if (!$channelName) {
64+
return ['type' => null, 'id' => null];
65+
}
66+
// Strip tenant prefix: tenant_X.ProcessMaker.Models.User.14 -> ProcessMaker.Models.User.14
67+
$channel = preg_replace('/^tenant_\d+\./', '', $channelName);
68+
if (preg_match('/ProcessMaker\.Models\.User\.(\d+)/', $channel, $m)) {
69+
return ['type' => 'User', 'id' => $m[1]];
70+
}
71+
if (preg_match('/ProcessMaker\.Models\.ProcessRequest\.(\d+)/', $channel, $m)) {
72+
return ['type' => 'ProcessRequest', 'id' => $m[1]];
73+
}
74+
if (preg_match('/ProcessMaker\.Models\.ProcessRequestToken\.(\d+)/', $channel, $m)) {
75+
return ['type' => 'ProcessRequestToken', 'id' => $m[1]];
76+
}
77+
78+
return ['type' => 'other', 'id' => null];
79+
}
80+
81+
private function getResponseBody($response): ?string
82+
{
83+
$content = $response->getContent();
84+
if (is_string($content) && strlen($content) < 500) {
85+
return $content;
86+
}
87+
88+
return $content ? '[truncated]' : null;
89+
}
4590
}

ProcessMaker/Providers/BroadcastServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class BroadcastServiceProvider extends ServiceProvider
1515
*/
1616
public function boot()
1717
{
18-
//auth:web,anon is needed to allow anonymous users to listen to channels
1918
Broadcast::routes(['middleware' => ['web', 'auth:web,anon', BroadcastAuthDebug::class]]);
2019
require base_path('routes/channels.php');
2120
}

resources/js/bootstrap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ if (window.Processmaker && window.Processmaker.broadcasting) {
347347
config.authEndpoint = `${window.location.origin}/broadcasting/auth`;
348348
}
349349
config.auth = config.auth || {};
350+
config.auth.headers = config.auth.headers || {};
350351
if (config.auth.withCredentials === undefined) {
351352
config.auth.withCredentials = true;
352353
}

routes/web.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@
215215
Route::get('tasks/update_variable/{token_abe}', [TaskController::class, 'updateVariable'])->name('tasks.abe.update');
216216
});
217217

218-
// Add our broadcasting routes
219-
Broadcast::routes();
220-
221218
// Authentication Routes...
222219
Route::get('login', [LoginController::class, 'showLoginForm'])->name('login');
223220
Route::post('login', [LoginController::class, 'loginWithIntendedCheck']);

0 commit comments

Comments
 (0)