Skip to content

Commit 7e7a7ab

Browse files
authored
Merge pull request #6699 from erjohnson/token-storage
Update where to store tokens information
2 parents c651679 + 1a48ac1 commit 7e7a7ab

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

articles/security/store-tokens.md

+15-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
description: Explains how to securely store tokens used in token-based authentication.
2+
description: In this guide we outline how to store tokens used in token-based authentication.
3+
toc: true
34
topics:
45
- security
56
- security-bulletins
@@ -10,42 +11,24 @@ contentType:
1011
useCase:
1112
- development
1213
---
13-
# Where to Store Tokens
14-
15-
This document explains how to securely store tokens used in token-based authentication. The following assumes a basic knowledge of JSON Web Tokens (JWTs). To learn more about JWTs see:
16-
17-
* [Auth0 Tokens](/tokens)
18-
* [JSON Web Tokens (JWT) in Auth0](/jwt)
19-
20-
## Where to Store Your JWTs
21-
22-
::: warning
23-
You __must__ [verify a JWT's signature](/tokens/id-token#verify-the-signature) before storing and using it.
24-
:::
2514

26-
With token-based authentication, you are given the choice of where to store the JWT. We strongly recommend that you store your tokens in local storage/session storage or a cookie.
27-
28-
### Web Storage (local storage/session storage)
29-
30-
Commonly, the JWT is placed in the browsers local storage and this works well for most use cases.
15+
# Where to Store Tokens
3116

32-
When logging in a user with a username and password, the response body contains the Access Token JWT. Then you need to handle this response in the client side code. This token can then be stored in localStorage or sessionStorage.
17+
Not sure where to store [tokens](/tokens)? This guide outlines how to securely store tokens used in token-based authentication.
3318

34-
[Click here for an example using sessionStorage](https://github.com/auth0-blog/angular-token-auth/blob/master/auth.client.js#L31)
19+
## Don't store tokens in local storage
3520

36-
Both `localStorage` and `sessionStorage` both extend `Storage`. The only difference between them is the persistance of the data:
21+
Browser local storage (or session storage) is not secure. Any data stored there may be vulnerable to [cross-site scripting](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). If an attacker steals a token, they can gain access to and make requests to your API. Treat tokens like credit card numbers or passwords: don’t store them in local storage.
3722

38-
`localStorage` - data persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.
23+
## If a backend is present
3924

40-
`sessionStorage` - Changes made are saved and available for the current page, as well as future visits to the site on the same window. Once the window is closed, the storage is deleted.
25+
If your application has a backend server at all, then tokens should be handled server-side using the [Authorization Code flow](/application-auth/current/server-side-web), [Authorization Code flow with Proof Key for Code Exchange](/application-auth/current/mobile-desktop), or hybrid flow.
4126

42-
#### Web Storage Disadvantages
27+
## Single page applications
4328

44-
* Unlike cookies, local storage is sandboxed to a specific domain and its data cannot be accessed by any other domain including sub-domains.
45-
* Web Storage is accessible through JavaScript on the same domain so any JavaScript running on your site will have access to web storage, and because of this can be vulnerable to cross-site scripting (XSS) attacks.
46-
* The developer must ensure that the JWT is always sent over HTTPS and never HTTP.
29+
If you have a single page application (SPA) with no corresponding backend server, your SPA should request new tokens on page load and store them in memory without any persistence. To make API calls, your SPA would then use the in-memory copy of the token.
4730

48-
### Using Cookies
31+
## Using cookies
4932

5033
You can also use cookies to store the JWT. The exact way to set a cookie depends on the client side language you are using.
5134

@@ -55,8 +38,9 @@ There are different options to control the lifetime of a cookie:
5538
* Implement a server side check (typically done for you by the web framework in use), and you could implement expiration or sliding window expiration.
5639
* Cookies can be persistent (not destroyed after the browser is closed) with an expiration.
5740
* Cookies can be read by both the JavaScript and the server side code or only server side if the `httpOnly` flag is set.
41+
* You can set the `secure=true` flag so cookies can only be set over an encrypted connection.
5842

59-
## Understanding Sessions and Cookies
43+
### Understanding sessions and cookies
6044

6145
:::warning
6246
Please be sure to refer to the [Lock API documentation](/libraries/lock/v10/api) for the most up-to-date code snippets.
@@ -66,16 +50,12 @@ Please be sure to refer to the [Lock API documentation](/libraries/lock/v10/api)
6650

6751
This video will show you how to handle session data when building a web app. It will help you understand how your application uses cookies and sessions to manage the state of an authenticated user. This video example uses Node.js with Passport, but the techniques apply to any traditional server-based web application.
6852

69-
#### Cookie Disadvantages
53+
### Disadvantages of cookies
7054

7155
* The max size of a cookie is only 4kb so that may be problematic if you have many claims attached to the token.
72-
* Cookies can be vulnerable to cross-site request forgery (CSRF or XSRF) attacks. This type of attack occurs when a malicious web site causes a user’s web browser to perform an unwanted action on a trusted site where the user is currently authenticated. This is an exploit of how the browser handles cookies. Using a web app framework’s CSRF protection makes cookies a secure option for storing a JWT. CSRF can also be partially prevented by checking the HTTP `Referer` and `Origin` header.
56+
* Cookies can be vulnerable to cross-site request forgery (CSRF or XSRF) attacks. Using a web app framework’s CSRF protection makes cookies a secure option for storing a JWT. CSRF can also be partially prevented by checking the HTTP `Referer` and `Origin` header. You can also set the `SameSite=strict` cookie flag to prevent CSRF attacks.
7357
* Can be difficult to implement if the application requires cross-domain access. Cookies have additional properties (Domain/Path) that can be modified to allow you to specify where the cookie is allowed to be sent.
7458

75-
## How to implement
76-
77-
Many of our Quickstarts demonstrate how to store tokens, and the exact implementation will be different based on the technology being used. Please refer to the Quickstarts at the top of our [Documentation Home Page](/), select the appropriate Quickstart based on your application type, and follow the code samples provided.
78-
7959
## Keep reading
8060

8161
::: next-steps

0 commit comments

Comments
 (0)