8000 Merge branch '4.4' into 5.0 · symfony/symfony-docs@be15e8d · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be15e8d

Browse files
committed
Merge branch '4.4' into 5.0
* 4.4: [Mercure] Compatibility with v0.10
2 parents f5a183c + 57fdc9f commit be15e8d

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

mercure.rst

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ has `high-level implementations`_ in many programming languages.
2727

2828
Mercure comes with an authorization mechanism,
2929
automatic re-connection in case of network issues
30-
with retrieving of lost updates, "connection-less" push for smartphones and
31-
auto-discoverability (a supported client can automatically discover and
32-
subscribe to updates of a given resource thanks to a specific HTTP header).
30+
with retrieving of lost updates, a presence API,
31+
"connection-less" push for smartphones and auto-discoverability (a supported
32+
client can automatically discover and subscribe to updates of a given resource
33+
thanks to a specific HTTP header).
3334

3435
All these features are supported in the Symfony integration.
3536

@@ -71,7 +72,7 @@ Run the following command to start it:
7172

7273
.. code-block:: terminal
7374
74-
$ ./mercure --jwt-key='aVerySecretKey' --addr='localhost:3000' --allow-anonymous --cors-allowed-origins='*'
75+
$ ./mercure --jwt-key='!ChangeMe!' --addr='localhost:3000' --allow-anonymous --cors-allowed-origins='*'
7576
7677
.. note::
7778

@@ -102,7 +103,7 @@ to the Mercure Hub to be authorized to publish updates.
102103
This JWT should be stored in the ``MERCURE_JWT_TOKEN`` environment variable.
103104

104105
The JWT must be signed with the same secret key as the one used by
105-
the Hub to verify the JWT (``aVerySecretKey`` in our example).
106+
the Hub to verify the JWT (``!ChangeMe!`` in our example).
106107
Its payload must contain at least the following structure to be allowed to
107108
publish:
108109

@@ -120,7 +121,7 @@ public updates (see the authorization_ section for further information).
120121
.. tip::
121122

122123
The jwt.io website is a convenient way to create and sign JWTs.
123-
Checkout this `example JWT`_, that grants publishing rights for all *targets*
124+
Checkout this `example JWT`_, that grants publishing rights for all *topics*
124125
(notice the star in the array).
125126
Don't forget to set your secret key properly in the bottom of the right panel of the form!
126127

@@ -196,7 +197,8 @@ Subscribing to updates in JavaScript is straightforward:
196197
}
197198
198199
Mercure also allows to subscribe to several topics,
199-
and to use URI Templates as patterns:
200+
and to use URI Templates or the special value ``*`` (matched by all topics)
201+
as patterns:
200202

201203
.. code-block:: javascript
202204
@@ -329,8 +331,8 @@ Authorization
329331
-------------
330332

331333
Mercure also allows to dispatch updates only to authorized clients.
332-
To do so, set the list of **targets** allowed to receive the update
333-
as the third parameter of the ``Update`` constructor::
334+
To do so, mark the update as **private** by setting the third parameter
335+
of the ``Update`` constructor to ``true``::
334336

335337
// src/Controller/Publish.php
336338
namespace App\Controller;
@@ -346,19 +348,19 @@ as the third parameter of the ``Update`` constructor::
346348
$update = new Update(
347349
'http://example.com/books/1',
348350
json_encode(['status' => 'OutOfStock']),
349-
['http://example.com/user/kevin', 'http://example.com/groups/admin'] // Here are the targets
351+
true // private
350352
);
351353

352-
// Publisher's JWT must contain all of these targets or * in mercure.publish or you'll get a 401
353-
// Subscriber's JWT must contain at least one of these targets or * in mercure.subscribe to receive the update
354+
// Publisher's JWT must contain this topic, a URI template it matches or * in mercure.publish or you'll get a 401
355+
// Subscriber's JWT must contain this topic, a URI template it matches or or * in mercure.subscribe to receive the update
354356
$publisher($update);
355357

356-
return new Response('published to the selected targets!');
358+
return new Response('private update published!');
357359
}
358360
}
359361

360-
To subscribe to private updates, subscribers must provide
361-
a JWT containing at least one target marking the update to the Hub.
362+
To subscribe to private updates, subscribers must provide to the Hub
363+
a JWT containing containing a topic selector matching by the update's topic.
362364

363365
To provide this JWT, the subscriber can use a cookie,
364366
or a ``Authorization`` HTTP header.
@@ -380,9 +382,9 @@ If the client is not a web browser, then using an authorization header is the wa
380382
});
381383
382384
In the following example controller,
383-
the generated cookie contains a JWT, itself containing the appropriate targets.
385+
the generated cookie contains a JWT, itself containing the appropriate topic selector.
384386
This cookie will be automatically sent by the web browser when connecting to the Hub.
385-
Then, the Hub will verify the validity of the provided JWT, and extract the targets
387+
Then, the Hub will verify the validity of the provided JWT, and extract the topic selectors
386388
from it.
387389

388390
To generate the JWT, we'll use the ``lcobucci/jwt`` library. Install it:
@@ -414,8 +416,8 @@ And here is the controller::
414416
$username = $this->getUser()->getUsername(); // Retrieve the username of the current user
415417
$token = (new Builder())
416418
// set other appropriate JWT claims, such as an expiration date
417-
->withClaim('mercure', ['subscribe' => ["http://example.com/user/$username"]]) // could also include the security roles, or anything else
418-
->getToken(new Sha256(), new Key($this->getParameter('mercure_secret_key'))); // don't forget to set this parameter! Test value: aVerySecretKey
419+
->withClaim('mercure', ['subscribe' => ["http://example.com/books/1"]]) // can also be a URI template, or *
420+
->getToken(new Sha256(), new Key($this->getParameter('mercure_secret_key'))); // don't forget to set this parameter! Test value: !ChangeMe!
419421

420422
$response = $this->json(['@id' => '/demo/books/1', 'availability' => 'https://schema.org/InStock']);
421423
$response->headers->set(

0 commit comments

Comments
 (0)
0