Skip to content

Commit

Permalink
webfinger controller event pass request once (#739)
Browse files Browse the repository at this point in the history
pass the webfinger request from controller to WebfingerResponseEvent and
its handlers once rather than (implicitly) pulling the request from the
stack, also adjusted WebFingerParameters the same way to have it parse
the params out of the given request object

just to make it clearer what's the required input, rather than magically conjuring them out of thin air on use
  • Loading branch information
asdfzdfj authored May 11, 2024
1 parent c6b47b5 commit e218282
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Controller/ActivityPub/WebFingerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private readonly EventDispatcherInterface $eventDisp

public function __invoke(Request $request): JsonResponse
{
$event = new WebfingerResponseEvent(new JsonRd());
$event = new WebfingerResponseEvent(new JsonRd(), $request);
$this->eventDispatcher->dispatch($event);

if (!empty($event->jsonRd->getLinks())) {
Expand Down
7 changes: 5 additions & 2 deletions src/Event/ActivityPub/WebfingerResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace App\Event\ActivityPub;

use App\ActivityPub\JsonRd;
use Symfony\Component\HttpFoundation\Request;

class WebfingerResponseEvent
{
public function __construct(public JsonRd $jsonRd)
{
public function __construct(
public JsonRd $jsonRd,
public Request $request,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
use App\Service\ActivityPub\Webfinger\WebFingerParameters;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class GroupWebFingerProfileSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly WebFingerParameters $webfingerParameters,
private readonly MagazineRepository $magazineRepository,
private readonly UrlGeneratorInterface $urlGenerator,
Expand All @@ -34,13 +32,13 @@ public static function getSubscribedEvents(): array

public function buildResponse(WebfingerResponseEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
$params = $this->webfingerParameters->getParams();
$params = $this->webfingerParameters->getParams($event->request);
$jsonRd = $event->jsonRd;

if (isset($params[WebFingerParameters::ACCOUNT_KEY_NAME]) && $actor = $this->getActor(
$params[WebFingerParameters::ACCOUNT_KEY_NAME]
)) {
if (
isset($params[WebFingerParameters::ACCOUNT_KEY_NAME])
&& $actor = $this->getActor($params[WebFingerParameters::ACCOUNT_KEY_NAME])
) {
$accountHref = $this->urlGenerator->generate(
'ap_magazine',
['name' => $actor->name],
Expand Down
13 changes: 6 additions & 7 deletions src/EventSubscriber/ActivityPub/GroupWebFingerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
use App\Service\ActivityPub\Webfinger\WebFingerParameters;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class GroupWebFingerSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly WebFingerParameters $webfingerParameters,
private readonly MagazineRepository $magazineRepository,
private readonly UrlGeneratorInterface $urlGenerator
Expand All @@ -34,18 +32,19 @@ public static function getSubscribedEvents(): array

public function buildResponse(WebfingerResponseEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
$params = $this->webfingerParameters->getParams();
$request = $event->request;
$params = $this->webfingerParameters->getParams($request);
$jsonRd = $event->jsonRd;

$subject = $request->query->get('resource') ?: '';
if (!empty($subject)) {
$jsonRd->setSubject($subject);
}

if (isset($params[WebFingerParameters::ACCOUNT_KEY_NAME]) && $actor = $this->getActor(
$params[WebFingerParameters::ACCOUNT_KEY_NAME]
)) {
if (
isset($params[WebFingerParameters::ACCOUNT_KEY_NAME])
&& $actor = $this->getActor($params[WebFingerParameters::ACCOUNT_KEY_NAME])
) {
$accountHref = $this->urlGenerator->generate(
'ap_magazine',
['name' => $actor->name],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use JetBrains\PhpStorm\ArrayShape;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserWebFingerProfileSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly WebFingerParameters $webfingerParameters,
private readonly UserRepository $userRepository,
private readonly UrlGeneratorInterface $urlGenerator,
Expand All @@ -40,8 +38,7 @@ public static function getSubscribedEvents(): array

public function buildResponse(WebfingerResponseEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
$params = $this->webfingerParameters->getParams();
$params = $this->webfingerParameters->getParams($event->request);
$jsonRd = $event->jsonRd;

if (isset($params[WebFingerParameters::ACCOUNT_KEY_NAME])) {
Expand Down
13 changes: 6 additions & 7 deletions src/EventSubscriber/ActivityPub/UserWebFingerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use App\Service\ActivityPub\Webfinger\WebFingerParameters;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserWebFingerSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly WebFingerParameters $webfingerParameters,
private readonly UserRepository $userRepository,
private readonly UrlGeneratorInterface $urlGenerator
Expand All @@ -34,18 +32,19 @@ public static function getSubscribedEvents(): array

public function buildResponse(WebfingerResponseEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
$params = $this->webfingerParameters->getParams();
$request = $event->request;
$params = $this->webfingerParameters->getParams($request);
$jsonRd = $event->jsonRd;

$subject = $request->query->get('resource') ?: '';
if (!empty($subject)) {
$jsonRd->setSubject($subject);
}

if (isset($params[WebFingerParameters::ACCOUNT_KEY_NAME]) && $actor = $this->getActor(
$params[WebFingerParameters::ACCOUNT_KEY_NAME]
)) {
if (
isset($params[WebFingerParameters::ACCOUNT_KEY_NAME])
&& $actor = $this->getActor($params[WebFingerParameters::ACCOUNT_KEY_NAME])
) {
$accountHref = $this->urlGenerator->generate(
'ap_user',
['username' => $actor->getUserIdentifier()],
Expand Down
11 changes: 6 additions & 5 deletions src/Service/ActivityPub/Webfinger/WebFingerParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@

namespace App\Service\ActivityPub\Webfinger;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;

class WebFingerParameters
{
public const REL_KEY_NAME = 'rel';
public const HOST_KEY_NAME = 'host';
public const ACCOUNT_KEY_NAME = 'account';

public function __construct(private readonly RequestStack $requestStack)
public function __construct()
{
}

public function getParams(): array
/**
* @return array{acccount?: string, host?: string, rel?: array}
*/
public function getParams(Request $request): array
{
$params = [];

$request = $this->requestStack->getCurrentRequest();

if ($resource = $request->query->get('resource')) {
$host = $request->server->get('HTTP_HOST'); // @todo

Expand Down

0 comments on commit e218282

Please sign in to comment.