Session, JWT and OAuth

Session, JWT and OAuth An architecture diagram generated by Archify. Browser · holds whatever it was given · Architecture component Browser holds whatever it was given Your API · works out who is asking · Architecture component · the same request either way Your API works out who is asking the same request either way Cookie sid=ab9 · HttpOnly, Secure, SameSite · Session: delete the row, and the next request 401s Cookie sid=ab9 HttpOnly, Secure, SameSite Session store · one row, and you can delete it · Session: delete the row, and the next request 401s · 401 on the next request Session store one row, and you can delete it 401 on the next request Signed token · header.payload.signature · JWT: nothing to delete, so it works until exp Signed token header.payload.signature Verify with your key · nothing is looked up · JWT: nothing to delete, so it works until exp · works until exp says otherwise Verify with your key nothing is looked up works until exp says otherwise Authorization server · the login happens over there · OAuth: revoked at the next refresh, not before Authorization server the login happens over there One-use code · bound to the caller by PKCE · OAuth: revoked at the next refresh, not before One-use code bound to the caller by PKCE Access and refresh · the access token is a JWT · OAuth: revoked at the next refresh, not before · dead at the next refresh Access and refresh the access token is a JWT dead at the next refresh GET /me 3 ms a request 0.05 ms, no lookup one-use code swapped with the verifier Session: delete the row, and the next request 401s JWT: nothing to delete, so it works until exp OAuth: revoked at the next refresh, not before Legend The browser Where the session lives Your API What proves it Somebody else

Session

  • • An id in a cookie. HttpOnly keeps it out of JavaScript, Secure off plain http, SameSite decides who can make the browser send it
  • • Every request pays a store lookup, and sends 48 bytes to get it
  • • An in-memory store behind two instances signs people out at random

JWT

  • • Signed is not encrypted. Whoever holds it reads the claims
  • • Verify with the algorithm you expected, never the one the token asks for. alg none, and RS256 keys used as HS256 secrets, are the classic breaks
  • • A denylist buys revocation back by putting the lookup on every request again

OAuth

  • • PKCE binds the code to whoever started the flow. state stops somebody else's code being planted in your session
  • • An access token says this app may call that API. Who anybody is is the id_token
  • • The refresh token is the long-lived credential. Rotate it on every use