laravel-websockets/src/LaravelEcho/Http/Controllers/TriggerEvent.php

53 lines
1.7 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
use Illuminate\Http\Request;
2018-11-22 20:36:25 +00:00
use Symfony\Component\HttpKernel\Exception\HttpException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
2018-11-21 11:13:40 +00:00
2018-11-22 20:36:25 +00:00
class TriggerEvent extends EchoController
2018-11-21 11:13:40 +00:00
{
2018-11-21 23:12:23 +00:00
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
2018-11-21 21:11:44 +00:00
protected $channelManager;
2018-11-21 14:42:04 +00:00
public function __construct(ChannelManager $channelManager)
{
$this->channelManager = $channelManager;
}
2018-11-21 11:13:40 +00:00
public function __invoke(Request $request)
{
2018-11-22 17:13:43 +00:00
$this->verifySignature($request);
2018-11-21 14:42:04 +00:00
foreach ($request->json()->get('channels', []) as $channelId) {
2018-11-21 21:11:44 +00:00
$channel = $this->channelManager->find($request->appId, $channelId);
2018-11-21 14:42:04 +00:00
2018-11-21 23:12:23 +00:00
optional($channel)->broadcast([
2018-11-21 14:42:04 +00:00
'channel' => $channelId,
'event' => $request->json()->get('name'),
'data' => $request->json()->get('data'),
]);
}
2018-11-21 23:12:23 +00:00
2018-11-21 11:13:40 +00:00
return $request->json()->all();
}
2018-11-22 17:13:43 +00:00
protected function verifySignature(Request $request)
{
$bodyMd5 = md5($request->getContent());
$signature =
"POST\n/apps/{$request->get('appId')}/events\n".
"auth_key={$request->get('auth_key')}".
"&auth_timestamp={$request->get('auth_timestamp')}".
"&auth_version={$request->get('auth_version')}".
"&body_md5={$bodyMd5}";
$authSignature = hash_hmac('sha256', $signature, config('broadcasting.connections.pusher.secret'));
if ($authSignature !== $request->get('auth_signature')) {
2018-11-22 20:36:25 +00:00
throw new HttpException(401, 'Invalid auth signature provided.');
2018-11-22 17:13:43 +00:00
}
}
2018-11-21 11:13:40 +00:00
}