Token theft and replay
A bearer access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. works for whoever presents it. This lesson follows a copy of the printer's token for Ava's photos from the place it leaks to the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. that accepts it, what limits such a copy, and how binding a token to a key makes a copy useless.
Where access tokens escape
The attacks so far went after the authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it., the credential on its way to becoming a token. Once the printer holds an access token for Ava Archer's photos, the question changes. As a bearer tokenA token that works for whoever presents it: the API checks that the token is genuine and current, not who is holding it. That is why bearer tokens must be kept from being copied., it works for whoever presents it, and the printer sends it to the photo API with every request, which gives a copy many chances to escape.
Tokens rarely leak because someone broke the cryptography. They leak because a copy ends up somewhere it was never meant to be.
| Route | How a copy gets out |
|---|---|
| Logs | The printer records the full request when a photo API call fails, Authorization header included, or a debugging proxy records every header it sees. |
| URLs | A developer puts the token in a query parameter for convenience. The address then lands in browser history, server logs, and Referer headers, which is why Using access tokens kept tokens in the header. |
| Browser storage | A browser application such as the photo editor keeps tokens where injected script can reach them, as Browser applications described. |
| Proxies | A proxy that ends the TLS connection in front of an API sees every request in plain form, and its logs or caches can keep what it sees. |
| A compromised API | An attacker who controls the photo API, or can only read its logs, collects every token that clients present to it. |
| A counterfeit API | A client that learns an API's address at run time, for example from a setting a person types in, can be pointed at a server built to collect tokens. |
| A misdirected request | A client follows an address from a response, such as a link to the next page of results on another host, and attaches the token without checking where it is going. |
In the last three, the token reaches the wrong hands through the front door, in an ordinary API request that the client chose to send. Clients should check where their tokens go, as Using access tokens described, but experience shows many do not. That is why the most dependable limits on a copy are set by the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. and checked by the API, instead of depending on every client behaving well.
Replaying a copied token
All domains and tokens in these examples are fictional. Suppose the printer's error logging captured a request at 09:02 UTC, and someone with access to the logs copied the token. At 09:03, from their own laptop, they send this:
GET /albums/42/photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer eyJhbGciOiJFUzI1NiIsImtpZCI6InBob3Rvcy0yMDI2LTA5IiwidHlwIjoiYXQrand0In0...The token is the JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. access token from Token formats and validation, shortened here for display. Nothing distinguishes this request from one the printer would make. The signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message. verifies, the issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts. and audience are right, the token has not expired, and photos.read covers the operation, so the photo API returns Ava's album. Replaying a token means exactly this: presenting a copy as if the sender were the party it was issued to, and nothing in a bearer token tells the API who is presenting it.
What limits the copy is written in the token itself:
"aud": "https://api.photos.example",
"scope": "photos.read",
"exp": 1790845800exp is 09:10:00 UTC on 1 October 2026, seven minutes after the replay. Until then, the copy works. Revoking it helps only at APIs that check with the authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. server. An API that validates the JWT locally keeps accepting it until it expires, as Token revocation explained.
The copy also does only what the token allows. With photos.read, the attacker cannot delete Ava's photos. With an audience of https://api.photos.example, the sharing API at https://share.photos.example refuses it: the token was not issued for that API, as Scopes and audiences explained.
Limiting the damage
No single measure stops a copied bearer token. Several together shrink what a copy is worth.
| Measure | What it limits |
|---|---|
| Short lifetimes | How long a copy works. Refresh tokens are what make lifetimes of minutes practical. |
| Audience restriction | Where a copy works: one API, not every API that trusts the issuer. |
| Minimal scope | What a copy can do: read photos, not delete them. |
| TLS on every connection | Copies taken from the network in transit. |
| Keeping tokens out of URLs and logs | The most common leaks. Logging code removes Authorization headers and token parameters before anything is written. |
| Detection | How long a theft goes unnoticed. Signs include requests from unexpected networks for a client, unusual request rates, and attempts to use tokens that were already revoked. |
Current guidance spells out several of these. Access tokens should carry the minimum privileges the use case needs and should be restricted to a specific API, or a small set of them, and every API must refuse a token that was not meant for it. Clients must not send access tokens in a URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark. query parameter, and TLSTransport Layer Security, the protection behind HTTPS. It encrypts traffic between two connected systems, detects changes on the way, and uses certificates so the client can confirm which server it reached. is recommended all the way from the client to the API. It also says an API must treat the tokens it receives like other sensitive secrets, never storing or passing them on in plain form.
Binding a token to its holder
All of those measures limit a copy. None of them makes a copy useless, because a bearer token is complete by itself. A sender-constrained token is not. It is bound to a key that the client holds, and the API accepts it only from a sender who can prove possession of that key.
The idea works like this. The client generates a key pairA private key and a public key generated together and mathematically linked. The private key creates signatures and the public key checks them, but the private key cannot practically be worked out from the public one. and keeps the private key. When the authorization server issues the token, it records which key the token belongs to. In a JWT access token, that record is a confirmation claim carrying a fingerprint of the public key, or of a certificate that contains it. With each API request, the client sends a fresh proof made with the private key, or proves possession of it through its TLS connection. The API checks that the proof matches the key named in the token. Someone who copies the token from a log does not have the private key, so the copy fails.
The printer's request
- Signature, issuer, audience
- Valid
- exp
- 09:10 UTC, not reached
- photos.read
- Covers the request
- Proof of the client key
- Not needed
Album returned
A copy, replayed at 09:03
- Signature, issuer, audience
- Valid
- exp
- 09:10 UTC, not reached
- photos.read
- Covers the request
- Proof of the client key
- Not needed
Album returned to the copier
A copy, replayed at 09:03
- Signature, issuer, audience
- Valid
- exp
- 09:10 UTC, not reached
- photos.read
- Covers the request
- Proof of the client key
- Missing: no private key
Refused
Binding protects against copies, not against an attacker who controls the client.
Binding protects against copies, not against an attacker who controls the client. Script injected into the photo editor can use the editor's key while the page is open, just as it can use the token. If the key is kept where it cannot be exported, though, the key itself cannot travel to the attacker's own machine the way a copied token can. The script can still prepare proofs in advance while the page is open, which is why servers tie each proof to one access token and can also require a fresh value of their own in it.
Current guidance says authorization servers and APIs should use sender-constrained access tokens. Advanced OAuth covers the two standard mechanisms, DPoP and mutual TLS, with their proofs and the checks at each end.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Measure what limits a copied access tokenReplay one of your own access tokens from another terminal and measure each limit on it: audience, scope, lifetime, revocation and clean logs, then find where the tenant offers to bind tokens to a key.