@@ -111,8 +111,16 @@ This token must be signed with the same secret key as the one used by the Hub to
111
111
This secret key must be stored in the ``MERCURE_JWT_SECRET `` environment variable.
112
112
MercureBundle will use it to automatically generate and sign the needed JWTs.
113
113
114
- If you don't want to use the provided environment variables,
115
- use the following configuration:
114
+ In addition to these environment variables,
115
+ MercureBundle provides a more advanced configuration configuration:
116
+
117
+ * ``secret ``: the key to use to sign the JWT
118
+ * ``publish ``: a list of topics to allow publishing to when generating the JWT
119
+ * ``subscribe ``: a list of topics to allow subscribing to when generating the JWT
120
+ * ``algorithm ``: The algorithm to use to sign the JWT
121
+ * ``provider ``: The ID of a service to call to provide the JWT
122
+ * ``factory ``: The ID of a service to call to create the JWT
123
+ * ``value ``: the raw JWT to use (all other options will be ignored)
116
124
117
125
.. configuration-block ::
118
126
@@ -125,6 +133,12 @@ use the following configuration:
125
133
url : https://mercure-hub.example.com/.well-known/mercure
126
134
jwt :
127
135
secret : ' !ChangeMe!'
136
+ publish : ['foo', 'https://example.com/foo']
137
+ subscribe : ['bar', 'https://example.com/bar']
138
+ algorithm : ' hmac.sha256'
139
+ provider : ' My\Provider'
140
+ factory : ' My\Factory'
141
+ value : ' my.jwt'
128
142
129
143
.. code-block :: xml
130
144
@@ -135,7 +149,18 @@ use the following configuration:
135
149
name =" default"
136
150
url =" https://mercure-hub.example.com/.well-known/mercure"
137
151
>
138
- <jwt secret =" !ChangeMe!" />
152
+ <jwt
153
+ secret =" !ChangeMe!"
154
+ algorithm =" hmac.sha256"
155
+ provider =" My\Provider"
156
+ factory =" My\Factory"
157
+ value =" my.jwt"
158
+ >
159
+ <publish >foo</publish >
160
+ <publish >https://example.com/foo</publish >
161
+ <subscribe >bar</subscribe >
162
+ <subscribe >https://example.com/bar</subscribe >
163
+ </jwt >
139
164
</hub >
140
165
</config >
141
166
@@ -148,68 +173,32 @@ use the following configuration:
148
173
'url' => 'https://mercure-hub.example.com/.well-known/mercure',
149
174
'jwt' => [
150
175
'secret' => '!ChangeMe!',
176
+ 'publish' => ['foo', 'https://example.com/foo'],
177
+ 'subscribe' => ['bar', 'https://example.com/bar'],
178
+ 'algorithm' => 'hmac.sha256',
179
+ 'provider' => 'My\Provider',
180
+ 'factory' => 'My\Factory',
181
+ 'value' => 'my.jwt',
151
182
],
152
183
],
153
184
],
154
185
]);
155
186
156
- Alternatively, it's also possible to pass directly the JWT to use:
157
-
158
- .. configuration-block ::
159
-
160
- .. code-block :: yaml
161
-
162
- # config/packages/mercure.yaml
163
- mercure :
164
- hubs :
165
- default :
166
- url : https://mercure-hub.example.com/.well-known/mercure
167
- jwt :
168
- value : ' the.JWT'
169
-
170
- .. code-block :: xml
171
-
172
- <!-- config/packages/mercure.xml -->
173
- <?xml version =" 1.0" encoding =" UTF-8" ?>
174
- <config >
175
- <hub
176
- name =" default"
177
- url =" https://mercure-hub.example.com/.well-known/mercure"
178
- >
179
- <jwt value =" the.JWT" />
180
- </hub >
181
- </config >
182
-
183
- .. code-block :: php
184
-
185
- // config/packages/mercure.php
186
- $container->loadFromExtension('mercure', [
187
- 'hubs' => [
188
- 'default' => [
189
- 'url' => 'https://mercure-hub.example.com/.well-known/mercure',
190
- 'jwt' => [
191
- 'value' => 'the.JWT',
192
- ],
193
- ],
194
- ],
195
- ]);
196
-
187
+ .. tip ::
197
188
198
- The JWT payload must contain at least the following structure to be allowed to
199
- publish:
189
+ The JWT payload must contain at least the following structure to be allowed to
190
+ publish:
200
191
201
- .. code-block :: json
192
+ .. code-block :: json
202
193
203
- {
204
- "mercure" : {
205
- "publish" : []
194
+ {
195
+ "mercure" : {
196
+ "publish" : []
197
+ }
206
198
}
207
- }
208
-
209
- Because the array is empty, the Symfony app will only be authorized to publish
210
- public updates (see the authorization _ section for further information).
211
199
212
- .. tip ::
200
+ Because the array is empty, the Symfony app will only be authorized to publish
201
+ public updates (see the authorization _ section for further information).
213
202
214
203
The jwt.io website is a convenient way to create and sign JWTs.
215
204
Checkout this `example JWT `_, that grants publishing rights for all *topics *
@@ -364,7 +353,7 @@ by using the ``AbstractController::addLink`` helper method::
364
353
{
365
354
public function discover(Request $request, Discovery $discovery): JsonResponse
366
355
{
367
- // Link: <http ://hub.example.com/.well-known/mercure>; rel="mercure"
356
+ // Link: <https ://hub.example.com/.well-known/mercure>; rel="mercure"
368
357
$discovery->addLink($request);
369
358
370
359
return $this->json([
@@ -380,7 +369,7 @@ and to subscribe to it:
380
369
.. code-block :: javascript
381
370
382
371
// Fetch the original resource served by the Symfony web API
383
- fetch (' /books/1' ) // Has Link: <http ://hub.example.com/.well-known/mercure>; rel="mercure"
372
+ fetch (' /books/1' ) // Has Link: <https ://hub.example.com/.well-known/mercure>; rel="mercure"
384
373
.then (response => {
385
374
// Extract the hub URL from the Link header
386
375
const hubUrl = response .headers .get (' Link' ).match (/ <([^ >] + )>;\s + rel=(?:mercure| "[^ "] * mercure[^ "] * ")/ )[1 ];
@@ -688,6 +677,10 @@ sent:
688
677
mercure.hub.default :
689
678
class : App\Tests\Functional\Stub\HubStub
690
679
680
+ As MercureBundle support multiple hubs, you may have to replace
681
+ the other service definitions accordingly.
682
+
683
+
691
684
.. tip ::
692
685
693
686
Symfony Panther has `a feature to test applications using Mercure `_.
0 commit comments