I auth storage

This commit is contained in:
a6a2f5842 2025-09-15 10:20:13 +02:00
parent bc6abf6841
commit 5bd6cf2341
3 changed files with 49 additions and 5 deletions

View File

@ -257,8 +257,8 @@ class Channel
if (! hash_equals( if (! hash_equals(
hash_hmac('sha256', $signature, $connection->app->secret), hash_hmac('sha256', $signature, $connection->app->secret),
Str::after($payload->auth, ':')) Str::after(optional($payload)->auth ?? ':', ':')
) { )) {
throw new InvalidSignature; throw new InvalidSignature;
} }
} }

View File

@ -20,10 +20,10 @@ class WebsocketService
// ); // );
} }
function getTenantable(string $socketId) function getAuth(string $socketId)
{ {
config(['cache.default' => 'file']); config(['cache.default' => 'file']);
return cache()->get('ws_socket_tenantable_' . $socketId); return cache()->get('ws_socket_auth_' . $socketId);
} }
public static function getChannelConnections(string $channelName) public static function getChannelConnections(string $channelName)
@ -43,4 +43,34 @@ class WebsocketService
config(['cache.default' => 'file']); config(['cache.default' => 'file']);
return cache()->get('ws_connection_' . $socketId); return cache()->get('ws_connection_' . $socketId);
} }
public static function getAuthedUsers()
{
config(['cache.default' => 'file']);
return cache()->get('ws_socket_authed_users') ?? [];
}
public static function isUserConnected($userId)
{
config(['cache.default' => 'file']);
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
$user_ids = array_values($authed_users);
return in_array($userId, $user_ids);
}
public static function getUserSocketIds($userId)
{
config(['cache.default' => 'file']);
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
$socket_ids = [];
foreach ($authed_users as $socket_id => $u_id) {
if ($u_id == $userId) {
$socket_ids[] = $socket_id;
}
}
return $socket_ids;
}
} }

View File

@ -191,7 +191,7 @@ class Handler implements MessageComponentInterface
} }
cache()->forget( cache()->forget(
'ws_socket_tenantable_' . $connection->socketId, 'ws_socket_auth_' . $connection->socketId,
); );
if (@$this->channel_connections[$channel]) { if (@$this->channel_connections[$channel]) {
@ -207,6 +207,10 @@ class Handler implements MessageComponentInterface
'ws_active_channels', 'ws_active_channels',
array_keys($this->channel_connections) array_keys($this->channel_connections)
); );
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
unset($authed_users[$connection->socketId]);
cache()->forever('ws_socket_authed_users', $authed_users);
} }
$this->channelManager $this->channelManager
@ -458,6 +462,16 @@ class Handler implements MessageComponentInterface
/** @var \App\Models\User */ /** @var \App\Models\User */
$user = Auth::user(); $user = Auth::user();
$user->refresh(); $user->refresh();
cache()->forever(
'ws_socket_auth_' . $connection->socketId,
$user,
);
$authed_users = cache()->get('ws_socket_authed_users') ?? [];
$authed_users[$connection->socketId] = $user->id;
cache()->forever('ws_socket_authed_users', $authed_users);
} }
} }