Skip to content

Commit 958d95d

Browse files
committed
open api security option added
1 parent 464851a commit 958d95d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

config/request-docs.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@
161161
],
162162
],
163163
],
164+
//openapi export with security configuration,
165+
// if type set null then doc will exclude global security schema.
166+
// Ref: https://spec.openapis.org/oas/v3.0.3#security-scheme-object
167+
'security' => [
168+
//available options [null, bearer, basic, apikey]
169+
'type' => 'bearer',
170+
//Note: only works for "apikey", available options [query, header, cookie]
171+
'position' => 'header',
172+
'key_name' => 'api_key'
173+
],
164174
],
165175

166176
//export request docs as json file from terminal

src/LaravelRequestDocsToOpenApi.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function openApi(array $docs): LaravelRequestDocsToOpenApi
2323
];
2424

2525
$this->docsToOpenApi($docs);
26+
$this->appendGlobalSecurityScheme();
2627
return $this;
2728
}
2829

@@ -190,6 +191,48 @@ protected function getAttributeType(string $rule): string
190191
return "object";
191192
}
192193

194+
protected function appendGlobalSecurityScheme(): void
195+
{
196+
$securityType = config('request-docs.open_api.security.type');
197+
198+
if ($securityType == null) {
199+
return;
200+
}
201+
202+
switch ($securityType) {
203+
case 'bearer':
204+
$this->openApi['components']['securitySchemes']['bearerAuth'] = [
205+
'type' => 'http',
206+
'scheme' => 'bearer'
207+
];
208+
$this->openApi['security'][] = [
209+
'bearerAuth' => []
210+
];
211+
break;
212+
213+
case 'basic':
214+
$this->openApi['components']['securitySchemes']['basicAuth'] = [
215+
'type' => 'http',
216+
'scheme' => 'basic'
217+
];
218+
$this->openApi['security'][] = [
219+
'basicAuth' => []
220+
];
221+
break;
222+
223+
case 'apikey':
224+
$this->openApi['components']['securitySchemes']['apiKeyAuth'] = [
225+
'type' => 'apiKey',
226+
'name' => config('request-docs.open_api.security.key_name', 'api_key'),
227+
'in' => config('request-docs.open_api.security.position', 'header')
228+
];
229+
$this->openApi['security'][] = ['apiKeyAuth' => []];
230+
break;
231+
232+
default:
233+
break;
234+
}
235+
}
193236
/**
194237
* @codeCoverageIgnore
195238
*/

0 commit comments

Comments
 (0)