Skip to content

Commit 65d45be

Browse files
committed
minor symfony#14670 [Mercure] Update token generation (flovrent)
This PR was merged into the 5.2 branch. Discussion ---------- [Mercure] Update token generation Following the release of the version 4 of the `lcobucci/jwt` library, we get the following error `Cannot Instantiate Interface Lcobucci\JWT\Builder`, same thing with `Key`. Like you can see here : https://github.com/lcobucci/jwt/releases/tag/4.0.0, there are BC-break about `Key` and `Builder` who have become Interface. In accordance with the documentation on https://lcobucci-jwt.readthedocs.io/en/latest/configuration/#configuration, I allow myself to propose the following merge request. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `5.x` for features of unreleased versions). --> Commits ------- fb8dea5 Update mercure.rst
2 parents fc17ad3 + fb8dea5 commit 65d45be

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

mercure.rst

+9-6
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ And here is the controller::
408408
// src/Controller/DiscoverController.php
409409
namespace App\Controller;
410410

411-
use Lcobucci\JWT\Builder;
411+
use Lcobucci\JWT\Configuration;
412412
use Lcobucci\JWT\Signer\Hmac\Sha256;
413413
use Lcobucci\JWT\Signer\Key;
414414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -423,11 +423,14 @@ And here is the controller::
423423
{
424424
$hubUrl = $this->getParameter('mercure.default_hub');
425425
$this->addLink($request, new Link('mercure', $hubUrl));
426-
427-
$token = (new Builder())
428-
// set other appropriate JWT claims, such as an expiration date
429-
->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
430-
->getToken(new Sha256(), new Key($this->getParameter('mercure_secret_key'))); // don't forget to set this parameter! Test value: !ChangeMe!
426+
427+
$key = Key\InMemory::plainText('mercure_secret_key'); // don't forget to set this parameter! Test value: !ChangeMe!
428+
$configuration = Configuration::forSymmetricSigner(new Sha256(), $key);
429+
430+
$token = $configuration->builder()
431+
->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
432+
->getToken($configuration->signer(), $configuration->signingKey())
433+
->toString();
431434

432435
$response = $this->json(['@id' => '/demo/books/1', 'availability' => 'https://schema.org/InStock']);
433436
$cookie = Cookie::create('mercureAuthorization')

0 commit comments

Comments
 (0)