Skip to content

Commit ca68692

Browse files
committed
open api security options added
1 parent 958d95d commit ca68692

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

config/request-docs.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@
165165
// if type set null then doc will exclude global security schema.
166166
// Ref: https://spec.openapis.org/oas/v3.0.3#security-scheme-object
167167
'security' => [
168-
//available options [null, bearer, basic, apikey]
168+
//available options [null, bearer, basic, apikey, jwt]
169169
'type' => 'bearer',
170-
//Note: only works for "apikey", available options [query, header, cookie]
170+
'name' => 'api_key',
171+
//Note: only works for "apikey" & "jwt", available options [query, header, cookie]
171172
'position' => 'header',
172-
'key_name' => 'api_key'
173173
],
174174
],
175175

src/LaravelRequestDocsToOpenApi.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class LaravelRequestDocsToOpenApi
77
private array $openApi = [];
88

99
/**
10-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
10+
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
1111
* @return $this
1212
*/
1313
public function openApi(array $docs): LaravelRequestDocsToOpenApi
@@ -28,14 +28,14 @@ public function openApi(array $docs): LaravelRequestDocsToOpenApi
2828
}
2929

3030
/**
31-
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
31+
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
3232
* @return void
3333
*/
3434
private function docsToOpenApi(array $docs): void
3535
{
3636
$this->openApi['paths'] = [];
3737
$deleteWithBody = config('request-docs.open_api.delete_with_body', false);
38-
$excludeHttpMethods = array_map(fn ($item) => strtolower($item), config('request-docs.open_api.exclude_http_methods', []));
38+
$excludeHttpMethods = array_map(fn($item) => strtolower($item), config('request-docs.open_api.exclude_http_methods', []));
3939

4040
foreach ($docs as $doc) {
4141
$httpMethod = strtolower($doc->getHttpMethod());
@@ -91,6 +91,7 @@ private function docsToOpenApi(array $docs): void
9191
}
9292
}
9393
}
94+
9495
protected function setAndFilterResponses(Doc $doc): array
9596
{
9697
$docResponses = $doc->getResponses();
@@ -116,12 +117,12 @@ protected function makeQueryParameterItem(string $attribute, $rule): array
116117
$rule = implode('|', $rule);
117118
}
118119
$parameter = [
119-
'name' => $attribute,
120+
'name' => $attribute,
120121
'description' => $rule,
121-
'in' => 'query',
122-
'style' => 'form',
123-
'required' => str_contains($rule, 'required'),
124-
'schema' => [
122+
'in' => 'query',
123+
'style' => 'form',
124+
'required' => str_contains($rule, 'required'),
125+
'schema' => [
125126
'type' => $this->getAttributeType($rule),
126127
],
127128
];
@@ -135,12 +136,12 @@ protected function makePathParameterItem(string $attribute, $rule): array
135136
}
136137

137138
$parameter = [
138-
'name' => $attribute,
139+
'name' => $attribute,
139140
'description' => $rule,
140-
'in' => 'path',
141-
'style' => 'simple',
142-
'required' => str_contains($rule, 'required'),
143-
'schema' => [
141+
'in' => 'path',
142+
'style' => 'simple',
143+
'required' => str_contains($rule, 'required'),
144+
'schema' => [
144145
'type' => $this->getAttributeType($rule),
145146
],
146147
];
@@ -151,10 +152,10 @@ protected function makeRequestBodyItem(string $contentType): array
151152
{
152153
$requestBody = [
153154
'description' => "Request body",
154-
'content' => [
155+
'content' => [
155156
$contentType => [
156157
'schema' => [
157-
'type' => 'object',
158+
'type' => 'object',
158159
'properties' => [],
159160
],
160161
],
@@ -168,9 +169,9 @@ protected function makeRequestBodyContentPropertyItem(string $rule): array
168169
$type = $this->getAttributeType($rule);
169170

170171
return [
171-
'type' => $type,
172+
'type' => $type,
172173
'nullable' => str_contains($rule, 'nullable'),
173-
'format' => $this->attributeIsFile($rule) ? 'binary' : $type,
174+
'format' => $this->attributeIsFile($rule) ? 'binary' : $type,
174175
];
175176
}
176177

@@ -203,6 +204,7 @@ protected function appendGlobalSecurityScheme(): void
203204
case 'bearer':
204205
$this->openApi['components']['securitySchemes']['bearerAuth'] = [
205206
'type' => 'http',
207+
'name' => config('request-docs.open_api.security.name', 'Bearer Authorization Token'),
206208
'scheme' => 'bearer'
207209
];
208210
$this->openApi['security'][] = [
@@ -213,6 +215,7 @@ protected function appendGlobalSecurityScheme(): void
213215
case 'basic':
214216
$this->openApi['components']['securitySchemes']['basicAuth'] = [
215217
'type' => 'http',
218+
'name' => config('request-docs.open_api.security.name', 'Basic Authorization Username and Password'),
216219
'scheme' => 'basic'
217220
];
218221
$this->openApi['security'][] = [
@@ -223,16 +226,30 @@ protected function appendGlobalSecurityScheme(): void
223226
case 'apikey':
224227
$this->openApi['components']['securitySchemes']['apiKeyAuth'] = [
225228
'type' => 'apiKey',
226-
'name' => config('request-docs.open_api.security.key_name', 'api_key'),
229+
'name' => config('request-docs.open_api.security.name', 'api_key'),
227230
'in' => config('request-docs.open_api.security.position', 'header')
228231
];
229232
$this->openApi['security'][] = ['apiKeyAuth' => []];
230233
break;
231234

235+
case 'jwt':
236+
$this->openApi['components']['securitySchemes']['bearerAuth'] = [
237+
'type' => 'http',
238+
'scheme' => 'bearer',
239+
'name' => config('request-docs.open_api.security.name', 'Bearer Authorization Token'),
240+
'in' => config('request-docs.open_api.security.position', 'header'),
241+
'bearerFormat' => 'JWT'
242+
];
243+
$this->openApi['security'][] = [
244+
'bearerAuth' => []
245+
];
246+
break;
247+
232248
default:
233249
break;
234250
}
235251
}
252+
236253
/**
237254
* @codeCoverageIgnore
238255
*/

0 commit comments

Comments
 (0)