Skip to content

Sessions and tokens: what the check costs, and what it cannot undo

Authentication happens once, and the service needs a proof of it on every later request. The session-handling lesson of the scalability module looked at where the session data can live: in an instance, in a shared store, or in a token the client carries. This lesson asks a different question about the same choices: what does it cost the service, in messages and in time, to check the proof, and what can it not undo?

Three designs are compared.

  • A session cookie. The service keeps the session and the cookie names it. Checking means looking the session up.
  • A JWT checked locally. The client holds a signed token and the service verifies the signature and the claims itself.
  • An opaque token with introspection. The token means nothing to the service, so it asks the authorization server about it.

RFC 6749 gives the roles of an OAuth 2.0 request their names. The resource owner can grant access to a protected resource. The client is the application making requests on the owner’s behalf. The resource server hosts the protected resource and answers requests that carry an access token, and the authorization server issues access tokens once it has authenticated the owner. In the flow below, the service is the resource server.

An access token is, in the RFC’s words, a credential used to access protected resources. It may be opaque, an identifier that points to data kept on the server, or it may carry the authorization information itself in a verifiable way, as a string of data and a signature. That one sentence is the whole difference between the second and third designs.

Try it: the same first request, three ways

Section titled “Try it: the same first request, three ways”

The flow starts with the session cookie. Choose Bearer JWT, checked locally and Opaque token, introspection in the comparison list and watch the messages. The service and the authorization server are two machines, and the token variants open a connection to each.

TLS, then who you are, then what you may do

80 ms
80 ms
10 ms
10 ms
Compare

The service checks the password, keeps the session in its own memory and gives the client a cookie that names it. Each later request carries the cookie and the service looks the session up. One connection, two requests.

340 ms
  1. TCP handshake40 ms
    SYN
    Client to Service: SYN. Arrives at 40 ms.
  2. 80 ms
    SYN-ACK
    Service to Client: SYN-ACK. Arrives at 80 ms.
  3. TLS 1.3 handshake120 ms
    ACK + ClientHello
    Client to Service: ACK + ClientHello. Arrives at 120 ms.
  4. 160 ms
    ServerHello … Finished
    Service to Client: ServerHello … Finished. Arrives at 160 ms.
  5. Authentication: proving who you are200 ms
    Finished + POST /login (username, password)
    Client to Service: Finished + POST /login (username, password). Arrives at 200 ms.Processing: 10 ms
  6. 250 ms
    200, Set-Cookie: session=…
    Service to Client: 200, Set-Cookie: session=…. Arrives at 250 ms.
  7. Authorization: the request and the check290 ms
    GET /orders, Cookie: session=…
    Client to Service: GET /orders, Cookie: session=…. Arrives at 290 ms.Processing: 10 ms
  8. 340 ms
    200 + orders
    Service to Client: 200 + orders. Arrives at 340 ms.

Time until the first response arrives: 340 ms4 round trips on the wire · 20 ms of processing

Model notes

Assumptions

  • Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
  • TLS 1.3 over TCP with a full handshake is used for every new connection, as in the connection-setup flow: one round trip for TCP, one for TLS, and the client’s first request travels with its Finished message. One processing time stands for everything a server does with a message: checking a password, creating or looking up a session, issuing a token, verifying a signature, checking a permission.
  • The first request only. The authorization server is a different host from the service, so the two token variants open a connection to each. The cookie variant signs in at the service itself.

Simplifications

  • A single request with the credentials stands for the whole sign-in. A browser sign-in with the OAuth authorization code flow adds redirects and the time the person takes to type, and neither is drawn. The token request is the token endpoint of RFC 6749, which must be reached over TLS.
  • The service already holds the key that checks the signature of a JWT, so it verifies the token without any message. Fetching that key the first time, and again when it is rotated, costs a call to the authorization server that is not drawn. A JWT stays valid until it expires; the model does not show how to revoke one earlier.
  • The introspection call goes over a connection that is already open and has no cache: every request pays it. A service can keep the answer for a short time, which removes the call for the requests that follow.
  • The session is stored in the memory of the service, so finding it costs no message. A session store shared by several instances adds a call to the store on every request.
  • Refresh tokens, certificate validation time, protection against cross-site request forgery, the size of the credentials on the wire and the requests that follow the first one are left out. Later requests over the same connection cost one round trip, plus the introspection call for an opaque token.

