Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,8 @@ public function register(): void
httpAuth: $config->get('api-platform.swagger_ui.http_auth', []),
tags: $config->get('api-platform.openapi.tags', []),
errorResourceClass: Error::class,
validationErrorResourceClass: ValidationError::class
validationErrorResourceClass: ValidationError::class,
withCredentials: $config->get('api-platform.swagger_ui.with_credentials', false),
);
});

Expand Down
1 change: 1 addition & 0 deletions src/Laravel/State/SwaggerUiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
'clientSecret' => $this->oauthClientSecret,
'pkce' => $this->oauthPkce,
],
'withCredentials' => $this->openApiOptions->getWithCredentials(),
];

$status = 200;
Expand Down
6 changes: 6 additions & 0 deletions src/Laravel/Tests/DocsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ public function testHtmlDocsRendersScalarWithoutFooterWhenRequested(): void
$this->assertStringContainsString('init-scalar-ui.js', $content);
$this->assertStringNotContainsString('id="formats"', $content);
}

public function testSwaggerDataDoesNotContainWithCredentialsByDefault(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'text/html']);
$this->assertStringNotContainsString('"withCredentials":true', (string) $res->getContent());
}
}
40 changes: 40 additions & 0 deletions src/Laravel/Tests/DocsWithCredentialsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Config\Repository;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

class DocsWithCredentialsTest extends TestCase
{
use ApiTestAssertionsTrait;
use WithWorkbench;

protected function defineEnvironment($app): void
{
tap($app['config'], static function (Repository $config): void {
$config->set('api-platform.swagger_ui.with_credentials', true);
});
}

public function testSwaggerDataContainsWithCredentialsTrueWhenEnabled(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'text/html']);
$res->assertOk();
$content = (string) $res->getContent();
$this->assertStringContainsString('"withCredentials":true', $content);
}
}
2 changes: 2 additions & 0 deletions src/Laravel/config/api-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
// 'bearerFormat' => 'JWT',
// ],
// ],
//
// 'with_credentials' => true,
],

// 'openapi' => [
Expand Down
16 changes: 14 additions & 2 deletions src/Laravel/public/init-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ window.onload = function() {
}).observe(document, {childList: true, subtree: true});

const data = JSON.parse(document.getElementById('swagger-data').innerText);
const ui = SwaggerUIBundle(Object.assign({

const config = {
spec: data.spec,
dom_id: '#swagger-ui',
validatorUrl: null,
Expand All @@ -55,7 +56,18 @@ window.onload = function() {
SwaggerUIBundle.plugins.DownloadUrl,
],
layout: 'StandaloneLayout',
}, data.extraConfiguration));
};

if (data.withCredentials) {
// Cloudflare Access fix: ensure cookies are sent on token / CORS calls
config.requestInterceptor = (req) => {
req.credentials = 'include';
return req;
};
}

const withExtraConfig = Object.assign(config, data.extraConfiguration);
const ui = SwaggerUIBundle(withExtraConfig);

if (data.oauth.enabled) {
ui.initOAuth({
Expand Down
6 changes: 6 additions & 0 deletions src/OpenApi/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct(
private ?string $errorResourceClass = null,
private ?string $validationErrorResourceClass = null,
private ?string $licenseIdentifier = null,
private bool $withCredentials = false,
) {
}

Expand Down Expand Up @@ -178,4 +179,9 @@ public function getLicenseIdentifier(): ?string
{
return $this->licenseIdentifier;
}

public function getWithCredentials(): bool
{
return $this->withCredentials;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
$container->setParameter('api_platform.enable_scalar', $config['enable_scalar']);
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
$container->setParameter('api_platform.swagger.persist_authorization', $config['swagger']['persist_authorization']);
$container->setParameter('api_platform.swagger.with_credentials', $config['swagger']['with_credentials']);
$container->setParameter('api_platform.swagger.http_auth', $config['swagger']['http_auth']);
if ($config['openapi']['swagger_ui_extra_configuration'] && $config['swagger']['swagger_ui_extra_configuration']) {
throw new RuntimeException('You can not set "swagger_ui_extra_configuration" twice - in "openapi" and "swagger" section.');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
->addDefaultsIfNotSet()
->children()
->booleanNode('persist_authorization')->defaultValue(false)->info('Persist the SwaggerUI Authorization in the localStorage.')->end()
->booleanNode('with_credentials')->defaultValue(false)->info('Send credentials (cookies, authorization headers) on Swagger UI cross-origin requests (e.g. when running behind Cloudflare Access).')->end()
->arrayNode('versions')
->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
->defaultValue($supportedVersions)
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
'%api_platform.openapi.errorResourceClass%',
'%api_platform.openapi.validationErrorResourceClass%',
'%api_platform.openapi.license.identifier%',
'%api_platform.swagger.with_credentials%',
]);

$services->alias(Options::class, 'api_platform.openapi.options');
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Bundle/Resources/public/init-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ window.onload = function() {
}).observe(document, {childList: true, subtree: true});

const data = JSON.parse(document.getElementById('swagger-data').innerText);
const ui = SwaggerUIBundle(Object.assign({
const config = {
spec: data.spec,
dom_id: '#swagger-ui',
validatorUrl: null,
Expand All @@ -56,7 +56,16 @@ window.onload = function() {
SwaggerUIBundle.plugins.DownloadUrl,
],
layout: 'StandaloneLayout',
}, data.extraConfiguration));
};

if (data.withCredentials) {
config.requestInterceptor = (req) => {
req.credentials = 'include';
return req;
};
}

const ui = SwaggerUIBundle(Object.assign(config, data.extraConfiguration));

if (data.oauth.enabled) {
ui.initOAuth({
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SwaggerUi/SwaggerUiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
'spec' => $this->normalizer->normalize($openApi, 'json', []),
'persistAuthorization' => $this->openApiOptions->hasPersistAuthorization(),
'withCredentials' => $this->openApiOptions->getWithCredentials(),
'oauth' => [
'enabled' => $this->openApiOptions->getOAuthEnabled(),
'type' => $this->openApiOptions->getOAuthType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
'http_auth' => [],
'swagger_ui_extra_configuration' => [],
'persist_authorization' => false,
'with_credentials' => false,
],
'eager_loading' => [
'enabled' => true,
Expand Down
Loading