In a law firm's client-portal app, why does a background token refresh fail on a locked phone, and which availability class suits each secret?
answer
- chosen when the secret is stored
- locked is not the same as missing
- after first unlock, for background work
- while unlocked, for foreground keys
- device-only stays off new phones
basics
~20 sThe token was stored readable only while the phone is unlocked, so a job on a locked phone cannot read it. Give the refresh token the after-first-unlock, device-only class; keep the document key unlocked-only, since documents open in the foreground.
solid answer
~50 sAn **availability class**, chosen when a secret is stored, says when the system will release it: from the first unlock after a restart, only while the phone is unlocked, only with the user present, and whether it may move to another phone. A refresh token saved as readable only while unlocked, or behind a biometric prompt, cannot be read by a job the OS runs at night on a locked phone, so the refresh fails, and a careless app reads that as signed out. The refresh token needs the after-first-unlock class, in its this-device-only form so it never migrates. The key that protects the documents belongs in the strict while-unlocked class, because documents are opened in the foreground. On one platform these are named classes on each item; on the other, conditions on the key, such as recent user authentication, which a locked-phone job cannot meet either.
go deeper
Recall that a stored secret can be readable only while the phone is unlocked, and that background work runs while the phone is locked.
Explain the three dimensions of an availability class, lock state, user presence and device binding, and assign one to each secret the portal holds.
Show the production failure: clients signed out every morning because a lock error was read as a missing token. Separate locked from missing in code.
Set the rule for every team: name each secret's reader, pick the strictest class that reader can open, and make device-only the default for credentials.
## What an availability class decides When a phone app stores a secret in the OS-protected credential store, it also chooses **when** the system will release it. That choice, the **availability class**, is fixed when the item is created and covers three separate things: - **Lock state.** Readable only while the phone is unlocked, or from the first unlock after a restart until the next restart. - **User presence.** Readable only after the user passes a biometric or device-passcode prompt. - **Device binding.** Whether the item may move to another phone through an encrypted backup, or stays with this device. On one platform these are named classes set on each stored item, and files carry protection levels of the same kind. On the other, the same intent is expressed as conditions attached to a key when it is generated, for example "usable only within a set time after the user authenticated", and those conditions cannot be changed afterwards. Cross-platform frameworks surface the same choices as options on their secure-storage wrappers, so the design decision is identical whichever stack builds the app. ## The classes side by side The table uses the named classes of the platform that has them; on the other platform, read "device-only" as the natural state of a key that cannot be exported. | Class | Readable when | Moves to a new phone through an encrypted backup | Fits | |---|---|---|---| | After first unlock | From the first unlock after a restart until the next restart | Yes | Rarely right for a credential | | After first unlock, this device only | Same | No | A refresh token a background job reads | | While unlocked (the default on one platform) | Only while the phone is unlocked | Yes | Secrets used only in the foreground | | While unlocked, this device only | Only while unlocked | No | The key protecting the document cache | | Combined with user presence | Only after a biometric or passcode prompt | As the class it is combined with | Opening the app, sensitive actions | A this-device-only item is left out when a backup is restored onto a **different** phone, but it can come back when a backup is restored onto the **same** phone. ## Why the overnight refresh fails 1. The client signs in at lunchtime. The app stores the refresh token in the strict while-unlocked class, reasoning that it is the most sensitive thing it holds. 2. At night the OS runs the app's deferred refresh work while the phone lies locked on the bedside table. 3. The job asks the store for the token, and the store refuses because the phone is locked. 4. A careless app treats "cannot read" as "no token", clears its session and greets the client with a sign-in screen in the morning. Two variants fail the same way. After a restart that nobody has unlocked yet, even after-first-unlock items stay closed until that first unlock. And a token behind a user-presence prompt can never be read by background work, because nobody is there to answer the prompt. On the platform that expresses this as key conditions, a key requiring recent user authentication fails for the same reason once its window has passed. When the OS chooses to run the job is a separate subject; this one is whether the job can read what it needs once it runs. ## Assigning classes in the law-firm app - **Refresh token:** after first unlock, this device only. Background refresh works once the phone has been unlocked since it started, and the token never follows a backup to another phone. - **Access token:** in memory only. It is short-lived and obtained again from the refresh token. - **Wrapping key for the document cache:** while unlocked, this device only. Documents are read in the foreground, so a locked phone keeps them shut. - **Biometric unlock key:** user presence, this device only. It gates opening the app and is never needed by background work. - **Non-secret settings:** ordinary storage, with no class at all. The rule of thumb is to pick the strictest class that still lets every legitimate reader in, and to name the reader: the foreground screen, a background job, or a person at a prompt. ## Handling "not now" versus "not there" The store reports a locked item differently from a missing one, and the app must keep the two apart: - **Locked:** leave everything in place, skip this run, and retry after the next unlock. - **Missing or invalidated:** take the recovery path: sign in again, then create keys afresh. Confusing the two is how a privacy setting turns into clients signed out every morning, or, worse, a refresh job that deletes a perfectly good token at three in the morning.
- On the platform that offers a class requiring a device passcode, what happens to such an item if the user removes the passcode?The item becomes unavailable, and it cannot be created at all on a phone without a passcode. The app must treat that like any missing secret: send the client through sign-in, and say that offline documents need a device passcode, rather than quietly falling back to weaker storage.
- Why not keep the refresh token in the plain after-first-unlock class and skip device-only?On one platform, items in that class migrate to a new phone through an encrypted backup. A refresh token that arrives on a new phone is a credential that left the device it was issued to, and the client never signs in there. Device-only makes every new phone sign in and get its own session.
- The OS lets scheduled background work read every secret the app owns.
- The strictest availability class is always the right choice for a token.
- If the token cannot be read right now, the user is signed out.
- Keep a spare copy of the token in preferences for background jobs.
- A device-only item never comes back, even when restoring the same phone.