Skip to content

Cookie Authentication

Cookie authentication uses HTTP cookies to authenticate client requests and maintain session information. It works as follows:

  1. The client sends a login request to the server.
  2. On the successful login, the server response includes the Set-Cookie header that contains the cookie name, value, expiry time and some other info. Here is an example that sets the cookie named JSESSIONID:
1
Set-Cookie: JSESSIONID=abcde12345; Path=/; HttpOnly
  1. The client needs to send this cookie in the Cookie header in all subsequent requests to the server.
1
Cookie: JSESSIONID=abcde12345
  1. On the logout operation, the server sends back the Set-Cookie header that causes the cookie to expire.

Note: Cookie authentication is vulnerable to Cross-Site Request Forgeries (CSRF) attacks, so it should be used together with other security measures, such as CSRF tokens.

Note for Swagger UI and Swagger Editor users: Cookie authentication is currently not supported for “try it out” requests due to browser security restrictions. See this issue for more information. SwaggerHub does not have this limitation.

In OpenAPI 3.0 terms, cookie authentication is an API key that is sent in: cookie. For example, authentication via a cookie named JSESSIONID is defined as follows:

1
openapi: 3.0.0
2
---
3
# 1) Define the cookie name
4
components:
5
securitySchemes:
6
cookieAuth: # arbitrary name for the security scheme; will be used in the "security" key later
7
type: apiKey
8
in: cookie
9
name: JSESSIONID # cookie name
10
11
# 2) Apply cookie auth globally to all operations
12
security:
13
- cookieAuth: []

In this example, cookie authentication is applied globally to the whole API using the security key at the root level of the specification. If cookies are required for just a subset of operations, apply security on the operation level instead of doing it globally:

1
paths:
2
/users:
3
get:
4
security:
5
- cookieAuth: []
6
description: Returns a list of users.
7
responses:
8
"200":
9
description: OK

Cookie authentication can be combined with other authentication methods as explained in Using Multiple Authentication Types.

You may also want to document that your login operation returns the cookie in the Set-Cookie header. You can include this information in the description, and also define the Set-Cookie header in the response headers, like so:

1
paths:
2
/login:
3
post:
4
summary: Logs in and returns the authentication cookie
5
requestBody:
6
required: true
7
description: A JSON object containing the login and password.
8
content:
9
application/json:
10
schema:
11
$ref: "#/components/schemas/LoginRequest"
12
security: [] # no authentication
13
responses:
14
"200":
15
description: >
16
Successfully authenticated.
17
The session ID is returned in a cookie named `JSESSIONID`. You need to include this cookie in subsequent requests.
18
headers:
19
Set-Cookie:
20
schema:
21
type: string
22
example: JSESSIONID=abcde12345; Path=/; HttpOnly

Note that the Set-Cookie header and securitySchemes are not connected in any way, and the Set-Header definition is for documentation purposes only.

Did not find what you were looking for? Ask the community
Found a mistake? Let us know