Skip to content

Commit 5ccdac8

Browse files
authored
Merge pull request #134 from Lomkit/fix/security-schemes
🐛 open api security schemes
2 parents 8e30788 + ccc5216 commit 5ccdac8

File tree

2 files changed

+137
-36
lines changed

2 files changed

+137
-36
lines changed

config/rest.php

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
],
6666
'info' => [
6767
'title' => config('app.name'),
68-
'summary' => 'This is my projet\'s documentation',
69-
'description' => 'Find out all about my projet\'s API',
68+
'summary' => 'This is my project\'s documentation',
69+
'description' => 'Find out all about my project\'s API',
7070
'termsOfService' => null, // (Optional) Url to terms of services
7171
'contact' => [
7272
'name' => 'My Company',
@@ -103,23 +103,89 @@
103103
],
104104
// See https://spec.openapis.org/oas/v3.1.0#security-scheme-object
105105
'security' => [
106-
// [
107-
// 'type' => 'http',
108-
// 'description' => 'description',
109-
// 'scheme' => 'Bearer',
110-
// 'bearerFormat' => 'JWT'
111-
// ],
112-
// [
113-
// 'type' => 'oauth2',
114-
// 'flows' => [
115-
// 'authorizationCode' => [
116-
// 'scopes' => ['write:pets'],
117-
// 'tokenUrl' => 'https://example.com/api/oauth/token',
118-
// 'authorizationUrl' => 'https://example.com/api/oauth/dialog',
119-
// 'refreshUrl' => 'https://example.com/api/oauth/refresh',
120-
// ]
121-
// ]
122-
// ]
106+
// [
107+
// "api_key" => []
108+
// ],
109+
// [
110+
// "auth" => [
111+
// 'write:users',
112+
// 'read:users'
113+
// ]
114+
// ]
115+
],
116+
// See https://spec.openapis.org/oas/v3.1.0#security-scheme-object
117+
'securitySchemes' => [
118+
// "api_key" => [
119+
// "description" => "Authentication via API key",
120+
// "type" => "apiKey",
121+
// "name" => "x-api-key",
122+
// "in" => "header"
123+
// ],
124+
// "http_bearer" => [
125+
// "description" => "HTTP authentication with bearer token",
126+
// "type" => "http",
127+
// "scheme" => "bearer",
128+
// "bearerFormat" => "JWT"
129+
// ],
130+
// "oauth_authcode" => [
131+
// "description" => "Authentication via OAuth2 with authorization code flow",
132+
// "type" => "oauth2",
133+
// "flows" => [
134+
// "authorizationCode" => [
135+
// "authorizationUrl" => "https://example.com/api/oauth/dialog",
136+
// "tokenUrl" => "https://example.com/api/oauth/token",
137+
// "refreshUrl" => "https://example.com/api/oauth/refresh",
138+
// "scopes" => [
139+
// "do:something" => "do something"
140+
// ]
141+
// ]
142+
// ]
143+
// ],
144+
// "oauth_clientcredentials" => [
145+
// "description" => "Authentication via OAuth2 with client credentials flow",
146+
// "type" => "oauth2",
147+
// "flows" => [
148+
// "clientCredentials" => [
149+
// "tokenUrl" => "https://example.com/api/oauth/token",
150+
// "refreshUrl" => "https://example.com/api/oauth/refresh",
151+
// "scopes" => [
152+
// "do:something" => "do something"
153+
// ]
154+
// ]
155+
// ]
156+
// ],
157+
// "oauth_implicit" => [
158+
// "description" => "Authentication via OAuth2 with implicit flow",
159+
// "type" => "oauth2",
160+
// "flows" => [
161+
// "implicit" => [
162+
// "authorizationUrl" => "https://example.com/api/oauth/dialog",
163+
// "refreshUrl" => "https://example.com/api/oauth/refresh",
164+
// "scopes" => [
165+
// "write:foo" => "modify foo",
166+
// "read:foo" => "read foo"
167+
// ]
168+
// ]
169+
// ]
170+
// ],
171+
// "oauth_password" => [
172+
// "description" => "Authentication via OAuth2 with resource owner password flow",
173+
// "type" => "oauth2",
174+
// "flows" => [
175+
// "password" => [
176+
// "tokenUrl" => "https://example.com/api/oauth/token",
177+
// "refreshUrl" => "https://example.com/api/oauth/refresh",
178+
// "scopes" => [
179+
// "do:something" => "do something"
180+
// ]
181+
// ]
182+
// ]
183+
// ],
184+
// "open_id" => [
185+
// "description" => "Authentication via OpenID Connect",
186+
// "type" => "openIdConnect",
187+
// "openIdConnectUrl" => "https://example.com/openid/issuer/location"
188+
// ]
123189
],
124190
],
125191
];

