diff --git a/src/ActivityPub.php b/src/ActivityPub.php index 71093a7..b12c4af 100644 --- a/src/ActivityPub.php +++ b/src/ActivityPub.php @@ -46,8 +46,8 @@ class ActivityPub } $dispatcher = new EventDispatcher(); - $signatureListener = $this->module->get( 'signatureListener' ); - $dispatcher->addSubscriber( $signatureListener ); + $dispatcher->addSubscriber( $this->module->get( 'authListener' ) ); + $dispatcher->addSubscriber( $this->module->get( 'signatureListener' ) ); $dispatcher->addSubscriber( new ExceptionListener() ); $controllerResolver = new ControllerResolver(); diff --git a/src/Auth/AuthListener.php b/src/Auth/AuthListener.php new file mode 100644 index 0000000..823b30e --- /dev/null +++ b/src/Auth/AuthListener.php @@ -0,0 +1,55 @@ + 'checkAuth' + ); + } + + /** + * Constructs a new AuthenticationService + * + * @param Callable $authFunction A Callable that should accept + * + */ + public function __construct( Callable $authFunction ) + { + $this->authFunction = $authFunction; + } + + public function checkAuth( GetResponseEvent $event ) + { + $request = $event->getRequest(); + if ( $request->attributes->has( 'actor' ) ) { + return; + } + $actorId = call_user_func( $this->authFunction ); + if ( $actorId && ! empty( $actorId ) ) { + $request->attributes->set( 'actor', $actorId ); + } + } +} +?> diff --git a/src/Auth/AuthenticationService.php b/src/Auth/AuthenticationService.php deleted file mode 100644 index 3e68677..0000000 --- a/src/Auth/AuthenticationService.php +++ /dev/null @@ -1,32 +0,0 @@ -authFunction = $authFunction; - } -} -?> diff --git a/src/Config/ActivityPubModule.php b/src/Config/ActivityPubModule.php index f30281d..0a8dffd 100644 --- a/src/Config/ActivityPubModule.php +++ b/src/Config/ActivityPubModule.php @@ -1,6 +1,8 @@ false, 'dbPrefix' => '', + 'authFunction' => function() { + return false; + }, ); $options = array_merge( $defaults, $options ); $this->validateOptions( $options ); @@ -59,6 +64,9 @@ class ActivityPubModule $this->injector->register( 'signatureListener', SignatureListener::class ) ->addArgument( new Reference( 'httpSignatureService' ) ) ->addArgument( new Reference( 'objectsService' ) ); + + $this->injector->register( 'authListener', AuthListener::class ) + ->addArgument( $options['authFunction'] ); } /** diff --git a/test/Auth/AuthListenerTest.php b/test/Auth/AuthListenerTest.php new file mode 100644 index 0000000..a98ebeb --- /dev/null +++ b/test/Auth/AuthListenerTest.php @@ -0,0 +1,71 @@ +createMock( HttpKernelInterface::class ); + $request = Request::create( 'https://example.com/foo', Request::METHOD_GET ); + return new GetResponseEvent( + $kernel, $request, HttpKernelInterface::MASTER_REQUEST + ); + } + + public function testAuthListener() + { + $testCases = array( + array( + 'id' => 'basicTest', + 'authFunction' => function() { + return 'https://example.com/actor/1'; + }, + 'expectedAttributes' => array( + 'actor' => 'https://example.com/actor/1', + ), + ), + array( + 'id' => 'existingActorTest', + 'authFunction' => function() { + return 'https://example.com/actor/1'; + }, + 'requestAttributes' => array( + 'actor' => 'https://example.com/actor/2', + ), + 'expectedAttributes' => array( + 'actor' => 'https://example.com/actor/2', + ), + ), + array( + 'id' => 'defaultAuthTest', + 'authFunction' => function() { + return false; + }, + 'expectedAttributes' => array(), + ), + ); + foreach ( $testCases as $testCase ) { + $event = $this->getEvent(); + if ( array_key_exists( 'requestAttributes', $testCase ) ) { + foreach ( $testCase['requestAttributes'] as $attribute => $value ) { + $event->getRequest()->attributes->set( $attribute, $value ); + } + } + $authListener = new AuthListener( $testCase['authFunction'] ); + $authListener->checkAuth( $event ); + $this->assertEquals( + $testCase['expectedAttributes'], + $event->getRequest()->attributes->all(), + "Error on test $testCase[id]" + ); + } + } +} +?>