Skip to content

Commit f3ec3ad

Browse files
toitzisoyuka
andauthoredDec 13, 2024
feat(openapi): HTTP Authentication Support for Swagger UI (#6665)
* feat(openapi): http authentication support add support for http authentication (for example http basic or bearer tokens) Closes: #6664 * add symfony support --------- Co-authored-by: soyuka <[email protected]>
1 parent ce30ed4 commit f3ec3ad

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed
 

‎Bundle/DependencyInjection/ApiPlatformExtension.php

+1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
500500
$container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
501501
$container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
502502
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
503+
$container->setParameter('api_platform.swagger.http_auth', $config['swagger']['http_auth']);
503504
if ($config['openapi']['swagger_ui_extra_configuration'] && $config['swagger']['swagger_ui_extra_configuration']) {
504505
throw new RuntimeException('You can not set "swagger_ui_extra_configuration" twice - in "openapi" and "swagger" section.');
505506
}

‎Bundle/DependencyInjection/Configuration.php

+18
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,24 @@ private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
330330
->end()
331331
->end()
332332
->end()
333+
->arrayNode('http_auth')
334+
->info('Creates http security schemes for OpenAPI.')
335+
->useAttributeAsKey('key')
336+
->validate()
337+
->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
338+
->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
339+
->end()
340+
->prototype('array')
341+
->children()
342+
->scalarNode('scheme')
343+
->info('The OpenAPI HTTP auth scheme, for example "bearer"')
344+
->end()
345+
->scalarNode('bearerFormat')
346+
->info('The OpenAPI HTTP bearer format')
347+
->end()
348+
->end()
349+
->end()
350+
->end()
333351
->variableNode('swagger_ui_extra_configuration')
334352
->defaultValue([])
335353
->validate()

‎Bundle/Resources/config/openapi.xml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<argument>%api_platform.openapi.license.name%</argument>
6060
<argument>%api_platform.openapi.license.url%</argument>
6161
<argument>%api_platform.openapi.overrideResponses%</argument>
62+
<argument>%api_platform.swagger.http_auth%</argument>
6263
</service>
6364
<service id="ApiPlatform\OpenApi\Options" alias="api_platform.openapi.options" />
6465

‎Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,6 @@ public function testEventListenersConfiguration(): void
284284
];
285285

286286
$this->assertContainerHas($services, $aliases);
287+
$this->container->hasParameter('api_platform.swagger.http_auth');
287288
}
288289
}

0 commit comments

Comments
 (0)
Please sign in to comment.