Skip to content

Lesson 3 of 7

Signed tokens

Listen to an AI summary of Signed tokens

0:00 / 0:37

A token in place of a session id

Some applications keep no record of sessions on the server. The cookie holds a token instead. A token is a value that proves something to a server, such as who a person is.

A token that said only user: sam would let anyone act as Sam. The server adds a signature, a value that lets the receiver check who made the data and that the data has not changed. The server makes the signature with a key, a secret value only the server knows.

A JSON Web Token, also called a JWT, is a token with a signature that holds the details of a signed-in person. The details are not encrypted, so anyone who holds the token can read the details, and a token holds no secret. JSON is a text format that writes data as names and values inside curly brackets.

  1. { "alg": "HS256" }// the algorithm, the method that made the signature
  2. { "user": "sam", "exp": 1794585600 }// the details, with the time the token stops working
  3. Xk2f9QmB7tLw4hNc// the signature
A real JSON Web Token joins the three parts into one long string.

Checking a token

Code that reads a JSON Web Token checks three things:

  • The signature, on every request.
  • The algorithm, named by the server's code.
  • The expiry time (exp), when the token stops working.

Each piece of JavaScript reads a token with the library jsonwebtoken. Look at the name of the function in each piece.

// Weakness: reads the details and does not check the signature
const details = jwt.decode(token)

// Safer: checks the signature, one algorithm and the expiry
const details = jwt.verify(token, key, { algorithms: ['HS256'] })

The function jwt.decode reads the details and makes no check, so the server accepts a token that anyone wrote. The function jwt.verify checks the signature and the expiry time, and algorithms names the one algorithm the server accepts. A token can name the algorithm none, which means no signature, and some code accepts the token's choice.

The server has no record to delete at sign-out. A JSON Web Token gets a short life, such as 15 minutes, or the server lists the tokens that no longer work.

Three more faults in a token check

Some tokens are signed with a pair of keys. A private key, which only the signer holds, makes the signature. A public key, which anyone may hold, checks the signature. The algorithm RS256 uses a pair of keys, and HS256 uses one secret key for both jobs.

Code that lets the token choose the algorithm can be sent a token marked HS256 and signed with the public key. The code then uses the public key as the secret, and the check passes. The attack is called algorithm confusion. The safer code above names the one algorithm the server accepts. A server that checks with a public key names RS256 and no other algorithm.

A signing secret that is a short word, such as secret, can be guessed. An attacker tries words until one gives the signature of a real token, and can then sign any token. A signing secret is a long value made from a secure random number.

The first part of a token can say where the key is. kid names one of the keys, jku gives a URL to fetch keys from, and jwk holds a key inside the token. Code that fetches keys from the URL in jku, or uses the key in jwk, lets the sender choose the key. Safer code takes its keys from the settings of the server, and looks kid up in a fixed list.

Examples of what to look for

  • A JSON Web Token with a long life.
  • A secret, such as a password, in the details of a token.
  • A token read with jwt.decode, which makes no check, or checked with no algorithm named.
  • A token check that lists two kinds of algorithm together, such as HS256 and RS256.
  • A signing secret that is a short word.
  • Code that fetches keys from a URL in a token (jku), or uses a key carried in a token (jwk).

Check your understanding

  1. State the three things that code checks when the code reads a JSON Web Token.

    Show the answer

    The signature, the algorithm and the expiry time.

  2. What does the function jwt.decode check?

    Show the answer

    jwt.decode makes no check, so the server accepts a token that anyone wrote.

  3. Give one reason why a signing secret that is a short word is a weakness.

    Show the answer

    An attacker can guess the word, and can then sign any token.

Practise it

Challenges with a weakness this lesson teaches.