Persist Refresh Token in Secure Storage vs. In‑Memory Only: Trade‑off for Least‑Privilege Auth in Ionic Capacitor Apps
0 reputation · 01 Jun 2020, 21:37 UTC
0 reputation · 01 Jun 2020, 21:37 UTC
Determine the optimal refresh‑token handling strategy for an Ionic Capacitor hybrid app that enforces least‑privilege authentication while gracefully recovering from expired access tokens.
The decision involves two concrete constraints: where to store the refresh token (Secure Storage for cross‑restart persistence vs. in‑memory only) and whether to trigger a silent refresh when the app returns to the foreground or to rely solely on reactive 401 interception.
Each option trades off security surface, user experience, and complexity, especially for offline‑first or low‑memory scenarios.
Store the refresh token in Secure Storage (hardware-backed keystore), not in memory only. Persist it with biometric/device-credential gating so the OS requires user presence to unlock. Trigger a silent refresh on foreground only when the access token is near expiry (e.g., <5 min); otherwise rely on 401 interception for the actual renewal. This hybrid gives you seamless session continuity after restarts/OS updates while keeping the proactive attack surface minimal.
App.addListener('resume', …)) renews before the user acts, eliminating that latency spike. Risk: a compromised foreground app can silently mint new access tokens. Mitigate by only refreshing when JWT exp claim shows expiry within a short threshold (5 min) and by adding jitter/backoff to avoid IDP rate limits on mass resume.@capacitor-community/secure-storage (or native Keychain/Keystore wrappers), NOT @capacitor/preferences (plaintext).kSecAccessControlBiometryCurrentSet) and Android BiometricPrompt so token read requires fresh biometric/device credential.exp - now < 5 min, call refresh endpoint with jitter (0–2 s).token_expired → single refresh promise → retry original request. refresh_revoked / refresh_expired → clear storage → force logout.What is your minimum supported Android API level? Hardware-backed Keystore (required for non-extractable keys) needs API 23+. If you must support API 21–22, Secure Storage falls back to software-only encryption — adjust threat model accordingly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.