-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e69f77a
commit 616c689
Showing
14 changed files
with
188 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,6 @@ class EncryptCookies extends Middleware | |
* @var array | ||
*/ | ||
protected $except = [ | ||
// | ||
'mercureAuthorization' | ||
]; | ||
} |
55 changes: 55 additions & 0 deletions
55
app/Http/Middleware/MercureBroarcasterAuthorizationCookie.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Lcobucci\JWT\Configuration; | ||
use Lcobucci\JWT\Signer\Hmac\Sha256; | ||
use Lcobucci\JWT\Signer\Key\InMemory; | ||
use Illuminate\Support\Facades\Cookie; | ||
|
||
class MercureBroadcasterAuthorizationCookie | ||
{ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
/** @var Response $response */ | ||
$response = $next($request); | ||
|
||
if (!method_exists($response, 'withCookie')) { | ||
return $response; | ||
} | ||
|
||
return $response->withCookie($this->createCookie($request->user(), $request->secure())); | ||
} | ||
|
||
private function createCookie($user, bool $secure) | ||
{ | ||
// Add topic(s) this user has access to | ||
// This can also be URI Templates (to match several topics), or * (to match all topics) | ||
$subscriptions = [ | ||
"http://example/user/{$user->id}/direct-messages", | ||
]; | ||
|
||
$jwtConfiguration = Configuration::forSymmetricSigner( | ||
new Sha256(), | ||
InMemory::plainText(config('broadcasting.connections.mercure.secret')) | ||
); | ||
|
||
$token = $jwtConfiguration->builder() | ||
->withClaim('mercure', ['subscribe' => $subscriptions]) | ||
->getToken($jwtConfiguration->signer(), $jwtConfiguration->signingKey()) | ||
->toString(); | ||
|
||
return Cookie::make( | ||
'mercureAuthorization', | ||
$token, | ||
15, | ||
'/.well-known/mercure', // or which path you have mercure running | ||
parse_url(config('app.url'), PHP_URL_HOST), | ||
$secure, | ||
true | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.