Skip to content

Commit d3c27d8

Browse files
authored
Merge pull request #267 from mattpolzin/security-scheme-documentation
Add some documentation on using security schemes in OpenAPIKit.
2 parents 216295e + 9dbaa80 commit d3c27d8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,50 @@ In the OpenAPI specifcation, a security requirement (like can be found on the ro
156156

157157
OpenAPIKit defines the `SecurityRequirement` typealias as a dictionary with `JSONReference` keys; These references point to the Components Object and provide a slightly stronger contract than the String values required by the OpenAPI specification. Naturally, these are encoded to JSON/YAML as String values rather than JSON References to maintain compliance with the OpenAPI Specification.
158158

159+
To give an example, let's say you want to describe OAuth 2.0 authentication via the implicit flow. First, define a Security Scheme:
160+
```swift
161+
let oauthScheme = OpenAPI.SecurityScheme.oauth2(
162+
flows: .init(
163+
implicit: .init(
164+
authorizationUrl: URL(string: "https://my-api.com/oauth2/auth")!,
165+
scopes: ["read:widget": "read widget"]
166+
)
167+
)
168+
)
169+
```
170+
171+
Next, store it in your OpenAPI document's Components Object (which likely has other entries but we'll only specify the security schemes for this example):
172+
```swift
173+
let components = OpenAPI.Components(
174+
securitySchemes: ["implicit-oauth": oauthScheme]
175+
)
176+
```
177+
178+
Finally, your OpenAPI Document should use the Components Object we just created and also reference the OAuth implicit scheme via an internal JSON reference:
179+
```swift
180+
let document = OpenAPI.Document(
181+
info: .init(title: "API", version: "1.0"),
182+
servers: [],
183+
paths: [:],
184+
components: components,
185+
security: [[.component( named: "implicit-oauth"): ["read:widget"]]]
186+
)
187+
```
188+
189+
If your API supports multiple alternative authentication strategies (only one of which needs to be used), you might have additional entries in your Document's Security array:
190+
```swift
191+
let document = OpenAPI.Document(
192+
info: .init(title: "API", version: "1.0"),
193+
servers: [],
194+
paths: [:],
195+
components: components,
196+
security: [
197+
[.component( named: "implicit-oauth"): ["read:widget"]],
198+
[.component( named: "auth-code-oauth"): ["read:widget"]]
199+
]
200+
)
201+
```
202+
159203
#### Specification Extensions
160204
Many OpenAPIKit types support [Specification Extensions](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#specification-extensions). As described in the OpenAPI Specification, these extensions must be objects that are keyed with the prefix "x-". For example, a property named "specialProperty" on the root OpenAPI Object (`OpenAPI.Document`) is invalid but the property "x-specialProperty" is a valid specification extension.
161205

0 commit comments

Comments
 (0)