From c22f8150597a6191a5756bb1716ec3d3492e2d5f Mon Sep 17 00:00:00 2001 From: Tobias Oitzinger Date: Mon, 23 Sep 2024 13:26:51 +0200 Subject: [PATCH] feat(openapi): http authentication support add support for http authentication (for example http basic or bearer tokens) Closes: #6664 --- src/Laravel/ApiPlatformProvider.php | 1 + src/Laravel/config/api-platform.php | 6 ++++++ src/OpenApi/Factory/OpenApiFactory.php | 5 +++++ src/OpenApi/Options.php | 7 ++++++- src/OpenApi/Tests/Factory/OpenApiFactoryTest.php | 12 ++++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Laravel/ApiPlatformProvider.php b/src/Laravel/ApiPlatformProvider.php index 0a9272b5572..0d54e13630d 100644 --- a/src/Laravel/ApiPlatformProvider.php +++ b/src/Laravel/ApiPlatformProvider.php @@ -736,6 +736,7 @@ public function register(): void 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', []), + httpAuth: $config->get('api-platform.swagger_ui.http_auth', []), ); }); diff --git a/src/Laravel/config/api-platform.php b/src/Laravel/config/api-platform.php index 2d4b68783aa..6a421a48f4b 100644 --- a/src/Laravel/config/api-platform.php +++ b/src/Laravel/config/api-platform.php @@ -87,6 +87,12 @@ // 'refreshUrl' => '', // 'scopes' => ['scope1' => 'Description scope 1'], // 'pkce' => true + //], + //'http_auth' => [ + // 'Personal Access Token' => [ + // 'scheme' => 'bearer', + // 'bearerFormat' => 'JWT' + // ] //] ], diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index d5c4e2f6edb..8840f2ac218 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -687,6 +687,11 @@ private function getSecuritySchemes(): array $securitySchemes[$key] = new SecurityScheme('apiKey', $description, $apiKey['name'], $apiKey['type']); } + foreach ($this->openApiOptions->getHttpAuth() as $key => $httpAuth) { + $description = \sprintf('Value for the http %s parameter.', $httpAuth['scheme']); + $securitySchemes[$key] = new SecurityScheme('http', $description, null, null, $httpAuth['scheme'], $httpAuth['bearerFormat'] ?? null); + } + return $securitySchemes; } diff --git a/src/OpenApi/Options.php b/src/OpenApi/Options.php index 5683c7c2178..a229b4d36ec 100644 --- a/src/OpenApi/Options.php +++ b/src/OpenApi/Options.php @@ -15,7 +15,7 @@ final class Options { - public function __construct(private readonly string $title, private readonly string $description = '', private readonly string $version = '', private readonly bool $oAuthEnabled = false, private readonly ?string $oAuthType = null, private readonly ?string $oAuthFlow = null, private readonly ?string $oAuthTokenUrl = null, private readonly ?string $oAuthAuthorizationUrl = null, private readonly ?string $oAuthRefreshUrl = null, private readonly array $oAuthScopes = [], private readonly array $apiKeys = [], private readonly ?string $contactName = null, private readonly ?string $contactUrl = null, private readonly ?string $contactEmail = null, private readonly ?string $termsOfService = null, private readonly ?string $licenseName = null, private readonly ?string $licenseUrl = null, private bool $overrideResponses = true) + public function __construct(private readonly string $title, private readonly string $description = '', private readonly string $version = '', private readonly bool $oAuthEnabled = false, private readonly ?string $oAuthType = null, private readonly ?string $oAuthFlow = null, private readonly ?string $oAuthTokenUrl = null, private readonly ?string $oAuthAuthorizationUrl = null, private readonly ?string $oAuthRefreshUrl = null, private readonly array $oAuthScopes = [], private readonly array $apiKeys = [], private readonly ?string $contactName = null, private readonly ?string $contactUrl = null, private readonly ?string $contactEmail = null, private readonly ?string $termsOfService = null, private readonly ?string $licenseName = null, private readonly ?string $licenseUrl = null, private bool $overrideResponses = true, private readonly array $httpAuth = []) { } @@ -74,6 +74,11 @@ public function getApiKeys(): array return $this->apiKeys; } + public function getHttpAuth(): array + { + return $this->httpAuth; + } + public function getContactName(): ?string { return $this->contactName; diff --git a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php index d37b1ddd392..32d3d6ab2f3 100644 --- a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php +++ b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php @@ -521,6 +521,14 @@ public function testInvoke(): void 'type' => 'query', 'name' => 'key', ], + ], null, null, null, null, null, null, true, [ + 'bearer' => [ + 'scheme' => 'bearer', + 'bearerFormat' => 'JWT', + ], + 'basic' => [ + 'scheme' => 'basic', + ], ]), new PaginationOptions(true, 'page', true, 'itemsPerPage', true, 'pagination') ); @@ -622,12 +630,16 @@ public function testInvoke(): void 'oauth' => new SecurityScheme('oauth2', 'OAuth 2.0 authorization code Grant', null, null, null, null, new OAuthFlows(null, null, null, new OAuthFlow('/oauth/v2/auth', '/oauth/v2/token', '/oauth/v2/refresh', new \ArrayObject(['scope param'])))), 'header' => new SecurityScheme('apiKey', 'Value for the Authorization header parameter.', 'Authorization', 'header'), 'query' => new SecurityScheme('apiKey', 'Value for the key query parameter.', 'key', 'query'), + 'bearer' => new SecurityScheme('http', 'Value for the http bearer parameter.', null, null, 'bearer', 'JWT'), + 'basic' => new SecurityScheme('http', 'Value for the http basic parameter.', null, null, 'basic', null), ])); $this->assertEquals([ ['oauth' => []], ['header' => []], ['query' => []], + ['bearer' => []], + ['basic' => []], ], $openApi->getSecurity()); $paths = $openApi->getPaths();