Skip to content
Sign inGet all access
Chapter 9·10 minFree

Authentication and State

Authorization, bearer tokens, cookies and sessions.

HTTP forgets you between requests. Everything here reminds it.

The Authorization header

One header carries credentials of any kind, and the first word names the scheme:

terminal
nc -l 9000 | grep -i author &
curl -s --max-time 1 -H 'Authorization: Bearer eyJhbGc' localhost:9000/x
OutputAuthorization: Bearer eyJhbGc

Basic auth

Username and password, colon-joined and base64-encoded:

terminal
nc -l 9000 | grep -i author &
curl -s --max-time 1 -u user:pass localhost:9000/x
OutputAuthorization: Basic dXNlcjpwYXNz

Encoded, not encrypted — decoding needs no key:

printf 'dXNlcjpwYXNz' | base64 -d
Outputuser:pass

The 401 challenge

The server asks; WWW-Authenticate names the scheme it wants:

terminal
{ printf 'HTTP/1.1 401 Unauthorized\r\n'
  printf 'WWW-Authenticate: Basic realm="api"\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/x | head -2
OutputHTTP/1.1 401 UnauthorizedWWW-Authenticate: Basic realm="api"

Bearer tokens

“Whoever holds this is authorised” — one opaque string, no username:

terminal
nc -l 9000 | grep -i author &
curl -s --max-time 1 -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9' \
  localhost:9000/x
OutputAuthorization: Bearer eyJhbGciOiJIUzI1NiJ9

The format is the server’s business, never the protocol’s. A bearer token is a password with an expiry, so leaking one leaks the account.

API keys

A key in a query string travels further than you meant. Referer carries the whole URL to a third party:

terminal
nc -l 9000 | grep -iE '^(GET|referer)' &
curl -s --max-time 1 -e 'http://site/page?token=secret' localhost:9000/x
OutputGET /x HTTP/1.1Referer: http://site/page?token=secret

Cookies

State bolted onto a stateless protocol. The server sets one:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Set-Cookie: session=abc123; HttpOnly; Path=/\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i set-cookie
OutputSet-Cookie: session=abc123; HttpOnly; Path=/

One cookie per header, so several means several:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Set-Cookie: a=1\r\n'
  printf 'Set-Cookie: b=2\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i set-cookie
OutputSet-Cookie: a=1Set-Cookie: b=2

Sending them back

The client returns them in one Cookie header:

terminal
nc -l 9000 | grep -i '^cookie' &
curl -s --max-time 1 -b 'session=abc123' localhost:9000/x
OutputCookie: session=abc123

Several cookies share one header, separated by ; :

terminal
nc -l 9000 | grep -i '^cookie' &
curl -s --max-time 1 -b 'a=1; b=2' localhost:9000/x
OutputCookie: a=1; b=2

HttpOnly and Path were browser instructions, not data. Deleting one means setting it expired:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Set-Cookie: s=; Max-Age=0\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i set-cookie
OutputSet-Cookie: s=; Max-Age=0

Chapter 11 covers SameSite and Secure, where cookies stop being simple.

Sessions versus tokens

The question is where the state lives:

Session cookie Bearer token
Cookie holds an id the claims themselves
Server stores the session nothing
Revoke delete the row wait for expiry
Scaling shared store needed none
Size tiny grows with the claims

Expiry and refresh

Expiry is a conversation. The server says why in the challenge:

terminal
{ printf 'HTTP/1.1 401 Unauthorized\r\n'
  printf 'WWW-Authenticate: Bearer error="invalid_token", '
  printf 'error_description="expired"\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/x | head -2
OutputHTTP/1.1 401 UnauthorizedWWW-Authenticate: Bearer error="invalid_token", error_description="expired"

The client swaps a refresh token for a new access token and retries. Short access tokens limit a leak; the refresh token is what to protect.