@@ -27,9 +27,10 @@ has `high-level implementations`_ in many programming languages.
27
27
28
28
Mercure comes with an authorization mechanism,
29
29
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).
33
34
34
35
All these features are supported in the Symfony integration.
35
36
@@ -71,7 +72,7 @@ Run the following command to start it:
71
72
72
73
.. code-block :: terminal
73
74
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='*'
75
76
76
77
.. note ::
77
78
@@ -102,7 +103,7 @@ to the Mercure Hub to be authorized to publish updates.
102
103
This JWT should be stored in the ``MERCURE_JWT_TOKEN `` environment variable.
103
104
104
105
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).
106
107
Its payload must contain at least the following structure to be allowed to
107
108
publish:
108
109
@@ -120,7 +121,7 @@ public updates (see the authorization_ section for further information).
120
121
.. tip ::
121
122
122
123
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 *
124
125
(notice the star in the array).
125
126
Don't forget to set your secret key properly in the bottom of the right panel of the form!
126
127
@@ -196,7 +197,8 @@ Subscribing to updates in JavaScript is straightforward:
196
197
}
197
198
198
199
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:
200
202
201
203
.. code-block :: javascript
202
204
@@ -329,8 +331,8 @@ Authorization
329
331
-------------
330
332
331
333
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 `` ::
334
336
335
337
// src/Controller/Publish.php
336
338
namespace App\Controller;
@@ -346,19 +348,19 @@ as the third parameter of the ``Update`` constructor::
346
348
$update = new Update(
347
349
'http://example.com/books/1',
348
350
json_encode(['status' => 'OutOfStock']),
349
- ['http://example.com/user/kevin', 'http://example.com/groups/admin'] // Here are the targets
351
+ true // private
350
352
);
351
353
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
354
356
$publisher($update);
355
357
356
- return new Response('published to the selected targets !');
358
+ return new Response('private update published !');
357
359
}
358
360
}
359
361
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 .
362
364
363
365
To provide this JWT, the subscriber can use a cookie,
364
366
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
380
382
});
381
383
382
384
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 .
384
386
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
386
388
from it.
387
389
388
390
To generate the JWT, we'll use the ``lcobucci/jwt `` library. Install it:
@@ -414,8 +416,8 @@ And here is the controller::
414
416
$username = $this->getUser()->getUsername(); // Retrieve the username of the current user
415
417
$token = (new Builder())
416
418
// 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!
419
421
420
422
$response = $this->json(['@id' => '/demo/books/1', 'availability' => 'https://schema.org/InStock']);
421
423
$response->headers->set(
0 commit comments