I am trying to get the oAuth2 working with a mezzio application using mezzio-authentication-oauth2.
I can’t seem to get past an error: Too few arguments to function Mezzio\Authentication\AuthenticationMiddleware::__construct(), 0 passed
Is there a bug here? It sounds very similar to what weierophinney was experiencing here:
Perhaps I should be using a different library for oAuth2?
Any help is appreciated.
Code samples:
App/Factory.php
<?php
declare(strict_types=1);
namespace App;
use App\Middleware;
use Interop\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Mezzio\Application;
use Mezzio\Handler\NotFoundHandler;
use Mezzio\MiddlewareFactory;
use Mezzio\Router\Middleware\DispatchMiddleware;
use Mezzio\Router\Middleware\MethodNotAllowedMiddleware;
use Mezzio\Router\Middleware\RouteMiddleware;
use Mezzio\Router\RouteCollector;
use Laminas\HttpHandlerRunner\Emitter\EmitterInterface;
use Laminas\HttpHandlerRunner\RequestHandlerRunner;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\Stratigility\MiddlewarePipe;
use LosMiddleware\BasePath\BasePathMiddleware;
use Mezzio\Authentication\AuthenticationMiddleware;
class Factory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Application
{
$errorMiddleware = self::getErrorMiddleware($container);
$pipeline = new MiddlewarePipe();
$runner = new RequestHandlerRunner(
$pipeline,
$container->get(EmitterInterface::class),
$container->get(ServerRequestInterface::class),
static function(\Throwable $error) use ($errorMiddleware) : ResponseInterface
{
return $errorMiddleware->handleError($error);
}
);
$application = new Application(
$container->get(MiddlewareFactory::class),
$pipeline,
$container->get(RouteCollector::class),
$runner
);
$application->pipe($errorMiddleware);
$application->pipe(BasePathMiddleware::class);
$application->pipe(RouteMiddleware::class);
$application->pipe('api', AuthenticationMiddleware::class);
$application->pipe(MethodNotAllowedMiddleware::class);
$application->pipe(DispatchMiddleware::class);
$application->pipe(NotFoundHandler::class);
return $application;
}
protected static function getErrorMiddleware(ContainerInterface $container): Middleware\InternalServerError
{
return $container->get(Middleware\InternalServerError::class);
}
}
config/autoload/dependencies.global.php
<?php
declare(strict_types=1);
use Mezzio\Authentication\AuthenticationInterface;
use Mezzio\Authentication\OAuth2\OAuth2Adapter;
use LosMiddleware\BasePath\BasePathMiddleware;
return [
// Provides application-wide services.
// We recommend using fully-qualified class names whenever possible as
// service names.
'dependencies' => [
'aliases' => [
AuthenticationInterface::class => OAuth2Adapter::class
],
'invokables' => [
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
LosMiddleware\BasePath\BasePathMiddleware::class => LosMiddleware\BasePath\BasePathMiddlewareFactory::class
],
],
];
It seems that the factory for this middleware is not used / registered. Please check your configuration that the config provider of mezzio-authentication (Mezzio\Authentication\ConfigProvider::class) was added.
Hi,
Looks like I need 2 of them in the config:
\Mezzio\Authentication\OAuth2\ConfigProvider::class,
\Mezzio\Authentication\ConfigProvider::class,
Unfortunately I am off an application generated by OpenApiTool Generator instead of the mezzio skeleton, which provides a lot of features I would take for granted.
It is looking for the public.key file now, so I should be able to work with that.