src/Documentation/Schemas/OpenAPI.php

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class OpenAPI extends Schema
4444
*/
4545
protected array $security = [];
4646

47+
/**
48+
* A declaration of which security schemes mechanisms can be used across the API.
49+
*
50+
* @var array
51+
*/
52+
protected array $securitySchemes = [];
53+
4754
/**
4855
* Get the version number of the OpenAPI specification.
4956
*
@@ -140,6 +147,30 @@ public function withSecurity(array $security): self
140147
return $this;
141148
}
142149

150+
/**
151+
* Get the declaration of security mechanisms for the API.
152+
*
153+
* @return array
154+
*/
155+
public function securitySchemes(): array
156+
{
157+
return $this->securitySchemes;
158+
}
159+
160+
/**
161+
* Set the declaration of security mechanisms for the API.
162+
*
163+
* @param array $securitySchemes
164+
*
165+
* @return self
166+
*/
167+
public function withSecuritySchemes(array $securitySchemes): self
168+
{
169+
$this->securitySchemes = $securitySchemes;
170+
171+
return $this;
172+
}
173+
143174
/**
144175
* Set the Server Objects, which provide connectivity information to a target server.
145176
*
@@ -178,7 +209,10 @@ public function jsonSerialize(): mixed
178209
'paths' => collect($this->paths())->map->jsonSerialize()->toArray(),
179210
],
180211
isset($this->servers) ? ['servers' => collect($this->servers())->map->jsonSerialize()->toArray()] : [],
181-
isset($this->security) ? ['security' => collect($this->security())->map->jsonSerialize()->toArray()] : []
212+
isset($this->security) ? ['security' => $this->security] : [],
213+
['components' => array_merge(
214+
isset($this->securitySchemes) ? ['securitySchemes' => collect($this->securitySchemes())->map->jsonSerialize()->toArray()] : []
215+
)]
182216
);
183217
}
184218

@@ -207,30 +241,30 @@ public function generate(): OpenAPI
207241
$servers[] = $serverInstance;
208242
}
209243

210-
$securities = [];
211-
212-
foreach (config('rest.documentation.security') as $security) {
213-
$securityInstance = (new SecurityScheme())
214-
->withDescription($security['description'] ?? '')
215-
->withIn($security['in'] ?? '')
216-
->withType($security['type'] ?? '')
217-
->withName($security['name'] ?? '')
218-
->withBearerFormat($security['bearerFormat'] ?? '')
219-
->withOpenIdConnectUrl($security['openIdConnectUrl'] ?? '')
220-
->withScheme($security['scheme'] ?? '')
244+
$securitySchemes = [];
245+
246+
foreach (config('rest.documentation.securitySchemes') as $securitySchemeKey => $securityScheme) {
247+
$securitySchemeInstance = (new SecurityScheme())
248+
->withDescription($securityScheme['description'] ?? '')
249+
->withIn($securityScheme['in'] ?? '')
250+
->withType($securityScheme['type'] ?? '')
251+
->withName($securityScheme['name'] ?? '')
252+
->withBearerFormat($securityScheme['bearerFormat'] ?? '')
253+
->withOpenIdConnectUrl($securityScheme['openIdConnectUrl'] ?? '')
254+
->withScheme($securityScheme['scheme'] ?? '')
221255
->withFlows($oauthFlows = new OauthFlows());
222256

223-
foreach ($security['flows'] ?? [] as $key => $flow) {
257+
foreach ($securityScheme['flows'] ?? [] as $key => $flow) {
224258
$flowInstance = (new OauthFlow())
225259
->withScopes($flow['scopes'] ?? [])
226260
->withAuthorizationUrl($flow['authorizationUrl'] ?? '')
227-
->withTokenUrl($flow['tokenUrl'])
228-
->withRefreshUrl($flow['refreshUrl']);
261+
->withTokenUrl($flow['tokenUrl'] ?? '')
262+
->withRefreshUrl($flow['refreshUrl'] ?? '');
229263

230264
$oauthFlows->{'with'.Str::studly($key)}($flowInstance);
231265
}
232266

233-
$securities[] = $securityInstance;
267+
$securitySchemes[$securitySchemeKey] = $securitySchemeInstance;
234268
}
235269

236270
return Rest::applyDocumentationCallback(
@@ -242,7 +276,8 @@ public function generate(): OpenAPI
242276
->withPaths(
243277
$this->generatePaths()
244278
)
245-
->withSecurity($securities)
279+
->withSecuritySchemes($securitySchemes)
280+
->withSecurity(config('rest.documentation.security'))
246281
->withServers($servers)
247282
);
248283
}

0 commit comments

Comments
 (0)