Skip to content

Commit 2997a41

Browse files
authored
feat: Add iri and urlencode functions to mercure (#1360)
1 parent bd5f179 commit 2997a41

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

core/mercure.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,103 @@ In addition to `private`, the following options are available:
127127
* `type`: the SSE type of this event, if not set this field is omitted
128128
* `retry`: the `retry` field of the SSE, if not set this field is omitted
129129
* `normalization_context`: the specific normalization context to use for the update.
130+
131+
## Dispatching Restrictive Updates (Security Mode)
132+
133+
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber with `topic_selector` to receive only publications that are authorized (partner match).
134+
135+
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber can use the topic selector <https://example.com/books/{id}> as value of the topic query parameter.
136+
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is only authorized to access some of these resources.
137+
>
138+
> To solve this problem, the mercure.subscribe claim could contain a topic selector such as: <https://example.com/users/foo/{?topic}>.
139+
>
140+
> The publisher could then take advantage of the previously described behavior by publishing a private update having <https://example.com/books/1> as canonical topic and <https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1> as alternate topic.
141+
>
142+
> <https://mercure.rocks/spec#subscribers>
143+
144+
Below is an example using the `topics` option:
145+
146+
```php
147+
<?php
148+
// api/src/Entity/Book.php
149+
150+
namespace App\Entity;
151+
152+
use ApiPlatform\Core\Annotation\ApiResource;
153+
use ApiPlatform\Core\Api\UrlGeneratorInterface;
154+
use App\Entity\User;
155+
156+
#[ApiResource(
157+
mercure: [
158+
'private' => true,
159+
// the '@=' prefix is required when using expressions for arguments in topics
160+
'topics' => [
161+
'@=iri(object)',
162+
'@=iri(object.getOwner()) ~ "/?topic=" ~ escape(iri(object))',
163+
'@=iri(object, '.UrlGeneratorInterface::ABS_PATH.')', // you can also change the reference type
164+
'https://example.com/books/1',
165+
],
166+
],
167+
)]
168+
class Book
169+
{
170+
private ?User $owner;
171+
172+
public function getOwner(): ?User
173+
{
174+
return $this->owner;
175+
}
176+
}
177+
```
178+
179+
Using an *expression* function:
180+
181+
```php
182+
<?php
183+
// api/src/Entity/Book.php
184+
185+
namespace App\Entity;
186+
187+
use ApiPlatform\Core\Annotation\ApiResource;
188+
use App\Entity\User;
189+
190+
#[ApiResource(
191+
mercure: 'object.getMercureOptions()',
192+
)]
193+
class Book
194+
{
195+
private ?User $owner;
196+
197+
public function getMercureOptions(): array
198+
{
199+
// the '@=' prefix is required when using expressions for arguments in topics
200+
$topic1 = '@=iri(object)';
201+
$topic2 = '@=iri(object.getOwner()) ~ "/?topic=" ~ escape(iri(object))';
202+
$topic3 = '@=iri(object, '.UrlGeneratorInterface::ABS_PATH.')'; // you can also change the reference type
203+
$topic4 = 'https://example.com/books/1';
204+
205+
return [
206+
'private' => true,
207+
'topics' => [$topic1, $topic2, $topic3, $topic4],
208+
];
209+
}
210+
211+
public function getOwner(): ?User
212+
{
213+
return $this->owner;
214+
}
215+
}
216+
```
217+
218+
In this case, the JWT Token for the subscriber should contain:
219+
220+
```json
221+
{
222+
"mercure": {
223+
"subscribe": ["https://example.com/users/foo/{?topic}"]
224+
}
225+
}
226+
```
227+
228+
The subscribe topic should be:
229+
`https://example.com/books/{id}`

0 commit comments

Comments
 (0)