From 06fb99f2e5170c5c1443e773b35d122ea965fb48 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Wed, 15 Nov 2023 14:15:00 +0000 Subject: [PATCH] chore: update to support new AbstractController --- composer.json | 2 +- extend.php | 22 +++++++--------- src/Api/CurrentUserAttributes.php | 30 ++++++++++++++++++++++ src/Listeners/HandleLogout.php | 42 +++++++++++++++++++++++++++++++ src/Middleware/BindRequest.php | 26 +++++++++++++++++++ 5 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 src/Api/CurrentUserAttributes.php create mode 100644 src/Listeners/HandleLogout.php create mode 100644 src/Middleware/BindRequest.php diff --git a/composer.json b/composer.json index effe2c3..94fa284 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "omines/oauth2-gitlab": "^3.3.0", "wohali/oauth2-discord-new": "^1.2.1", "league/oauth2-linkedin": "^5.1.2", - "fof/extend": "^1.2.1" + "fof/extend": "^1.3.0" }, "replace": { "flarum/auth-facebook": "*", diff --git a/extend.php b/extend.php index dde8517..caca7b3 100644 --- a/extend.php +++ b/extend.php @@ -15,8 +15,7 @@ use Flarum\Api\Serializer\ForumSerializer; use Flarum\Extend; use Flarum\Frontend\Document; -use Flarum\User\User; -use FoF\Extend\Controllers\AbstractOAuthController; +use Flarum\User\Event\LoggedOut; use FoF\Extend\Events\OAuthLoginSuccessful; return [ @@ -34,7 +33,11 @@ new Extend\Locales(__DIR__.'/resources/locale'), (new Extend\Middleware('forum')) - ->add(Middleware\ErrorHandler::class), + ->add(Middleware\ErrorHandler::class) + ->add(Middleware\ServerRequestMiddleware::class), + + (new Extend\Middleware('api')) + ->add(Middleware\ServerRequestMiddleware::class), (new Extend\Routes('forum')) ->get('/auth/twitter', 'auth.twitter', Controllers\TwitterAuthController::class), @@ -68,16 +71,9 @@ ->serializeToForum('fof-oauth.fullscreenPopup', 'fof-oauth.fullscreenPopup', 'boolVal'), (new Extend\Event()) - ->listen(OAuthLoginSuccessful::class, Listeners\UpdateEmailFromProvider::class), + ->listen(OAuthLoginSuccessful::class, Listeners\UpdateEmailFromProvider::class) + ->listen(LoggedOut::class, Listeners\HandleLogout::class), (new Extend\ApiSerializer(CurrentUserSerializer::class)) - ->attributes(function (CurrentUserSerializer $serializer, User $user, array $attributes) { - $session = $serializer->getRequest()->getAttribute('session'); - - if ($session) { - $attributes['loginProvider'] = $session->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER); - } - - return $attributes; - }), + ->attributes(Api\CurrentUserAttributes::class), ]; diff --git a/src/Api/CurrentUserAttributes.php b/src/Api/CurrentUserAttributes.php new file mode 100644 index 0000000..4f93523 --- /dev/null +++ b/src/Api/CurrentUserAttributes.php @@ -0,0 +1,30 @@ +cache = $cache; + } + + public function __invoke(CurrentUserSerializer $serializer, User $user, array $attributes): array + { + $session = $serializer->getRequest()->getAttribute('session'); + + $attributes['loginProvider'] = $this->cache->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER . '_' . $session->getId()); + + return $attributes; + } +} diff --git a/src/Listeners/HandleLogout.php b/src/Listeners/HandleLogout.php new file mode 100644 index 0000000..bbbea51 --- /dev/null +++ b/src/Listeners/HandleLogout.php @@ -0,0 +1,42 @@ +cache = $cache; + } + + public function handle(LoggedOut $event) + { + $user = $event->user; + + /** @var ServerRequestInterface|null $request */ + $request = resolve('fof-oauth-request'); + + if ($request) { + $requestUser = RequestUtil::getActor($request); + + if ($requestUser->id === $user->id) { + /** @var Session $session */ + $session = $request->getAttribute('session'); + + $this->cache->forget(AbstractOAuthController::SESSION_OAUTH2PROVIDER . '_' . $session->getId()); + } + } + } +} diff --git a/src/Middleware/BindRequest.php b/src/Middleware/BindRequest.php new file mode 100644 index 0000000..b038699 --- /dev/null +++ b/src/Middleware/BindRequest.php @@ -0,0 +1,26 @@ +container = $container; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $this->container->instance('fof-oauth-request', $request); + + return $handler->handle($request); + } +}