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:
nc -l 9000 | grep -i author &
curl -s --max-time 1 -H 'Authorization: Bearer eyJhbGc' localhost:9000/xBasic auth
Username and password, colon-joined and base64-encoded:
nc -l 9000 | grep -i author &
curl -s --max-time 1 -u user:pass localhost:9000/xEncoded, not encrypted — decoding needs no key:
printf 'dXNlcjpwYXNz' | base64 -dThe 401 challenge
The server asks; WWW-Authenticate names the scheme it wants:
{ 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 -2Bearer tokens
“Whoever holds this is authorised” — one opaque string, no username:
nc -l 9000 | grep -i author &
curl -s --max-time 1 -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9' \
localhost:9000/xThe 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:
nc -l 9000 | grep -iE '^(GET|referer)' &
curl -s --max-time 1 -e 'http://site/page?token=secret' localhost:9000/xCookies
State bolted onto a stateless protocol. The server sets one:
{ 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-cookieOne cookie per header, so several means several:
{ 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-cookieSending them back
The client returns them in one Cookie header:
nc -l 9000 | grep -i '^cookie' &
curl -s --max-time 1 -b 'session=abc123' localhost:9000/xSeveral cookies share one header, separated by ; :
nc -l 9000 | grep -i '^cookie' &
curl -s --max-time 1 -b 'a=1; b=2' localhost:9000/xHttpOnly and Path were browser instructions, not data. Deleting one means setting it
expired:
{ 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-cookieChapter 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:
{ 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 -2The 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.