Sub topic 2
What belongs in secure storage (and what does not)
This chapter assumes you know basic Dart, async/await, Flutter packages, and the difference between access tokens and refresh tokens.
Secure storage is for small values that would create a security problem if another app or a person extracted them from the device. In Flutter, flutter_secure_storage stores values through platform security features such as the iOS Keychain and Android Keystore-backed storage.
Secure storage reduces the risk of secrets being exposed at rest. It does not make a compromised device safe, and it does not replace server-side authentication.
Start with a threat model
A threat model is a description of what you are protecting and who might attack it. For a mobile app, consider these situations:
- Someone reads the app's files from a device backup.
- A user loses an unlocked phone.
- Another app tries to read your app's private files.
- A debugging build prints sensitive data to logs.
- An attacker controls a rooted or jailbroken device.
- Malicious code runs inside your app process.
Secure storage helps most with the first three cases. It provides less protection when the device or your running process is already compromised.
Values that belong there
Store a value in secure storage when it acts like a credential or unlocks another secret.
Common examples include:
- Refresh tokens used to obtain new access tokens.
- Long-lived access tokens, when your authentication design requires them.
- OAuth client secrets that are safe to distribute only when the provider explicitly allows it. Many client secrets should not be shipped in a mobile app at all.
- Private keys used by the app.
- Small encryption keys that protect data stored elsewhere.
- Device registration secrets or server-issued device keys.
- Recovery codes, if the app must keep them locally.
A refresh token is usually the clearest example. An access token may be kept in memory because it expires quickly. A refresh token lasts longer, so storing it in ordinary preferences would expose a more valuable credential.
The value should be small. Secure storage is not a general-purpose database. Store an identifier or token there, then keep larger records in a database or cache protected by a suitable design.
Values that do not belong there
Do not put every setting in secure storage. Ordinary, non-sensitive preferences are simpler and usually faster to access.
Examples include:
- Theme selection.
- Language choice.
- Whether an onboarding screen was shown.
- Page filters and sorting choices.
- A cached API response.
- Product data that is already public.
- A user ID that has no authentication value.
- Temporary state that can be recreated after app startup.
A user ID may identify an account, but identification is different from authorization. If knowing the ID does not let someone access the account, secure storage is usually unnecessary.
Do not store a user's password so the app can sign in automatically. Passwords should be entered into the authentication flow and handled by the server. If an app stores a password, a device compromise can expose a credential that the user may have reused elsewhere.
| Value | Recommended location | Reason |
|---|---|---|
| Refresh token | Secure storage | Grants continued authentication |
| Theme setting | Ordinary preferences | Reveals no useful authority |
| Large cached response | Database or cache | Secure storage is intended for small values |
| User password | Do not store locally | It is highly reusable and difficult to protect safely |
| Private signing key | Secure storage or platform key API | Key exposure can allow impersonation |
Apply the decision to each value
For every candidate value, ask:
- What could an attacker do with this value?
- How long does the value remain useful?
- Can the app recreate it from the server?
- Is it a credential, a key, or merely a preference?
- Should it be removed when the user signs out?
- Does the platform support the required protection?
A useful design stores the smallest possible secret for the shortest useful time. It also avoids logging the value, placing it in crash reports, or sending it in analytics events.
Make the boundary explicit
Keep secure-storage access behind a small repository rather than scattering key names across widgets and services. The repository can define which values are secrets, control their lifetime, and clear them during sign-out. This boundary also makes later changes easier, such as rotating a token or changing platform options.
The next design question is how the package maps this boundary to the Keychain and Android Keystore.
Exercise
Your app stores a refresh token, a theme name, a cached list of products, and a private encryption key. Which values should use secure storage?
Show answer
Store the refresh token and private encryption key in secure storage. Keep the theme name in ordinary preferences. Keep the product list in a database or cache, using additional protection if its contents are sensitive.