Basic Authentication
Basic authentication is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic followed by a space and a base64-encoded string username:password. For example, to authorize as demo / p@55w0rd the client would send
1Authorization: Basic ZGVtbzpwQDU1dzByZA==Note: Because base64 is easily decoded, Basic authentication should only be used together with other security mechanisms such as HTTPS/SSL.
Describing Basic Authentication
Using OpenAPI 3.0, you can describe Basic authentication as follows:
1openapi: 3.0.42---3components:4 securitySchemes:5 basicAuth: # <-- arbitrary name for the security scheme6 type: http7 scheme: basic8
9security:10 - basicAuth: [] # <-- use the same name hereThe first section, securitySchemes, defines a security scheme named basicAuth (an arbitrary name). This scheme must have type: http and scheme: basic. The security section then applies Basic authentication to the entire API. The square brackets [] denote the security scopes used; the list is empty because Basic authentication does not use scopes. security can be set globally (as in the example above) or on the operation level. The latter is useful if only a subset of operations require Basic authentication:
1paths:2 /something:3 get:4 security:5 - basicAuth: []Basic authentication can also be combined with other authentication methods as explained in Using Multiple Authentication Types.
401 Response
You can also define the 401 “Unauthorized” response returned for requests with missing or incorrect credentials. This response includes the WWW-Authenticate header, which you may want to mention. As with other common responses, the 401 response can be defined in the global components/responses section and referenced elsewhere via $ref.
1paths:2 /something:3 get:4 ...5 responses:6 ...7 '401':8 $ref: '#/components/responses/UnauthorizedError'9 post:10 ...11 responses:12 ...13 '401':14 $ref: '#/components/responses/UnauthorizedError'15...16components:17 responses:18 UnauthorizedError:19 description: Authentication information is missing or invalid20 headers:21 WWW_Authenticate:22 schema:23 type: stringTo learn more about the responses syntax, see Describing Responses.
Did not find what you were looking for? Ask the community
Found a mistake? Let us know