Alias not working with mezzio-authentication-oauth2


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

As far as I understand, it shouldn’t need a value passed as I am setting an alias as described in the docs here:

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:


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(
            static function(\Throwable $error) use ($errorMiddleware) : ResponseInterface
                return $errorMiddleware->handleError($error);
        $application = new Application(
        $application->pipe('api', AuthenticationMiddleware::class);

        return $application;

    protected static function getErrorMiddleware(ContainerInterface $container): Middleware\InternalServerError
        return $container->get(Middleware\InternalServerError::class);




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.

That interesting @froschdesign, thanks for that. I would not have worked that out without your help.

Getting in “AuthenticationInterface service is missing” in /vendor/mezzio/mezzio-authentication/src/AuthenticationMiddlewareFactory.php:33

Any idea what is causing that?

Looks like I need 2 of them in the config:

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.

Thanks again @froschdesign