Skip to content

[Mercure] Update token generation #14670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Dec 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions mercure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ And here is the controller::
// src/Controller/DiscoverController.php
namespace App\Controller;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -423,11 +423,14 @@ And here is the controller::
{
$hubUrl = $this->getParameter('mercure.default_hub');
$this->addLink($request, new Link('mercure', $hubUrl));

$token = (new Builder())
// set other appropriate JWT claims, such as an expiration date
->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
->getToken(new Sha256(), new Key($this->getParameter('mercure_secret_key'))); // don't forget to set this parameter! Test value: !ChangeMe!

$key = Key\InMemory::plainText('mercure_secret_key'); // don't forget to set this parameter! Test value: !ChangeMe!
$configuration = Configuration::forSymmetricSigner(new Sha256(), $key);

$token = $configuration->builder()
->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
->getToken($configuration->signer(), $configuration->signingKey())
->toString();

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