In a law firm's client-portal phone app, why can't the refresh token live in a plain preferences file, and what should the app never store?
answer
- the sandbox is only one wall
- backups copy ordinary app files
- a bearer token works anywhere
- the OS-protected credential store
- the password is exchanged, never kept
basics
~20 sA plain preferences file is cleartext that device backups, file copies and rooted or jailbroken phones expose. Keep the refresh token in the OS-protected credential store; never persist the password, and keep only what offline use needs.
solid answer
~50 sThe app sandbox stops other apps opening the preferences file, but the file is still ordinary cleartext app data: both platforms put ordinary app files into the device backup by default unless the app excludes them, and a rooted or jailbroken phone reads it directly. A refresh token is a bearer credential, so a copy usually works anywhere until it expires or is revoked. The OS-protected credential store (a system store on one platform, a secret encrypted under a key-store key on the other) keeps small secrets encrypted under keys the app cannot read, releases them only to that app, and lets it choose when each is readable. It protects the token at rest, not from code running inside the app. Never persist the password (it is exchanged once for tokens), no raw token sits outside the store, and the phone keeps only what offline use needs.
go deeper
Recall the three ways a preferences file leaks: the device backup, a rooted or jailbroken phone, a copy of the app's folder. Then name the OS-protected credential store and say the password is never kept.
Explain what the credential store adds, encryption under keys the app cannot read and read conditions chosen when the secret is stored, and what it does not: it still hands the token to code running as the app.
Tie storage to the server: a bearer token copied off the phone works anywhere, so lifetime, revocation and how little the phone keeps for offline use matter as much as where it rests.
Frame a client-wide rule: which secrets the phone may hold at all, in which store, for how long, and which offline promises the firm declines because of what a lost phone would carry.
## The question behind the question Every phone app that signs a user in ends up holding a **refresh token**: a long-lived credential the app presents to obtain fresh, short-lived access tokens without asking for the password again. In a law firm's client-portal app it opens a client's confidential case documents and their messages with their lawyer. Where the app writes that token down decides who else can pick it up, and what the app writes down besides it decides how much a lost phone gives away. ## Why a plain preferences file fails A **plain preferences file** is the app's ordinary key-value settings file: convenient, fast, and stored as cleartext inside the app's private folder. The app sandbox stops *other apps* from opening it, and many teams stop reasoning there. The sandbox is one wall among several: - **Device backups.** Both platforms back up an app's ordinary files by default, leaving out cache folders and whatever the app explicitly excludes; on one platform the preferences files are named in that default set. A token in preferences travels into every backup. - **A compromised device.** On a rooted or jailbroken phone the sandbox no longer holds, and a process with elevated rights reads the file like any other. - **Copies of the app's folder.** Debugging, backup and forensic tools that copy an app's files copy the token with them. - **It is a bearer credential.** Whoever holds a copy can usually use it from any machine until it expires or the server revokes it; nothing in the token ties it to this phone. Encoding the value in `Base64`, scrambling it reversibly or giving the preference a meaningless name changes how it looks, not who can read it. ## What the OS-protected credential store adds Each platform offers an OS-protected way to keep small secrets, called here the **OS-protected credential store**. On one platform it is a system store: the system encrypts each item under keys it manages, releases it only to the app that stored it, and lets the app choose, when the item is created, the conditions under which it can be read: only while the phone is unlocked, from the first unlock after a restart, only with the user present, only on this device. On the other, the same protection is built by encrypting the token under a key held in the hardware-backed key store and keeping only the ciphertext, with the conditions attached to that key; cross-platform secure-storage wrappers do this on the app's behalf. | Where the token rests | Copied files or a backup | Other apps | Code running as the app | Read conditions | |---|---|---|---|---| | Plain preferences file | Readable as cleartext | Blocked by the sandbox | Readable | None | | OS-protected credential store | Not readable as cleartext | Blocked | Readable: the store answers the app | Chosen when stored | The last two columns matter in an interview. The store protects the token **at rest**. It does not protect it from code running inside the app, such as a compromised third-party library, because to the store that code *is* the app. That is one reason tokens stay short-lived and revocable by the server whatever the phone does. ## What never to persist The second half of the question is the one candidates skip. Keep on the phone only what offline use needs: 1. **Never the password.** Exchange it once for tokens and drop it. A stored password, however it is encrypted, is often reused on other services, cannot be revoked the way a token can, and lets whoever extracts it sign in afresh from anywhere. 2. **No raw token outside the credential store**: not in preferences, not in a column of the local database, not in a file written "for the background job". 3. **No more content than the offline promise.** If the firm lets clients read documents they marked for offline use, keep those, encrypted on disk; not every document they ever previewed. ## A short design for the portal - At sign-in the app receives its tokens, keeps the access token in memory and writes the refresh token to the credential store. - The password field is cleared and never written anywhere. - Offline documents are encrypted on disk under a key that is never stored beside them; how that key is protected is a separate decision. - The preferences file holds only non-secret settings: theme, which matter was open last, and the client's email address if they asked to have it remembered. - At launch the app reads the refresh token from the store; if it is not there, the app shows sign-in rather than guessing. ## How interviewers mark it A passing answer names the credential store. A good one also says what the store does **not** do, which is protect the token from code running as the app, and offers the never-persist list unprompted, starting with the password. A weak one defends preferences because "the sandbox protects them", or reaches for an encoding as if it were encryption.
- Does the credential store protect the token from malicious code running inside the app, such as a compromised third-party library?No. The store releases the secret to the app's identity, and anything executing in the app's process is the app as far as the store can tell. It defends the token at rest: against copied files, backups read elsewhere and other apps. Against code inside the app the defences are short token lifetimes, server-side revocation and keeping untrusted code out of the app.
- The firm wants clients to read documents offline for a week; what should the phone store to make that work?The minimum: the refresh token in the credential store, only the documents the client marked for offline use, encrypted under a key that is not stored beside them, and the time the offline grant expires. Not the password, not every matter's file list, not documents merely previewed once. Each extra item enlarges what a lost phone gives away.
Leaving the refresh token in preferences is like leaving the office key under the doormat of a building with a locked lobby: other tenants cannot reach it, but the cleaner, the locksmith and anyone who copies the doormat can. The credential store is the building's key safe, opened only for the named tenant; it still opens for anyone who walks in as that tenant.
- The app sandbox already protects preferences, so a token there is safe.
- Base64-encoding the token makes it unreadable to an attacker.
- Store the password encrypted so the app can sign the user back in silently.
- A token in the credential store is safe from anything running inside the app.
- Cache every document the client opened, in case they go offline later.