Alias not working with mezzio-authentication-oauth2

Hi,

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:
https://docs.mezzio.dev/mezzio-authentication-oauth2/v1/usage/#authenticate-a-middleware

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.

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?

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.

Thanks again @froschdesign