Sub topic 1
What You Are Protecting Against
Secure storage is useful only when you can name the threat it addresses. flutter_secure_storage protects small secrets stored on a device, such as a refresh token. It does not make every piece of app data private or make an already-compromised device safe.
Prerequisites
This chapter assumes you know basic Dart, async and await, Flutter app structure, and the purpose of authentication tokens. You should also know that an app runs on an operating system that controls files, processes, and access to hardware.
Define the threat model
A threat model is a short description of what you are protecting, who might attack it, and what capabilities the attacker has.
For a mobile app, a reasonable starting model includes these assumptions:
- An attacker may copy an unprotected file from a backup or a debug build.
- Another app should not be able to read your app's stored tokens through ordinary operating-system APIs.
- A user may lose a device or give it to someone else while it is locked.
- A token may remain on the device after the user signs out unless your app removes it.
- The device may be rooted, jailbroken, infected, or running under a debugger.
The operating system can enforce stronger boundaries than a plain file. On iOS, the Keychain provides protected storage. On Android, secure-storage packages use Android security services, commonly involving the Keystore and encrypted preferences. The package gives Flutter code one API while platform code handles the backend.
What secure storage protects
Secure storage is mainly a confidentiality control. Confidentiality means preventing unauthorized parties from reading a value. Platform-backed storage also makes casual extraction harder than reading a JSON file or shared preferences entry.
It is a good location for values such as:
- Refresh tokens.
- Short-lived access tokens, when your design requires local persistence.
- Device-bound credentials or installation identifiers.
- Small encryption keys used by another part of the app.
A token stored there still grants whatever access the server gives to that token. Encryption at rest does not reduce the token's authority. If malicious code runs inside your app process, it may be able to ask your app to read the token.
Secure storage is usually the wrong place for:
- User profile data.
- Cached API responses.
- Images, documents, or large databases.
- Data that must be searched or displayed frequently.
- Secrets that should exist only on a server.
Store ordinary application data in an appropriate database or cache. Keep server-controlled secrets on the server instead of shipping them in the app.
| Value | Typical location | Reason |
|---|---|---|
| Refresh token | Secure storage | Small and grants authenticated access |
| User display name | Database or cache | Not normally a credential |
| Large API response | Database or file cache | Secure storage is not a general data store |
| Server private key | Server-side secret manager | Never embed it in a mobile app |
What it does not protect
Secure storage cannot protect a secret from every threat:
- A rooted or jailbroken device may expose platform data.
- Malware with powerful device privileges may observe the app or intercept its requests.
- A debugger or instrumentation tool may inspect values while the app is running.
- A stolen token may be replayed until it expires or is revoked.
- Backups and device migration can have platform-specific behavior.
- A poorly handled sign-out flow can leave the token behind.
- Logs, crash reports, screenshots, and analytics can accidentally reveal secrets.
Treat the storage API as one layer in a larger design. Use short token lifetimes, server-side revocation, TLS, careful logging, and least-privilege token scopes. Never print tokens while debugging.
Decide before you store
For each value, ask:
- Is it a secret or merely private user data?
- Must it survive app restarts?
- What access does possession of it grant?
- How will the app revoke or delete it?
- What should happen if the platform refuses to read it?
These answers determine whether secure storage is appropriate and what failure behavior your app needs. The next step is to use the package API without treating it as a general-purpose database.
Exercise
Your app needs to remember a user's theme choice and keep a refresh token after restart. Which value belongs in secure storage, and why?
Show answer
The refresh token belongs in secure storage because it grants authenticated access. The theme choice is ordinary application data and can use preferences or another local settings store.