Skip to content

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 word followed by a space and a base64-encoded string username:password. For example, to authorize as demo / p@55w0rd the client would send

1
Authorization: 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:

1
openapi: 3.0.0
2
---
3
components:
4
securitySchemes:
5
basicAuth: # <-- arbitrary name for the security scheme
6
type: http
7
scheme: basic
8
9
security:
10
- basicAuth: [] # <-- use the same name here

The 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:

1
paths:
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.

1
paths:
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
...
16
components:
17
responses:
18
UnauthorizedError:
19
description: Authentication information is missing or invalid
20
headers:
21
WWW_Authenticate:
22
schema:
23
type: string

To 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