Parameter values are illustrative. They are not benchmarks of any real product.

With the starting settings the messages and totals are:

Variant Messages First response
Session cookie 8 340 ms
Bearer JWT, checked locally 12 500 ms
Opaque token, introspection 14 520 ms

Read the table in two steps.

The JWT is 160 ms slower than the cookie, and the check is not why. The cookie variant signs in at the service itself, so it opens one connection: 4 round trips of 80 ms plus two processing times of 10 ms. The token variants sign in at the authorization server and then open a second connection to the service, which adds a TCP and a TLS round trip: 6 round trips plus the same processing, 500 ms. The extra 160 ms is the price of the second connection. The signature check itself costs processing time and no round trip. That is the point of a local check, and it only shows on the requests that follow.

The opaque token adds 20 ms to the JWT’s 500 ms, and it adds it on every request. The service asks the authorization server whether the token is active, which is one round trip of 10 ms between them and one more processing time of 10 ms. Later requests on an open connection then cost a round trip to the service for the cookie and the JWT, and the same plus the introspection call for an opaque token.

Now change a slider. Set Round trip, service to authorization server to 100 ms, as if the authorization server were in another region, and the opaque variant becomes 610 ms while the others stay put. Every request now carries a 100 ms detour. Set it back, and raise Round trip, client to authorization server to 200 ms instead: the JWT becomes 860 ms and the opaque token 880 ms, and the cookie stays at 340 ms, because it never talks to the authorization server.

Mental model. A local check moves the cost from every request to the moment of issue. An introspection call pays for freshness on every request. Neither is free, and the cheaper one is the one that knows less.

RFC 6750 defines a bearer token as a security token with the property that any party in possession of it can use it in any way that any other party in possession of it can. There is no second proof. That is why the RFC requires confidentiality protection with TLS when a token is sent, and why it says token servers should issue short-lived bearer tokens, one hour or less. The sentences are the practical content of “keep it secret, and keep it short”.

A cookie has defences of its own. RFC 6265 defines the Secure attribute, which limits the cookie to secure channels, and notes that it protects only confidentiality: an attacker on the network can still overwrite a cookie from an insecure channel. The HttpOnly attribute keeps the cookie out of scripts, which blocks one way of stealing it.

A JWT is a compact representation of claims that can be signed, so that any holder of the right key can check it. RFC 7519 lists registered claims, among them iss (the issuer), sub (the subject), aud (the audience) and exp (the time after which the token must not be accepted). Its rule for exp is that when the claim is processed, the current date and time must be before the expiration time. Section 7.2 lists the steps for validating a token, and a token that fails any step is rejected.

Everything in that list is information inside the token, or the key that signed it. Nothing in it asks whether the account behind the token still exists, whether the user has logged out, or whether the token was stolen. A JWT checked locally is valid until it expires, or until the service learns something the token cannot tell it.

RFC 7519 does not say how to revoke a token before its expiry time. The words revoke and revocation do not appear in it. That silence is a fact about the specification and not a flaw in it: the RFC defines a token format, and what to do when a token must be cancelled is left to the system around it. A system that wants early revocation has to add something, a list of refused token ids that every service consults, or a call to the authorization server, and either one brings back the state and the messages the local check avoided.

RFC 6750 does name the case: its invalid_token error covers a token that is expired, revoked, malformed or invalid for another reason. So a resource server may refuse a revoked token. The question is how it finds out.

An opaque token solves that by asking. RFC 7662 defines a call in which a protected resource asks the authorization server for the state of a token, and the answer includes active, and may include the scope, exp and client_id. The endpoint must be protected by a transport-layer security mechanism, and the authorization server must require the protected resource to authenticate, so that an attacker cannot poll it for valid token values. For an inactive token, the response should contain only active: false.

