Skip to content

Commit 90e93b9

Browse files
authored
Merge pull request #12 from ritza-co/security-examples
security examples
2 parents f8d2960 + ba44688 commit 90e93b9

15 files changed

+1308
-213
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
generation/
22
code/
33
node_modules/
4+
.idea/

reference/security.md

Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# Security
22

3-
The `security` section is a list of [Security Requirement Objects](/openapi/security#security-requirement-object) that apply to all operations in the API (if defined at the [document](/openapi#openapi-document-structure) level) or to a specific operation (if defined at the [operation](/openapi/paths/operations) level).
3+
When designing an API, it is important to consider the security requirements for accessing the API. OpenAPI 3.1 provides a way to define security requirements at both the document and operation levels.
44

5-
Operation-level security requirements override any security requirements defined at the document level.
5+
Security requirements are defined as a list of [Security Requirement Objects](/openapi/security#security-requirement-object) in the `security` section. Each object in the list represents a set of security requirements that must be satisfied to access the API.
6+
7+
To add security to an API as a whole, the `security` keyword must be defined at the [document](/openapi#openapi-document-structure) level.
8+
9+
Likewise, to add security to a specific operation, the `security` keyword must be defined at the [operation](/openapi/paths/operations) level.
10+
11+
Security requirements defined at the operation level override the security requirements defined at the document level.
612

713
If not provided at the document level, the default security requirements are assumed to be `[]`, an empty array, meaning no security is required to access the API.
814

9-
Example:
15+
The following example requires an API key to access the API:
1016

1117
```yaml
1218
security:
@@ -19,17 +25,39 @@ components:
1925
in: header
2026
```
2127
22-
The named security schemes referenced **_must_** be [Security Scheme Objects](/openapi/security/security-schemes) defined in the [Components Object](/openapi/components) under the `securitySchemes` field.
28+
In valid OpenAPI 3.1, the [Security Requirement Objects](/openapi/security#security-requirement-object) listed in `security` sections may only reference [Security Scheme Objects](/openapi/security/security-schemes) that are defined in the [Components Object](/openapi/components) under the `securitySchemes` field. In other words, the `security` section may not contain inline security schemes, and it may not contain security schemes that are not defined yet.
2329

24-
Security can also be made optional by providing an empty object (`{}`) in the list of security requirements. For example:
30+
## Security Requirement Object
2531

26-
```yaml
27-
security:
28-
- apiKey: []
29-
- {}
30-
```
32+
A Security Requirement Object defines a map of security scheme names to [scopes or roles](#security-requirement-scopes-or-roles) that are required to access the API. The names **_must_** match the names of [Security Scheme Objects](/openapi/security/security-schemes) defined in the [Components Object](/openapi/components) under the `securitySchemes` field.
33+
34+
| Field | Type | Required | Description |
35+
| ---------------------- | :------------: | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
36+
| `{securitySchemeName}` | List\<string\> | | A list of [scopes or roles](#security-requirement-scopes-or-roles) required for the security scheme. If the security scheme type is `oauth2` or `openIdConnect`, this is a list of scope names required by the API consumer to be able to access or use the API. For any other type, this could contain a list of roles or similar required for the API consumer to obtain to authenticate with the API. |
37+
38+
## Supported Security Schemes
39+
40+
Before referencing a [Security Scheme](./security/security-schemes.md) as a requirement in the `security` section, it must be defined in the [Components Object](/openapi/components) under the `securitySchemes` field.
41+
42+
OpenAPI 3.1 supports the following security schemes:
43+
44+
- [API Key](./security/security-schemes/security-api-key.md)
45+
- [Basic HTTP](./security/security-schemes/security-basic.md)
46+
- [Bearer Token](./security/security-schemes/security-bearer.md)
47+
- [OAuth 2.0](./security/security-schemes/security-oauth2.md)
48+
- [OpenID Connect](./security/security-schemes/security-openid.md)
49+
- Digest
50+
- Mutual TLS
51+
52+
## Expressing Security Requirements
3153

32-
Security can also be disabled for a specific operation by providing an empty array (`[]`) in the list of security requirements. For example:
54+
The `security` keyword can be used in the following ways to express security requirements.
55+
56+
### Disabling Security
57+
58+
Security can be _disabled_ for a specific operation by providing an empty array (`[]`) in the list of security requirements.
59+
60+
In this example, the `POST` operation in the `/auth` path does not require security:
3361

3462
```yaml
3563
paths:
@@ -38,30 +66,28 @@ paths:
3866
operationId: authenticate
3967
summary: Authenticate with the API
4068
security: [] # Disable security for this operation
41-
requestBody:
42-
required: true
43-
content:
44-
application/json:
45-
schema:
46-
type: object
47-
properties:
48-
username:
49-
type: string
50-
password:
51-
type: string
52-
responses:
53-
"200":
54-
description: The api key to use for authenticated endpoints
55-
content:
56-
application/json:
57-
schema:
58-
type: object
59-
properties:
60-
token:
61-
type: string
69+
# ...
70+
```
71+
72+
### Optional Security
73+
74+
Security can also be made optional by providing an empty object (`{}`) in the list of security requirements.
75+
76+
In this example, the API may be accessed with or without an API key:
77+
78+
```yaml
79+
security:
80+
- apiKey: []
81+
- {}
6282
```
6383

64-
Security can be made completely optional for a specific operation by providing an empty object (`{}`) in the list of security requirements. For example:
84+
### Adding Optional Security to a Specific Operation
85+
86+
Security can be made _optional_ for a specific operation by providing an empty object (`{}`) in the list of security requirements.
87+
88+
This does not disable the security requirements defined at the document level, but makes them optional for this specific operation.
89+
90+
In this example, the `GET` operation in the `/drinks` path _may_ be accessed with or without an API key, but if authenticated, the response will include additional information:
6591

6692
```yaml
6793
paths:
@@ -71,68 +97,66 @@ paths:
7197
summary: Get a list of drinks, if authenticated this will include stock levels and product codes otherwise it will only include public information
7298
security:
7399
- {} # Make security optional for this operation
74-
responses:
75-
"200":
76-
description: A list of drinks
77-
content:
78-
application/json:
79-
schema:
80-
type: array
81-
items:
82-
$ref: "#/components/schemas/Drink"
100+
# ...
83101
```
84102

85-
The combination of different security requirements can be used to express complex authorization scenarios. For example:
103+
### Allowing a Choice of Security Schemes
104+
105+
To allow users to choose between multiple different security requirements, define the `security` keyword as a list of [Security Requirement Objects](/openapi/security#security-requirement-object). The API consumer can choose one of the requirements to authenticate.
106+
107+
In this example, the API may be accessed with an API key **OR** OAuth 2.0:
86108

87109
```yaml
88110
security: # apiKey OR oauth2 can be used
89111
- apiKey: []
90112
- oauth2:
91113
- read
92114
- write
93-
components:
94-
securitySchemes:
95-
apiKey:
96-
type: apiKey
97-
name: Authorization
98-
in: header
99-
oauth2:
100-
type: oauth2
101-
flows:
102-
implicit:
103-
authorizationUrl: https://speakeasy.bar/oauth2/authorize
104-
scopes:
105-
read: Read access to the API
106-
write: Write access to the API
107115
```
108116

109-
The above example allows for the API to be accessed via an API Key **OR** OAuth2.0 with both the `read` and `write` scopes.
117+
### Requiring Multiple Security Schemes Together
118+
119+
If multiple schemes are required together, then the [Security Requirement Object](/openapi/security#security-requirement-object) should be defined with multiple security schemes.
110120

111-
If multiple schemes are required together, then the [Security Requirement Object](/openapi/security#security-requirement-object) can define multiple schemes. For example:
121+
In this example, both an API key **AND** basic auth are required to access the API:
112122

113123
```yaml
114124
security: # both apiKey AND basic is required
115125
- apiKey: []
116126
basic: []
117-
components:
118-
securitySchemes:
119-
apiKey:
120-
type: apiKey
121-
name: X-API-Key
122-
in: header
123-
basic:
124-
type: http
125-
scheme: basic
126127
```
127128

128-
The above example requires both an API Key **AND** basic auth to be provided.
129+
### Complex Authorization Scenarios
129130

130131
This **AND**/**OR** logic along with optional (`{}`) security can be used in any combination to express complex authorization scenarios.
131132

132-
## Security Requirement Object
133+
In this example, the API may be accessed with an API key **AND** OAuth 2.0 **OR** with basic authentication:
133134

134-
A Security Requirement Object defines a map of security scheme names to scopes that are required to access the API. The names **_must_** match the names of [Security Scheme Objects](/openapi/security/security-schemes) defined in the [Components Object](/openapi/components) under the `securitySchemes` field.
135+
```yaml
136+
security: # apiKey AND oauth2 OR basic
137+
- apiKey: []
138+
oauth2:
139+
- read
140+
- write
141+
- basic: []
142+
```
135143

136-
| Field | Type | Required | Description |
137-
| ---------------------- | :------------: | :------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
138-
| `{securitySchemeName}` | List\<string\> | | A list of scopes/roles required for the security scheme. If the security scheme type is `oauth2` or `openIdConnect`, this is a list of scope names required by the API consumer to be able to access or use the API. For any other type, this could contain a list of roles or similar required for the API consumer to obtain to authenticate with the API. |
144+
## Security Requirement Scopes or Roles
145+
146+
When defining an OAuth 2.0 or OpenID Connect [Security Requirement Object](/openapi/security#security-requirement-object) for an operation, the `{securitySchemeName}` field should contain a list of scopes or roles required for the security scheme.
147+
148+
For example, the following security requirement object requires the `read` and `write` scopes for the `oauth2` security scheme:
149+
150+
```yaml
151+
paths:
152+
/drinks:
153+
get:
154+
operationId: listDrinks
155+
summary: Get a list of drinks
156+
# Operation requires read and write scopes
157+
security:
158+
- oauth2:
159+
- read
160+
- write
161+
# ...
162+
```

reference/security/security-api-key.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

reference/security/security-basic.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

reference/security/security-bearer.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

reference/security/security-oauth2.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

reference/security/security-openid.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)