Skip to content

Commit f193281

Browse files
soyukaalanpoulain
andauthored
OpenAPI invalid securityScheme with implicit flow (fix #4079) (#4115)
* OpenAPI invalid securityScheme with implicit flow (fix #4079) * add changelog * Fix tests * fix ol' swagger * fix ol' swagger * Update src/OpenApi/Options.php Co-authored-by: Alan Poulain <[email protected]> Co-authored-by: Alan Poulain <[email protected]>
1 parent 8b02b2b commit f193281

File tree

9 files changed

+33
-22
lines changed

9 files changed

+33
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* OpenAPI: Fix `Link->requestBody` default value (#4116)
2626
* GraphQL: Fix "Resource class cannot be determined." error when a null iterable field is returned (#4092)
2727
* Metadata: Check the output class when calculating serializer groups (#3696)
28+
* OpenAPI: Using an implicit flow is now valid, changes oauth configuration default values (#4115)
2829

2930
## 2.6.2
3031

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ private function addOAuthSection(ArrayNodeDefinition $rootNode): void
253253
->scalarNode('clientSecret')->defaultValue('')->info('The oauth client secret.')->end()
254254
->scalarNode('type')->defaultValue('oauth2')->info('The oauth client secret.')->end()
255255
->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
256-
->scalarNode('tokenUrl')->defaultValue('/oauth/v2/token')->info('The oauth token url.')->end()
257-
->scalarNode('authorizationUrl')->defaultValue('/oauth/v2/auth')->info('The oauth authentication url.')->end()
258-
->scalarNode('refreshUrl')->defaultValue('/oauth/v2/refresh')->info('The oauth refresh url.')->end()
256+
->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
257+
->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
258+
->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
259259
->arrayNode('scopes')
260260
->prototype('scalar')->end()
261261
->end()

src/OpenApi/Options.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ final class Options
3333
private $licenseName;
3434
private $licenseUrl;
3535

36-
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)
36+
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)
3737
{
3838
$this->title = $title;
3939
$this->description = $description;
4040
$this->version = $version;
4141
$this->oAuthEnabled = $oAuthEnabled;
4242
$this->oAuthType = $oAuthType;
4343
$this->oAuthFlow = $oAuthFlow;
44-
$this->oAuthTokenUrl = $oAuthTokenUrl;
45-
$this->oAuthAuthorizationUrl = $oAuthAuthorizationUrl;
46-
$this->oAuthRefreshUrl = $oAuthRefreshUrl;
44+
$this->oAuthTokenUrl = $oAuthTokenUrl ?: null;
45+
$this->oAuthAuthorizationUrl = $oAuthAuthorizationUrl ?: null;
46+
$this->oAuthRefreshUrl = $oAuthRefreshUrl ?: null;
4747
$this->oAuthScopes = $oAuthScopes;
4848
$this->apiKeys = $apiKeys;
4949
$this->contactName = $contactName;
@@ -74,27 +74,27 @@ public function getOAuthEnabled(): bool
7474
return $this->oAuthEnabled;
7575
}
7676

77-
public function getOAuthType(): string
77+
public function getOAuthType(): ?string
7878
{
7979
return $this->oAuthType;
8080
}
8181

82-
public function getOAuthFlow(): string
82+
public function getOAuthFlow(): ?string
8383
{
8484
return $this->oAuthFlow;
8585
}
8686

87-
public function getOAuthTokenUrl(): string
87+
public function getOAuthTokenUrl(): ?string
8888
{
8989
return $this->oAuthTokenUrl;
9090
}
9191

92-
public function getOAuthAuthorizationUrl(): string
92+
public function getOAuthAuthorizationUrl(): ?string
9393
{
9494
return $this->oAuthAuthorizationUrl;
9595
}
9696

97-
public function getOAuthRefreshUrl(): string
97+
public function getOAuthRefreshUrl(): ?string
9898
{
9999
return $this->oAuthRefreshUrl;
100100
}

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,14 @@ private function computeDoc(bool $v3, Documentation $documentation, \ArrayObject
675675

676676
if ($this->oauthEnabled) {
677677
$oauthAttributes = [
678-
'tokenUrl' => $this->oauthTokenUrl,
679678
'authorizationUrl' => $this->oauthAuthorizationUrl,
680-
'scopes' => $this->oauthScopes,
679+
'scopes' => new \ArrayObject($this->oauthScopes),
681680
];
682681

682+
if ($this->oauthTokenUrl) {
683+
$oauthAttributes['tokenUrl'] = $this->oauthTokenUrl;
684+
}
685+
683686
$securityDefinitions['oauth'] = [
684687
'type' => $this->oauthType,
685688
'description' => sprintf(

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,9 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11441144
'api_platform.oauth.clientSecret' => '',
11451145
'api_platform.oauth.type' => 'oauth2',
11461146
'api_platform.oauth.flow' => 'application',
1147-
'api_platform.oauth.tokenUrl' => '/oauth/v2/token',
1148-
'api_platform.oauth.authorizationUrl' => '/oauth/v2/auth',
1149-
'api_platform.oauth.refreshUrl' => '/oauth/v2/refresh',
1147+
'api_platform.oauth.tokenUrl' => '',
1148+
'api_platform.oauth.authorizationUrl' => '',
1149+
'api_platform.oauth.refreshUrl' => '',
11501150
'api_platform.oauth.scopes' => [],
11511151
'api_platform.enable_swagger_ui' => true,
11521152
'api_platform.enable_re_doc' => true,

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
142142
'clientSecret' => '',
143143
'type' => 'oauth2',
144144
'flow' => 'application',
145-
'tokenUrl' => '/oauth/v2/token',
146-
'authorizationUrl' => '/oauth/v2/auth',
147-
'refreshUrl' => '/oauth/v2/refresh',
145+
'tokenUrl' => '',
146+
'authorizationUrl' => '',
147+
'refreshUrl' => '',
148148
'scopes' => [],
149149
],
150150
'swagger' => [

tests/Fixtures/app/config/config_common.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ api_platform:
4747
collection:
4848
order_parameter_name: 'order'
4949
order: 'ASC'
50+
oauth:
51+
enabled: true
52+
clientId: my_client
53+
type: 'oauth2'
54+
flow: 'implicit'
55+
authorizationUrl: 'http://my-custom-server/openid-connect/auth'
56+
scopes: []
5057
exception_to_status:
5158
Symfony\Component\Serializer\Exception\ExceptionInterface: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
5259
ApiPlatform\Core\Exception\InvalidArgumentException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST

tests/Swagger/Serializer/DocumentationNormalizerV2Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
529529
'flow' => 'application',
530530
'tokenUrl' => '/oauth/v2/token',
531531
'authorizationUrl' => '/oauth/v2/auth',
532-
'scopes' => ['scope param'],
532+
'scopes' => new \ArrayObject(['scope param']),
533533
],
534534
],
535535
'security' => [['oauth' => []]],

tests/Swagger/Serializer/DocumentationNormalizerV3Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
583583
'authorizationCode' => [
584584
'tokenUrl' => '/oauth/v2/token',
585585
'authorizationUrl' => '/oauth/v2/auth',
586-
'scopes' => ['scope param'],
586+
'scopes' => new \ArrayObject(['scope param']),
587587
],
588588
],
589589
],

0 commit comments

Comments
 (0)