Skip to content

Commit 3c444ef

Browse files
Practice 5: Authorization with HTTP header
1 parent b565614 commit 3c444ef

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

public/js/activity.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const dicoverMercureHub = async () => fetch(window.location)
88
.get('Link')
99
.match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1];
1010

11-
return new URL(hubUrl);
11+
return {
12+
url: new URL(hubUrl),
13+
token: response.headers.get('X-Mercure-JWT'),
14+
}
1215
}
1316
)
1417

@@ -24,12 +27,14 @@ const addLogItem = (message) => {
2427
}
2528

2629
document.addEventListener('DOMContentLoaded', async () => {
27-
const hubUrl = await dicoverMercureHub();
30+
const { url, token } = await dicoverMercureHub()
2831

29-
hubUrl.searchParams.append('topic', 'http://localhost/activity')
30-
hubUrl.searchParams.append('topic', 'http://localhost/dinosaurs')
32+
url.searchParams.append('topic', 'http://localhost/activity')
33+
url.searchParams.append('topic', 'http://localhost/dinosaurs')
3134

32-
const es = new EventSource(hubUrl, { withCredentials: true });
35+
const options = token ? { headers: { Authorization: `Bearer ${token}` }, withCredentials: true } : {}
36+
37+
const es = new EventSourcePolyfill(url, options)
3338

3439
es.addEventListener('login', e => {
3540
const message = JSON.parse(e.data)

public/js/dinosaurs.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const dicoverMercureHub = async () => fetch(window.location)
99
.get('Link')
1010
.match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1];
1111

12-
return new URL(hubUrl);
12+
return {
13+
url: new URL(hubUrl),
14+
token: response.headers.get('X-Mercure-JWT'),
15+
}
1316
}
1417
)
1518

@@ -57,11 +60,13 @@ const removeDinosaur = (id, message) => {
5760
}
5861

5962
document.addEventListener('DOMContentLoaded', async () => {
60-
const hubUrl = await dicoverMercureHub();
63+
const { url, token } = await dicoverMercureHub();
6164

62-
hubUrl.searchParams.append('topic', 'http://localhost/dinosaurs')
65+
url.searchParams.append('topic', 'http://localhost/dinosaurs')
6366

64-
const es = new EventSource(hubUrl, { withCredentials: true });
67+
const options = token ? { headers: { Authorization: `Bearer ${token}` }, withCredentials: true } : {}
68+
69+
const es = new EventSourcePolyfill(url, options);
6570

6671
es.addEventListener('created', e => {
6772
const message = JSON.parse(e.data)

public/js/eventsource.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Listener/MercureAuthorizationListener.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Symfony\Bundle\SecurityBundle\Security;
88
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
99
use Symfony\Component\HttpKernel\Event\ResponseEvent;
10-
use Symfony\Component\Mercure\Authorization;
10+
use Symfony\Component\Mercure\HubInterface;
1111

1212
#[AsEventListener(event: ResponseEvent::class)]
1313
final readonly class MercureAuthorizationListener
1414
{
1515
public function __construct(
1616
private Security $security,
17-
private Authorization $authorization
17+
private HubInterface $hubInterface
1818
) {
1919
}
2020

@@ -34,8 +34,17 @@ public function onKernelResponse(ResponseEvent $event): void
3434
return;
3535
}
3636

37-
$request = $event->getRequest();
37+
$response = $event->getResponse();
3838

39-
$this->authorization->setCookie($request, $topics);
39+
// Generate the JWT token
40+
$JWTfactory = $this->hubInterface->getFactory();
41+
42+
if (null === $JWTfactory) {
43+
throw new \RuntimeException('The hub factory is not available.');
44+
}
45+
46+
$token = $JWTfactory->create($topics);
47+
48+
$response->headers->set('X-Mercure-JWT', $token);
4049
}
4150
}

templates/activity.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% block title %}Activity panel{% endblock %}
44

55
{% block javascripts %}
6+
<script src="{{ asset('js/eventsource.min.js') }}" defer></script>
67
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
78
<script src="{{ asset('js/activity.js') }}" defer></script>
89
{% endblock %}

templates/dinosaurs-list.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
{% endblock %}
99

1010
{% block javascripts %}
11+
<script src="{{ asset('js/eventsource.min.js') }}" defer></script>
1112
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
1213
<script src="{{ asset('js/dinosaurs.js') }}" defer></script>
1314
{% endblock %}

0 commit comments

Comments
 (0)