-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Practice 4: Authorization with Cookie
- Loading branch information
1 parent
7184f98
commit b565614
Showing
16 changed files
with
256 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const activityContainer = document.querySelector('#activity-container') | ||
|
||
|
||
const dicoverMercureHub = async () => fetch(window.location) | ||
.then(response => { | ||
const hubUrl = response | ||
.headers | ||
.get('Link') | ||
.match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1]; | ||
|
||
return new URL(hubUrl); | ||
} | ||
) | ||
|
||
const addLogItem = (message) => { | ||
const item = document.createElement('li') | ||
|
||
item.setAttribute('class', 'alert alert-primary') | ||
item.setAttribute('role', 'alert') | ||
|
||
item.innerHTML = message | ||
|
||
activityContainer.append(item) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', async () => { | ||
const hubUrl = await dicoverMercureHub(); | ||
|
||
hubUrl.searchParams.append('topic', 'http://localhost/activity') | ||
hubUrl.searchParams.append('topic', 'http://localhost/dinosaurs') | ||
|
||
const es = new EventSource(hubUrl, { withCredentials: true }); | ||
|
||
es.addEventListener('login', e => { | ||
const message = JSON.parse(e.data) | ||
|
||
addLogItem(message.message) | ||
}) | ||
|
||
es.addEventListener('logout', e => { | ||
const message = JSON.parse(e.data) | ||
|
||
addLogItem(message.message) | ||
}) | ||
|
||
es.addEventListener('created', e => { | ||
const message = JSON.parse(e.data) | ||
|
||
addLogItem(message.message) | ||
}) | ||
|
||
es.addEventListener('updated', e => { | ||
const message = JSON.parse(e.data) | ||
|
||
addLogItem(message.message) | ||
}) | ||
|
||
es.addEventListener('deleted', e => { | ||
const message = JSON.parse(e.data) | ||
|
||
addLogItem(message.message) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
final class ActivityController extends AbstractController | ||
{ | ||
#[Route('/activity', name: 'app_activity')] | ||
public function activity(): Response | ||
{ | ||
return $this->render('activity.html.twig'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Listener; | ||
|
||
use App\Realtime\Trigger\UserLoggetIn; | ||
use App\Realtime\Trigger\UserLoggetOut; | ||
use App\Service\Realtime\Publisher; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\Security\Http\Event\LoginSuccessEvent; | ||
use Symfony\Component\Security\Http\Event\LogoutEvent; | ||
|
||
final readonly class AuthenticationListener | ||
{ | ||
public function __construct( | ||
private Publisher $publisher | ||
) { | ||
} | ||
|
||
#[AsEventListener(event: LoginSuccessEvent::class)] | ||
public function onLoginSuccess(LoginSuccessEvent $event): void | ||
{ | ||
$identifier = $event->getAuthenticatedToken()->getUserIdentifier(); | ||
|
||
$this->publisher->publish(new UserLoggetIn($identifier)); | ||
} | ||
|
||
#[AsEventListener(event: LogoutEvent::class)] | ||
public function onLogout(LogoutEvent $event): void | ||
{ | ||
$identifier = $event->getToken()->getUserIdentifier(); | ||
|
||
$this->publisher->publish(new UserLoggetOut($identifier)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Listener; | ||
|
||
use Symfony\Bundle\SecurityBundle\Security; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\Mercure\Authorization; | ||
|
||
#[AsEventListener(event: ResponseEvent::class)] | ||
final readonly class MercureAuthorizationListener | ||
{ | ||
public function __construct( | ||
private Security $security, | ||
private Authorization $authorization | ||
) { | ||
} | ||
|
||
public function onKernelResponse(ResponseEvent $event): void | ||
{ | ||
$topics = []; | ||
|
||
if ($this->security->isGranted('ROLE_USER')) { | ||
$topics[] = 'http://localhost/dinosaurs'; | ||
} | ||
|
||
if ($this->security->isGranted('ROLE_ADMIN')) { | ||
$topics[] = 'http://localhost/activity'; | ||
} | ||
|
||
if (empty($topics)) { | ||
return; | ||
} | ||
|
||
$request = $event->getRequest(); | ||
|
||
$this->authorization->setCookie($request, $topics); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Realtime\Trigger; | ||
|
||
use App\Realtime\Trigger; | ||
|
||
final readonly class UserLoggetIn extends Trigger | ||
{ | ||
public function __construct(string $userIdentifier) | ||
{ | ||
parent::__construct( | ||
'login', | ||
['http://localhost/activity'], | ||
[ | ||
'userIdentifier' => $userIdentifier, | ||
'message' => "The user {$userIdentifier} has been logged in !" | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Realtime\Trigger; | ||
|
||
use App\Realtime\Trigger; | ||
|
||
final readonly class UserLoggetOut extends Trigger | ||
{ | ||
public function __construct(string $userIdentifier) | ||
{ | ||
parent::__construct( | ||
'logout', | ||
['http://localhost/activity'], | ||
[ | ||
'userIdentifier' => $userIdentifier, | ||
'message' => "The user {$userIdentifier} has been logged out !" | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block title %}Activity panel{% endblock %} | ||
|
||
{% block javascripts %} | ||
<script src="{{ asset('js/bootstrap.min.js') }}"></script> | ||
<script src="{{ asset('js/activity.js') }}" defer></script> | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<main class="w-100 p-3"> | ||
<h1>Activity Panel</h1> | ||
|
||
<ul id="activity-container"> | ||
{# Here will go the realtime activity messages #} | ||
</ul> | ||
</main> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters