@@ -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