Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit c1f6ffa

Browse files
coolcodemympociot
authored andcommitted
Fix Invalid Signature issue and enable event creator to be sent from any app (#39)
* Add the ability to configure middleware. Fixes #22 * Fix StyleCI Error. * Include X-App-ID * Reconstruct the PusherBroadcaster * fix styleci * change from overwriting constructor to new Broadcaster * optional inside dashboard gate * remove comment * fix for styleci * Fix typo * Removed unused $config['options']
1 parent ec96ca7 commit c1f6ffa

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

config/websockets.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
4+
35
return [
46

57
/*
@@ -47,6 +49,18 @@
4749
*/
4850
'path' => 'laravel-websockets',
4951

52+
/*
53+
* Dashboard Routes Middleware
54+
*
55+
* These middleware will be assigned to every dashboard route, giving you
56+
* the chance to add your own middleware to this list or change any of
57+
* the existing middleware. Or, you can simply stick with this list.
58+
*/
59+
'middleware' => [
60+
'web',
61+
Authorize::class,
62+
],
63+
5064
'statistics' => [
5165
/*
5266
* This model will be used to store the statistics of the WebSocketsServer.

resources/views/dashboard.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@
120120
authEndpoint: '/{{ request()->path() }}/auth',
121121
auth: {
122122
headers: {
123-
'X-CSRF-Token': "{{ csrf_token() }}"
123+
'X-CSRF-Token': "{{ csrf_token() }}",
124+
'X-App-ID': this.app.id
124125
}
125126
},
126127
enabledTransports: ['ws', 'flash']

src/Dashboard/Http/Controllers/AuthenticateDashboard.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
44

5+
use Pusher\Pusher;
56
use Illuminate\Http\Request;
6-
use Illuminate\Contracts\Broadcasting\Broadcaster;
7+
use BeyondCode\LaravelWebSockets\Apps\App;
8+
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
79

810
class AuthenticateDashboard
911
{
10-
public function __invoke(Request $request, Broadcaster $broadcaster)
12+
public function __invoke(Request $request)
1113
{
14+
/**
15+
* Find the app by using the header
16+
* and then reconstruct the PusherBroadcaster
17+
* using our own app selection.
18+
*/
19+
$app = App::findById($request->header('x-app-id'));
20+
21+
$broadcaster = new PusherBroadcaster(new Pusher(
22+
$app->key,
23+
$app->secret,
24+
$app->id,
25+
[]
26+
));
27+
1228
/*
1329
* Since the dashboard itself is already secured by the
1430
* Authorize middleware, we can trust all channel

src/Dashboard/Http/Middleware/Authorize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Authorize
88
{
99
public function handle($request, $next)
1010
{
11-
return Gate::check('viewWebSocketsDashboard') ? $next($request) : abort(403);
11+
return Gate::check('viewWebSocketsDashboard', [$request->user()]) ? $next($request) : abort(403);
1212
}
1313
}

src/WebSocketsServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function register()
6464
protected function registerRoutes()
6565
{
6666
Route::prefix(config('websockets.path'))->group(function () {
67-
Route::middleware(AuthorizeDashboard::class)->group(function () {
67+
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () {
6868
Route::get('/', ShowDashboard::class);
6969
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']);
7070
Route::post('auth', AuthenticateDashboard::class);

0 commit comments

Comments
 (0)