The price is the one the flow showed: a round trip and a processing time on every request. The RFC names the way to lower it. A protected resource may cache the response, and the RFC states the trade: a short cache gives fresher information at the cost of more traffic, and a longer one risks stale information, because the token may be revoked while the resource relies on the cached answer, which creates a window in which a revoked token could be used. If the response carries exp, it must not be cached past that time.

So both designs have a window, and they differ in who sets it.

Window in which a cancelled token still works
JWT checked locally Up to the token’s own lifetime, unless the service adds a list or a call
Opaque token, no cache None beyond the time the request takes
Opaque token, cached for 30 seconds Up to 30 seconds

The first row is the lifetime that the authorization server chose. The third is a number the service chose. Neither is a guarantee about a particular product: the table says what the specifications leave open.

Check your understanding: tokens and what they cost

This quiz needs JavaScript to check your answers.

Question 1 of 3

A service accepts a request when the JWT’s signature is valid and its expiry time has not passed, and it checks nothing else. At 10:00 an administrator disables the user’s account. The token expires at 10:45.

What does the service do with a request carrying this token at 10:20?

Choose one answer.

Question 2 of 3

In the course’s flow, a service reaches the authorization server with a round trip of 40 ms, and one processing time is 20 ms. A JWT is checked locally with one processing time. An opaque token is checked by an introspection call, which adds one round trip to the authorization server and one more processing time.

How many milliseconds longer does a request with an opaque token take than one with a JWT?

Enter a number and choose its unit.

milliseconds
Question 3 of 3

RFC 6750 says a bearer token must be sent over TLS. Why?

Choose one answer.

What happens if a token is stolen and cannot wait to expire?

Section titled “What happens if a token is stolen and cannot wait to expire?”

Take a JWT with a lifetime of 1 hour, the longest that RFC 6750 suggests, and a service that checks it locally. The token is stolen the moment it is issued. The account is disabled immediately, but nothing in the token changes. The thief can use it for up to 60 × 60 = 3,600 seconds.

The remedies each move a cost.

  • A shorter lifetime. At 15 minutes, the window is 900 seconds. The client has to obtain a new token four times as often. RFC 6749 describes the refresh token for that: a credential used to obtain access tokens when the current one expires, which is used only with the authorization server. A stolen refresh token then matters more than a stolen access token.
  • A deny list. Services refuse token ids on a list. The window closes, and the service now holds state it must keep consistent, which is what the local check was meant to avoid.
  • Introspection, with a short cache. The window is the cache duration, and every request that misses the cache pays 20 ms at the starting settings.

None of these is the right answer in general. The right one depends on how much damage a stolen token can do in the time left, which is a question about the system and not about the token.

A related consequence follows from the same model. In the flow, a request with a JWT sends no message to the authorization server, so while that server is unreachable, an unexpired token is still accepted and no new sign-in can succeed. An opaque token sends its call on every request, so a service with no cache has no answer to give for any request. Which is better depends on whether serving possibly cancelled tokens is worse than refusing everyone. The model does not say.

  • In OAuth 2.0 the client presents an access token to the resource server, which was issued by the authorization server. A bearer token can be used by whoever holds it, so it travels over TLS and is kept short-lived.
  • With the starting settings, the first request takes 340 ms with a session cookie (8 messages), 500 ms with a JWT (12 messages) and 520 ms with an opaque token (14 messages). The JWT’s extra 160 ms is the second connection, and the opaque token’s extra 20 ms is paid on every request.
  • A local check reads what is in the token and costs no round trip. It cannot know what happened after the token was issued, and RFC 7519 does not describe a way to revoke a token before it expires.
  • Introspection asks every time and can be cached; RFC 7662 says a longer cache risks a window in which a revoked token still works.
  • A stolen token with a lifetime of 1 hour is usable for up to 3,600 seconds, and 900 seconds at 15 minutes. A shorter lifetime, a deny list or introspection each trade the window for refreshes, state or messages.

Next: TLS and encryption, and which threat each one stops.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.