Skip to content

Commit

Permalink
Pusher Authorization Endpoint (#16)
Browse files Browse the repository at this point in the history
* added pusher auth endpoint.

* Fix styling
  • Loading branch information
yordadev authored Sep 8, 2022
1 parent 9e1a23e commit 389606f
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "^8.0",
"illuminate/contracts": "^8.0|^9.0",
"guzzlehttp/guzzle": "^7.4.5"
"guzzlehttp/guzzle": "^7.4.5",
"pusher/pusher-php-server": "^7.1@beta"
},
"require-dev": {
"ext-pdo_sqlite": "*",
Expand Down
208 changes: 206 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions routes/BroadcastAuthRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use YorCreative\QueryWatcher\Services\BroadcastAuthService;

Route::post('/broadcasting/query-watcher/{driver}/auth', function (Request $request, $driver) {
return match ($driver) {
'pusher' => BroadcastAuthService::pusher($request),
default => response([], 401),
};
})->name('broadcasting.pusher.auth');
15 changes: 12 additions & 3 deletions src/QueryWatcherServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace YorCreative\QueryWatcher;

use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Pusher\Pusher;
use YorCreative\QueryWatcher\Events\QueryEvent;
use YorCreative\QueryWatcher\Listeners\QueryListener;

Expand All @@ -20,6 +21,8 @@ public function register()
{
$this->mergeConfigFrom(dirname(__DIR__, 1).'/config/querywatcher.php', 'querywatcher');

$this->loadRoutesFrom(dirname(__DIR__, 1).'/routes/BroadcastAuthRoute.php');

$this->publishes([
dirname(__DIR__, 1).'/config' => base_path('config'),
]);
Expand Down Expand Up @@ -49,8 +52,14 @@ public function getEventListeners(): array

public function boot()
{
Broadcast::routes();

require dirname(__DIR__, 1).'/routes/QueryChannel.php';

$this->app->singleton(Pusher::class, function () {
return new Pusher(
Config::get('broadcasting.connections.pusher.key'),
Config::get('broadcasting.connections.pusher.secret'),
Config::get('broadcasting.connections.pusher.app_id')
);
});
}
}
31 changes: 31 additions & 0 deletions src/Services/BroadcastAuthService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace YorCreative\QueryWatcher\Services;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pusher\Pusher;
use Pusher\PusherException;

class BroadcastAuthService
{
/**
* @param Request $request
* @return Response|Application|ResponseFactory
*
* @throws PusherException
*/
public static function pusher(Request $request): Response|Application|ResponseFactory
{
$pusher = app(Pusher::class);

$auth = $pusher->authorizeChannel(
$request->input('channel_name'),
$request->input('socket_id')
);

return response($auth, 200);
}
}

0 comments on commit 389606f

Please sign in to comment.