From edf8ed2be60f349a43575286fbdb23445a050128 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 10:10:20 +0100 Subject: [PATCH 1/6] OpenAPI invalid securityScheme with implicit flow (fix #4079) --- .../DependencyInjection/Configuration.php | 6 +++--- src/OpenApi/Options.php | 18 +++++++++--------- tests/Fixtures/app/config/config_common.yml | 7 +++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php index de9a01c80a8..fe9c361c4fb 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php @@ -253,9 +253,9 @@ private function addOAuthSection(ArrayNodeDefinition $rootNode): void ->scalarNode('clientSecret')->defaultValue('')->info('The oauth client secret.')->end() ->scalarNode('type')->defaultValue('oauth2')->info('The oauth client secret.')->end() ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end() - ->scalarNode('tokenUrl')->defaultValue('/oauth/v2/token')->info('The oauth token url.')->end() - ->scalarNode('authorizationUrl')->defaultValue('/oauth/v2/auth')->info('The oauth authentication url.')->end() - ->scalarNode('refreshUrl')->defaultValue('/oauth/v2/refresh')->info('The oauth refresh url.')->end() + ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end() + ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end() + ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end() ->arrayNode('scopes') ->prototype('scalar')->end() ->end() diff --git a/src/OpenApi/Options.php b/src/OpenApi/Options.php index 043c16f1b47..c1824d73b8c 100644 --- a/src/OpenApi/Options.php +++ b/src/OpenApi/Options.php @@ -33,7 +33,7 @@ final class Options private $licenseName; private $licenseUrl; - public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, string $oAuthType = '', string $oAuthFlow = '', string $oAuthTokenUrl = '', string $oAuthAuthorizationUrl = '', string $oAuthRefreshUrl = '', array $oAuthScopes = [], array $apiKeys = [], string $contactName = null, string $contactUrl = null, string $contactEmail = null, string $termsOfService = null, string $licenseName = null, string $licenseUrl = null) + public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, string $oAuthType = null, string $oAuthFlow = null, string $oAuthTokenUrl = null, string $oAuthAuthorizationUrl = null, string $oAuthRefreshUrl = null, array $oAuthScopes = [], array $apiKeys = [], string $contactName = null, string $contactUrl = null, string $contactEmail = null, string $termsOfService = null, string $licenseName = null, string $licenseUrl = null) { $this->title = $title; $this->description = $description; @@ -41,9 +41,9 @@ public function __construct(string $title, string $description = '', string $ver $this->oAuthEnabled = $oAuthEnabled; $this->oAuthType = $oAuthType; $this->oAuthFlow = $oAuthFlow; - $this->oAuthTokenUrl = $oAuthTokenUrl; - $this->oAuthAuthorizationUrl = $oAuthAuthorizationUrl; - $this->oAuthRefreshUrl = $oAuthRefreshUrl; + $this->oAuthTokenUrl = $oAuthTokenUrl ?: null; + $this->oAuthAuthorizationUrl = $oAuthAuthorizationUrl ?: null; + $this->oAuthRefreshUrl = $oAuthRefreshUrl ?: null; $this->oAuthScopes = $oAuthScopes; $this->apiKeys = $apiKeys; $this->contactName = $contactName; @@ -74,27 +74,27 @@ public function getOAuthEnabled(): bool return $this->oAuthEnabled; } - public function getOAuthType(): string + public function getOAuthType(): ?string { return $this->oAuthType; } - public function getOAuthFlow(): string + public function getOAuthFlow(): ?string { return $this->oAuthFlow; } - public function getOAuthTokenUrl(): string + public function getOAuthTokenUrl(): ?string { return $this->oAuthTokenUrl; } - public function getOAuthAuthorizationUrl(): string + public function getOAuthAuthorizationUrl(): ?string { return $this->oAuthAuthorizationUrl; } - public function getOAuthRefreshUrl(): string + public function getOAuthRefreshUrl(): ?string { return $this->oAuthRefreshUrl; } diff --git a/tests/Fixtures/app/config/config_common.yml b/tests/Fixtures/app/config/config_common.yml index 69db4cfb002..0f9d4d27e07 100644 --- a/tests/Fixtures/app/config/config_common.yml +++ b/tests/Fixtures/app/config/config_common.yml @@ -47,6 +47,13 @@ api_platform: collection: order_parameter_name: 'order' order: 'ASC' + oauth: + enabled: true + clientId: my_client + type: 'oauth2' + flow: 'implicit' + authorizationUrl: 'http://my-custom-server/openid-connect/auth' + scopes: [] exception_to_status: Symfony\Component\Serializer\Exception\ExceptionInterface: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST ApiPlatform\Core\Exception\InvalidArgumentException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST From e9d6de107ebf814edbb863eb33b77cf24eeae0c5 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 10:12:47 +0100 Subject: [PATCH 2/6] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0b03f98dbe..98a0e72a8aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * OpenAPI: Fix `Link->requestBody` default value (#4116) * GraphQL: Fix "Resource class cannot be determined." error when a null iterable field is returned (#4092) * Metadata: Check the output class when calculating serializer groups (#3696) +* OpenAPI: Using an implicit flow is now valid, changes oauth configuration default values (#4115) ## 2.6.2 From 1794435518c5f16ce0d3b87a177947e1fb11f5ca Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 10:34:35 +0100 Subject: [PATCH 3/6] Fix tests --- .../Bundle/DependencyInjection/ApiPlatformExtensionTest.php | 6 +++--- .../Bundle/DependencyInjection/ConfigurationTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index 38f379dd726..bae66a199eb 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -1144,9 +1144,9 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo 'api_platform.oauth.clientSecret' => '', 'api_platform.oauth.type' => 'oauth2', 'api_platform.oauth.flow' => 'application', - 'api_platform.oauth.tokenUrl' => '/oauth/v2/token', - 'api_platform.oauth.authorizationUrl' => '/oauth/v2/auth', - 'api_platform.oauth.refreshUrl' => '/oauth/v2/refresh', + 'api_platform.oauth.tokenUrl' => '', + 'api_platform.oauth.authorizationUrl' => '', + 'api_platform.oauth.refreshUrl' => '', 'api_platform.oauth.scopes' => [], 'api_platform.enable_swagger_ui' => true, 'api_platform.enable_re_doc' => true, diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php index 1df2208bb80..3133bca92e1 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php @@ -142,9 +142,9 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm 'clientSecret' => '', 'type' => 'oauth2', 'flow' => 'application', - 'tokenUrl' => '/oauth/v2/token', - 'authorizationUrl' => '/oauth/v2/auth', - 'refreshUrl' => '/oauth/v2/refresh', + 'tokenUrl' => '', + 'authorizationUrl' => '', + 'refreshUrl' => '', 'scopes' => [], ], 'swagger' => [ From 755387583e9acb2bcd80b3e47c5e00a73ecd4fa7 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 15:23:21 +0100 Subject: [PATCH 4/6] fix ol' swagger --- src/Swagger/Serializer/DocumentationNormalizer.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index e405251228d..9ce098bec77 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -675,11 +675,14 @@ private function computeDoc(bool $v3, Documentation $documentation, \ArrayObject if ($this->oauthEnabled) { $oauthAttributes = [ - 'tokenUrl' => $this->oauthTokenUrl, 'authorizationUrl' => $this->oauthAuthorizationUrl, - 'scopes' => $this->oauthScopes, + 'scopes' => new \ArrayObject($this->oauthScopes), ]; + if ($this->oauthTokenUrl) { + $oauthAttributes['tokenUrl'] = $this->oauthTokenUrl; + } + $securityDefinitions['oauth'] = [ 'type' => $this->oauthType, 'description' => sprintf( From 8ab18a7e134cfb48625fc97516a212eb62bd267b Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 5 Mar 2021 16:45:59 +0100 Subject: [PATCH 5/6] fix ol' swagger --- tests/Swagger/Serializer/DocumentationNormalizerV2Test.php | 2 +- tests/Swagger/Serializer/DocumentationNormalizerV3Test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV2Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV2Test.php index 6afe6864eed..318f10774a8 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV2Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV2Test.php @@ -529,7 +529,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void 'flow' => 'application', 'tokenUrl' => '/oauth/v2/token', 'authorizationUrl' => '/oauth/v2/auth', - 'scopes' => ['scope param'], + 'scopes' => new \ArrayObject(['scope param']), ], ], 'security' => [['oauth' => []]], diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 3ea56e07583..24d1b7cc241 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -583,7 +583,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void 'authorizationCode' => [ 'tokenUrl' => '/oauth/v2/token', 'authorizationUrl' => '/oauth/v2/auth', - 'scopes' => ['scope param'], + 'scopes' => new \ArrayObject(['scope param']), ], ], ], From 771834ceac9c3a3d9cc4fe0fdec43ad8ff8d7f1a Mon Sep 17 00:00:00 2001 From: Antoine Bluchet Date: Tue, 9 Mar 2021 19:06:18 +0100 Subject: [PATCH 6/6] Update src/OpenApi/Options.php Co-authored-by: Alan Poulain --- src/OpenApi/Options.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenApi/Options.php b/src/OpenApi/Options.php index c1824d73b8c..d4a995123af 100644 --- a/src/OpenApi/Options.php +++ b/src/OpenApi/Options.php @@ -33,7 +33,7 @@ final class Options private $licenseName; private $licenseUrl; - public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, string $oAuthType = null, string $oAuthFlow = null, string $oAuthTokenUrl = null, string $oAuthAuthorizationUrl = null, string $oAuthRefreshUrl = null, array $oAuthScopes = [], array $apiKeys = [], string $contactName = null, string $contactUrl = null, string $contactEmail = null, string $termsOfService = null, string $licenseName = null, string $licenseUrl = null) + public function __construct(string $title, string $description = '', string $version = '', bool $oAuthEnabled = false, ?string $oAuthType = null, ?string $oAuthFlow = null, ?string $oAuthTokenUrl = null, ?string $oAuthAuthorizationUrl = null, ?string $oAuthRefreshUrl = null, array $oAuthScopes = [], array $apiKeys = [], string $contactName = null, string $contactUrl = null, string $contactEmail = null, string $termsOfService = null, string $licenseName = null, string $licenseUrl = null) { $this->title = $title; $this->description = $description;