Skip to content

Commit

Permalink
fix(laravel): swagger ui authentication (#6661)
Browse files Browse the repository at this point in the history
fix swagger ui authentication in laravel, by providing necessary options

Closes: #6662
  • Loading branch information
toitzi authored Sep 23, 2024
1 parent 121a328 commit 85306f2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,33 @@ public function register(): void
/** @var ConfigRepository */
$config = $app['config'];

return new Options(title: $config->get('api-platform.title') ?? '');
return new Options(
title: $config->get('api-platform.title', ''),
description: $config->get('api-platform.description', ''),
version: $config->get('api-platform.version', ''),
oAuthEnabled: $config->get('api-platform.swagger_ui.oauth.enabled', false),
oAuthType: $config->get('api-platform.swagger_ui.oauth.type', null),
oAuthFlow: $config->get('api-platform.swagger_ui.oauth.flow', null),
oAuthTokenUrl: $config->get('api-platform.swagger_ui.oauth.tokenUrl', null),
oAuthAuthorizationUrl: $config->get('api-platform.swagger_ui.oauth.authorizationUrl', null),
oAuthRefreshUrl: $config->get('api-platform.swagger_ui.oauth.refreshUrl', null),
oAuthScopes: $config->get('api-platform.swagger_ui.oauth.scopes', []),
apiKeys: $config->get('api-platform.swagger_ui.apiKeys', []),
);
});

$this->app->singleton(SwaggerUiProcessor::class, function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];

return new SwaggerUiProcessor(
urlGenerator: $app->make(UrlGeneratorInterface::class),
normalizer: $app->make(NormalizerInterface::class),
openApiOptions: $app->make(Options::class),
oauthClientId: $config->get('api-platform.swagger_ui.oauth.clientId'),
oauthClientSecret: $config->get('api-platform.swagger_ui.oauth.clientSecret'),
oauthPkce: $config->get('api-platform.swagger_ui.oauth.pkce', false),
);
});

$this->app->singleton(DocumentationController::class, function (Application $app) {
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 @@ -70,6 +70,7 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
'flow' => $this->openApiOptions->getOAuthFlow(),
'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
'redirectUrl' => $request->getSchemeAndHttpHost().'/vendor/api-platform/swagger-ui/oauth2-redirect.html',
'scopes' => $this->openApiOptions->getOAuthScopes(),
'clientId' => $this->oauthClientId,
'clientSecret' => $this->oauthClientSecret,
Expand Down
19 changes: 18 additions & 1 deletion src/Laravel/config/api-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,24 @@
],

'swagger_ui' => [
'enabled' => true
'enabled' => true,
//'apiKeys' => [
// 'api' => [
// 'type' => 'Bearer',
// 'name' => 'Authentication Token',
// 'in' => 'header'
// ]
//],
//'oauth' => [
// 'enabled' => true,
// 'type' => 'oauth2',
// 'flow' => 'authorizationCode',
// 'tokenUrl' => '',
// 'authorizationUrl' =>'',
// 'refreshUrl' => '',
// 'scopes' => ['scope1' => 'Description scope 1'],
// 'pkce' => true
//]
],

'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,
Expand Down

0 comments on commit 85306f2

Please sign in to comment.