Skip to content

[BUG][php-slim4] api-key authentication throws exception #21958

@PhotoKevin

Description

@PhotoKevin

Bug Report Checklist

  • [X ] Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • [X ] Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using security APIKeyHeader with generator php-slim4 the resulting code throws an exception. (my domains removed for privacy)

Fatal error: Uncaught InvalidArgumentException: Authenticator option has not been setted. in ****/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php:52
Stack trace: 
#0 ****/lib/App/RegisterRoutes.php(510): Dyorg\TokenAuthentication->__construct(Array)
#1 ****/public/index.php(79): OpenAPIServer\App\RegisterRoutes->__invoke(Object(Slim\App))
#2 {main} thrown in ****/vendor/dyorg/slim-token-authentication/src/TokenAuthentication.php on line 52
openapi-generator version

This occurs with 7.15.0

OpenAPI declaration file content or url

Modify the petstore.json to include:

                "security": [
                    {
                        "APIKeyHeader": []
                    }
                ],
Generation Details

java -jar gen\openapi-generator-cli-7.14.0.jar generate -i petstore.json -g php-slim4 --skip-validate-spec -o ./petstore

Steps to reproduce

curl -X GET "https://yourhost.com/api/pet -H "X-API-Key: mykey" --output pet.json

Related issues/PRs

No related issues that I can find

Suggest a fix

The problem lies in php-slim4-server\register_routes.mustache

                    if ($authMethod['isApiKey']) {
                        $authenticatorConfig = [
                            'path' => '/',
                            'authenticator' => new ApiKeyAuthenticator,
                            'regex' => '/\s+(.*)$/i',
                            'argument' => null,
                            'attribute' => 'authorization_token',
                            'error' => ['{{authPackage}}\ApiKeyAuthenticator', 'handleUnauthorized'],
                        ];
                        if ($authMethod['isKeyInHeader']) {
                            $authenticatorConfig = [
                                'header' => $authMethod['keyParamName'],
                                'parameter' => null,
                                'cookie' => null,
                            ];

The seconds (and some subsequent) assignment of $authenticatorConfig should be a += so that the additional values are added to the configuration instead of replacing them.

Secondarily, the regex has \s+ which requires the key to begin with whitespace. Since headers generally have their whitespace automatically trimmed, this ought to be changed to \s*. I suspect the 'isBearer' and 'OAuth' regular expressions also ought to be changed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions