JSON Web Tokens (JWT)
How access and refresh tokens work in JWT authentication, the endpoints a server needs, and where a client should keep each token.
3 min readUpdated
A JSON Web Token (JWT, RFC 7519) is a compact, signed set of claims. It has three Base64URL-encoded parts separated by dots: a header that names the signing algorithm, a payload with the claims (user ID, roles, expiry time exp), and a signature over the first two parts. A server that holds the key can check the signature and the expiry without a database lookup, which is why JWTs are popular as short-lived access tokens.
This note describes the common access token plus refresh token flow and the server modules that implement it.
Authentication flow
The diagram shows the basic exchange: the client posts the username and password, the server creates a JWT signed with its secret and returns it, and the client then sends the JWT in the Authorization header with every request for a protected resource. The server checks the signature, reads the user information from the token and responds.
Production systems usually add a refresh token to this exchange:
- The user logs in with their credentials. On success the server issues two tokens: an access token valid for a short time (for example 10 minutes) and a refresh token with a longer lifetime (for example 24 hours for applications that handle sensitive data; simpler applications use days or even months).
- On every request to a protected resource, the client sends the access token in a request header.
- When the access token expires, the client sends the refresh token to obtain a new access token. This repeats for as long as the refresh token is valid.
- When the refresh token expires, the user is logged out and has to log in again.
The access token is never looked up on the server, so it cannot be revoked before it expires; the short lifetime limits the damage if it leaks. The refresh token is stored on the server, so logging out or blocking a user takes effect at the next refresh.
Server modules
Five modules implement the flow:
- register: check whether a user with the given username exists. If not, create the user, log them in, generate an access token and a refresh token, and return both.
- login: verify the user’s credentials, generate an access token and a refresh token, and return both.
- generateRefreshToken: check that the refresh token in the request body is valid by looking it up in the database. If it is, take the payload stored in it, sign a new access token with that payload and return it, so the user stays logged in.
- logout: delete the refresh token sent in the request body from the database. The client deletes its copy of the access token.
- checkAuth: middleware for protected routes. Check that the request carries the access token header (
x-auth-tokenin the original implementation) and verify its signature with the key. If the check passes, add the token payload to the request object for the route handlers; otherwise return an error to the client.
Note: the standard place for the access token is the Authorization: Bearer <token> header defined in RFC 6750. A custom header such as x-auth-token works, but standard clients and API gateways expect Authorization.
Store tokens on the client
The original version of this note stored the refresh token in local storage and the access token in a cookie. Local storage is readable by any script on the page, so a single cross-site scripting (XSS) flaw exposes a token that stays valid for hours or days. OWASP recommends against keeping session tokens in local storage. A safer split:
- Keep the refresh token in a cookie with the
HttpOnly,SecureandSameSiteattributes, scoped to the refresh endpoint. Scripts cannot read it, and the browser sends it only where it is needed. - Keep the access token in memory (a JavaScript variable). It is lost on page reload, and the client gets a new one from the refresh endpoint.
References
- RFC 7519: JSON Web Token
- RFC 6750: Bearer token usage
- Introduction to JSON Web Tokens (jwt.io)
- OWASP JSON Web Token Cheat Sheet
- OWASP HTML5 Security Cheat Sheet: local storage
- JJWT: a JWT library for Java
- Generate JWT token and verify in plain Java (Metamug)
- Authentication using JWT and refresh token (Medium)