In a mobile wallet app, why should the request pipeline refresh the access token on a 401 but not on a 403?
answer
- who refused: the token or the rule
- the challenge header names the reason
- invalid_token versus insufficient_scope
- a refresh keeps the original grant's scope
- second 401 after a refresh ends it
basics
~20 sA 401 says the credentials themselves were refused, usually an expired access token, so one refresh and one replay can fix it. A 403 says valid credentials lack permission; a refreshed token carries the same rights, so refreshing cannot help.
solid answer
~40 sThe two codes blame different things. A `401` means the server did not accept the credentials on the request: with bearer tokens it carries a `WWW-Authenticate` challenge, typically `error="invalid_token"` for a token that is expired, revoked or malformed, and the client may get a new token and retry. So the pipeline refreshes once and replays the call once. A `403` means the server understood who you are and still refuses: `insufficient_scope`, an account restriction, a frozen wallet. A refresh cannot raise the scope the user originally granted, so the right reaction is to stop and show the user why, not to refresh. And if a call still gets `401` after a successful refresh and replay, the pipeline does not loop: it treats the session as ended and sends the user to sign in.
go deeper
Recall that 401 blames the credentials and 403 blames the permission, and that only the first can be fixed by getting a new access token.
Explain the WWW-Authenticate challenge, the invalid_token versus insufficient_scope errors, and why the pipeline replays each call once rather than looping.
Show the edge cases: a late 401 from a call sent before a refresh, a revoked session after replay, and keeping business refusals visible instead of retried.
Argue for one team-wide error taxonomy the pipeline maps codes into, so every feature reacts to auth, permission and business refusals the same way.
## Two refusals that mean different things An installed wallet app sends a **bearer access token** on almost every call. Sooner or later the server refuses one, and the first thing the client's networking layer must decide is *what* was refused. HTTP gives two codes for this, and they are not interchangeable. | | `401 Unauthorized` | `403 Forbidden` | |---|---|---| | What the server is saying | "I do not accept these credentials" | "I know who you are, and the answer is still no" | | Typical bearer-token reason | `error="invalid_token"`: expired, revoked, malformed | `error="insufficient_scope"`, or a business rule | | Must carry | a `WWW-Authenticate` challenge | nothing special; the body may explain | | Can a new token help? | yes, usually | no, a refresh keeps the same rights | | Pipeline reaction | refresh once, replay once | surface the reason, do not refresh | The HTTP semantics say a server generating a `401` **must** send a `WWW-Authenticate` header with at least one challenge, and that the client may repeat the request with new credentials. The bearer-token rules add the `invalid_token` error for a token that is "expired, revoked, malformed, or invalid for other reasons" and say the client may request a new access token and retry. For `403` the same semantics say the client should **not** automatically repeat the request with the same credentials, and add that a request may be forbidden for reasons unrelated to credentials at all. ## What the pipeline does with a 401 The auth step of the pipeline, the one that attaches the token to each call, also owns the reaction to `401`: 1. Note which access token the failed call carried. 2. If a newer token already exists (another call refreshed it meanwhile), replay with that token. 3. Otherwise obtain a fresh token through the refresh grant, sharing one refresh among every call that is waiting. 4. Replay the original call **once**, marked as already replayed. 5. If the replay also returns `401`, stop: the session is no longer valid, so clear the tokens and move the app to a signed-out state. The cap in step 4 matters. A server that has revoked the session will keep answering `401`, and an unbounded loop of refresh-and-replay drains the battery, floods the token endpoint and never reaches the user. The HTTP rules point the same way: if the same challenge comes back after the client has already tried to authenticate, the response should be shown to the user rather than retried. ## What the pipeline does with a 403 A `403` reaches the feature that made the call, as a typed "not permitted" error, and the feature decides what the user sees: "Your account cannot send money until you verify your identity", "This card is frozen". Refreshing would be wasted work for two reasons: - **Scope does not grow on refresh.** The refresh grant may not request any scope the user did not originally grant, so a refreshed token holds the same permissions as the old one. - **The reason may not be the token at all.** A daily limit, a regulatory hold or a blocked recipient are business rules; no credential change fixes them. Some products do have a **step-up** path, where a sensitive action needs the user to sign in again or confirm with a second factor. That is a deliberate, user-driven flow with its own signal from the server, not something the pipeline triggers silently on every `403`. ## Edge cases an interviewer probes - **A 401 on a call that carried no token.** The app sent an authenticated request before sign-in finished; the fix is ordering, not refresh. - **A 401 that arrives after the token was already refreshed.** The call left with the old token; it only needs a replay with the new one, not a second refresh. - **A 403 used to hide a resource.** A server may answer `404` instead of `403` to conceal that something exists, so the client should not build logic on the difference between them. - **A 403 treated as sign-out.** Throwing the user out on a permission error loses their place for nothing; their session is fine. ## Why the distinction matters in a wallet On a payments app a misread code is expensive in both directions. Refreshing on `403` hides a real business message ("limit reached") behind a spinner and a silent retry. Signing out on `403` makes a user re-enter credentials to see the same refusal. Treating a `401` as permanent forces a sign-in every time an access token expires, which on short-lived tokens can be several times a day. Reading the code, and the challenge that comes with it, is the first decision the pipeline makes on every refused call.
- In a mobile wallet app, the replay after a successful refresh also gets 401. What should the pipeline do?Stop there. One refresh and one replay per call is the budget; a second `401` with a token the server issued moments ago means the session itself is no longer valid, for example revoked from another device. Clear the stored tokens, fail the waiting calls with a session-ended error and route the user to sign in, keeping enough context to return them to where they were.
- Should a 403 ever lead a mobile app to ask the user to sign in again?Only through an explicit step-up flow the server signals, such as re-confirming identity before a large transfer. A plain refresh cannot add permissions, because the refresh grant may not request scope beyond the original grant. Without such a flow, a `403` is shown as a permission or business message and the session stays as it is.
- Any auth error means the token expired, so refresh on both 401 and 403.
- A refreshed access token can unlock an endpoint the user lacked scope for.
- Keep refreshing and replaying on 401 until the call finally succeeds.
- A 403 means the session is broken, so sign the user out.
- A 401 is permanent, so send the user to the sign-in screen at once.