Skip to content

Commit

Permalink
Practice 5: Authorization with HTTP header
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGonzalez committed May 7, 2024
1 parent b565614 commit 3c444ef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
15 changes: 10 additions & 5 deletions public/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const dicoverMercureHub = async () => fetch(window.location)
.get('Link')
.match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1];

return new URL(hubUrl);
return {
url: new URL(hubUrl),
token: response.headers.get('X-Mercure-JWT'),
}
}
)

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

document.addEventListener('DOMContentLoaded', async () => {
const hubUrl = await dicoverMercureHub();
const { url, token } = await dicoverMercureHub()

hubUrl.searchParams.append('topic', 'http://localhost/activity')
hubUrl.searchParams.append('topic', 'http://localhost/dinosaurs')
url.searchParams.append('topic', 'http://localhost/activity')
url.searchParams.append('topic', 'http://localhost/dinosaurs')

const es = new EventSource(hubUrl, { withCredentials: true });
const options = token ? { headers: { Authorization: `Bearer ${token}` }, withCredentials: true } : {}

const es = new EventSourcePolyfill(url, options)

es.addEventListener('login', e => {
const message = JSON.parse(e.data)
Expand Down
13 changes: 9 additions & 4 deletions public/js/dinosaurs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const dicoverMercureHub = async () => fetch(window.location)
.get('Link')
.match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1];

return new URL(hubUrl);
return {
url: new URL(hubUrl),
token: response.headers.get('X-Mercure-JWT'),
}
}
)

Expand Down Expand Up @@ -57,11 +60,13 @@ const removeDinosaur = (id, message) => {
}

document.addEventListener('DOMContentLoaded', async () => {
const hubUrl = await dicoverMercureHub();
const { url, token } = await dicoverMercureHub();

hubUrl.searchParams.append('topic', 'http://localhost/dinosaurs')
url.searchParams.append('topic', 'http://localhost/dinosaurs')

const es = new EventSource(hubUrl, { withCredentials: true });
const options = token ? { headers: { Authorization: `Bearer ${token}` }, withCredentials: true } : {}

const es = new EventSourcePolyfill(url, options);

es.addEventListener('created', e => {
const message = JSON.parse(e.data)
Expand Down
6 changes: 6 additions & 0 deletions public/js/eventsource.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/Listener/MercureAuthorizationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Mercure\Authorization;
use Symfony\Component\Mercure\HubInterface;

#[AsEventListener(event: ResponseEvent::class)]
final readonly class MercureAuthorizationListener
{
public function __construct(
private Security $security,
private Authorization $authorization
private HubInterface $hubInterface
) {
}

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

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

$this->authorization->setCookie($request, $topics);
// Generate the JWT token
$JWTfactory = $this->hubInterface->getFactory();

if (null === $JWTfactory) {
throw new \RuntimeException('The hub factory is not available.');
}

$token = $JWTfactory->create($topics);

$response->headers->set('X-Mercure-JWT', $token);
}
}
1 change: 1 addition & 0 deletions templates/activity.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% block title %}Activity panel{% endblock %}

{% block javascripts %}
<script src="{{ asset('js/eventsource.min.js') }}" defer></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<script src="{{ asset('js/activity.js') }}" defer></script>
{% endblock %}
Expand Down
1 change: 1 addition & 0 deletions templates/dinosaurs-list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{% endblock %}

{% block javascripts %}
<script src="{{ asset('js/eventsource.min.js') }}" defer></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<script src="{{ asset('js/dinosaurs.js') }}" defer></script>
{% endblock %}
Expand Down

0 comments on commit 3c444ef

Please sign in to comment.