Skip to content

Commit 85306f2

Browse files
authoredSep 23, 2024··
fix(laravel): swagger ui authentication (#6661)
fix swagger ui authentication in laravel, by providing necessary options Closes: #6662
1 parent 121a328 commit 85306f2

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
 

‎src/Laravel/ApiPlatformProvider.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,33 @@ public function register(): void
705705
/** @var ConfigRepository */
706706
$config = $app['config'];
707707

708-
return new Options(title: $config->get('api-platform.title') ?? '');
708+
return new Options(
709+
title: $config->get('api-platform.title', ''),
710+
description: $config->get('api-platform.description', ''),
711+
version: $config->get('api-platform.version', ''),
712+
oAuthEnabled: $config->get('api-platform.swagger_ui.oauth.enabled', false),
713+
oAuthType: $config->get('api-platform.swagger_ui.oauth.type', null),
714+
oAuthFlow: $config->get('api-platform.swagger_ui.oauth.flow', null),
715+
oAuthTokenUrl: $config->get('api-platform.swagger_ui.oauth.tokenUrl', null),
716+
oAuthAuthorizationUrl: $config->get('api-platform.swagger_ui.oauth.authorizationUrl', null),
717+
oAuthRefreshUrl: $config->get('api-platform.swagger_ui.oauth.refreshUrl', null),
718+
oAuthScopes: $config->get('api-platform.swagger_ui.oauth.scopes', []),
719+
apiKeys: $config->get('api-platform.swagger_ui.apiKeys', []),
720+
);
721+
});
722+
723+
$this->app->singleton(SwaggerUiProcessor::class, function (Application $app) {
724+
/** @var ConfigRepository */
725+
$config = $app['config'];
726+
727+
return new SwaggerUiProcessor(
728+
urlGenerator: $app->make(UrlGeneratorInterface::class),
729+
normalizer: $app->make(NormalizerInterface::class),
730+
openApiOptions: $app->make(Options::class),
731+
oauthClientId: $config->get('api-platform.swagger_ui.oauth.clientId'),
732+
oauthClientSecret: $config->get('api-platform.swagger_ui.oauth.clientSecret'),
733+
oauthPkce: $config->get('api-platform.swagger_ui.oauth.pkce', false),
734+
);
709735
});
710736

711737
$this->app->singleton(DocumentationController::class, function (Application $app) {

‎src/Laravel/State/SwaggerUiProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
7070
'flow' => $this->openApiOptions->getOAuthFlow(),
7171
'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
7272
'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
73+
'redirectUrl' => $request->getSchemeAndHttpHost().'/vendor/api-platform/swagger-ui/oauth2-redirect.html',
7374
'scopes' => $this->openApiOptions->getOAuthScopes(),
7475
'clientId' => $this->oauthClientId,
7576
'clientSecret' => $this->oauthClientSecret,

‎src/Laravel/config/api-platform.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,24 @@
7272
],
7373

7474
'swagger_ui' => [
75-
'enabled' => true
75+
'enabled' => true,
76+
//'apiKeys' => [
77+
// 'api' => [
78+
// 'type' => 'Bearer',
79+
// 'name' => 'Authentication Token',
80+
// 'in' => 'header'
81+
// ]
82+
//],
83+
//'oauth' => [
84+
// 'enabled' => true,
85+
// 'type' => 'oauth2',
86+
// 'flow' => 'authorizationCode',
87+
// 'tokenUrl' => '',
88+
// 'authorizationUrl' =>'',
89+
// 'refreshUrl' => '',
90+
// 'scopes' => ['scope1' => 'Description scope 1'],
91+
// 'pkce' => true
92+
//]
7693
],
7794

7895
'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,

0 commit comments

Comments
 (0)
Please sign in to comment.