Skip to content

Commit

Permalink
chore: update to support new AbstractController
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 15, 2023
1 parent 7444520 commit 06fb99f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
22 changes: 9 additions & 13 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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),
Expand Down Expand Up @@ -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),
];
30 changes: 30 additions & 0 deletions src/Api/CurrentUserAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace FoF\OAuth\Api;

use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\User\User;
use FoF\Extend\Controllers\AbstractOAuthController;
use Illuminate\Contracts\Cache\Store as Cache;

class CurrentUserAttributes
{
/**
* @var Cache
*/
protected $cache;

public function __construct(Cache $cache)
{
$this->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;
}
}
42 changes: 42 additions & 0 deletions src/Listeners/HandleLogout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace FoF\OAuth\Listeners;

use Flarum\Http\RequestUtil;
use Flarum\User\Event\LoggedOut;
use FoF\Extend\Controllers\AbstractOAuthController;
use Illuminate\Contracts\Cache\Store as Cache;
use Illuminate\Session\Store as Session;
use Psr\Http\Message\ServerRequestInterface;

class HandleLogout
{
/**
* @var Cache
*/
protected $cache;

public function __construct(Cache $cache)
{
$this->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());
}
}
}
}
26 changes: 26 additions & 0 deletions src/Middleware/BindRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace FoF\OAuth\Middleware;

use Illuminate\Contracts\Container\Container;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ServerRequestMiddleware implements MiddlewareInterface
{
protected $container;

public function __construct(Container $container)
{
$this->container = $container;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->container->instance('fof-oauth-request', $request);

return $handler->handle($request);
}
}

0 comments on commit 06fb99f

Please sign in to comment.