Join our Newsletter — 33% off our NHI Course
Home› FAQ› Authentication, Authorisation & Trust› How should security teams store and refresh session…
Authentication, Authorisation & Trust

How should security teams store and refresh session tokens in iOS apps without breaking user sessions?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated October 30, 2026 Domain: Authentication, Authorisation & Trust

Store both access and refresh tokens in the Keychain, not UserDefaults, and always replace the stored refresh token after a successful exchange. Keep the access token short-lived so role changes and revocations take effect quickly. On refresh, preserve the session through transient errors, and clear tokens only when the server returns a terminal invalid_grant response.

How to store iOS session tokens so refresh does not break the user experience

iOS apps should treat access and refresh tokens as protected credentials, not app data. Store them in the Keychain, use the refresh token only to obtain a new access token, and overwrite the stored refresh token immediately after a successful exchange so the app does not keep using an older token family member.

That pattern matters because the app is trying to balance two goals at once: strong security and uninterrupted sessions. A short-lived access token limits how long a stolen token remains useful, while refresh token rotation keeps the persistent credential current without forcing the user to sign in again after every API call.

For the storage layer, the Keychain is the right default because it is designed for secret-bearing material and survives app restarts without moving tokens into less suitable storage such as UserDefaults. The practical test is simple: if the value can re-establish API access or user authority, it belongs in protected secret storage, not in convenience storage or logs. The iOS App Security Guide covers Keychain classes and secure storage decisions for mobile apps.

Refresh-token handling should be atomic from the app’s point of view. Once the authorization server returns a new access token and refresh token pair, persist the new refresh token immediately and discard the old one. That avoids replaying a stale refresh token after rotation, which is a common cause of session loss in mobile clients that cache token state too loosely. The Token and Session Security Guide explains refresh rotation, replay resistance and token lifetime trade-offs.

Short-lived access tokens reduce the blast radius of revocation and role change, but they also increase the frequency of refresh traffic. That makes the refresh path part of the app’s reliability design, not just its authentication design. The Token Lifetime Policy Design Guide is useful when you need to decide how short is short enough without creating unnecessary churn for mobile users.

Token refresh should be tolerant of transient failures such as brief connectivity loss, DNS issues, or temporary 5xx responses from the identity provider. In those cases, keep the existing session state and retry with backoff rather than clearing tokens preemptively. Only a terminal invalid_grant response should trigger full session teardown, because that is the signal that the refresh credential is no longer valid.

One operational detail is easy to miss: the access token may still be valid while the refresh token has already rotated. If the app keeps the old refresh token after a successful exchange, the next refresh attempt can fail even though the user never signed out. That is why replacement must happen after every successful refresh exchange, not only during login or logout. The OAuth Refresh Token Rotation and Expiry Guide covers refresh token reuse and expiry behaviour.

From an abuse perspective, refresh tokens are the higher-value target because they can extend access beyond a single access-token lifetime. If the token is extracted from device storage, logs, backups, or a compromised process, the attacker may be able to mint new access tokens until rotation or revocation breaks the chain. That is why session continuity and compromise resilience have to be designed together, not treated as separate problems.

Risk and Threat Considerations

The main risk is over-retention of a reusable credential. If refresh tokens are stored insecurely, copied into backups, or left unchanged after rotation, a stolen token can outlive the access token and keep the session usable for an attacker.

Failure mechanism: The app either persists tokens in the wrong store, fails to overwrite the rotated refresh token, or clears state on temporary refresh failures. In the first two cases, compromise becomes easier or longer lived; in the third, users get logged out unnecessarily and the app creates avoidable session churn.

Impact: Attackers may preserve access after password changes or role updates until the refresh token is revoked, while legitimate users may see broken sessions, repeated sign-in prompts, or inconsistent state between client and server.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while NIST SP 800-53 Rev 5, OWASP ASVS and NIST SP 800-63 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST SP 800-53 Rev 5IA-5 — Authenticator ManagementCovers lifecycle handling of refresh and access tokens as authenticators.
IA-2 — Identification and Authentication (Organizational Users)Session token handling depends on reliable user authentication state in the client.
SC-28 — Protection of Information at RestKeychain storage is about protecting secrets stored on device.
Recommendation — Rotate and replace refresh tokens immediately after successful exchange. Tie token acceptance to authenticated user state and reauthentication triggers. Store tokens only in protected at-rest storage, not general app storage.
OWASP ASVSV7 — Session ManagementSession token rotation, expiry and invalidation are core session management concerns.
V9 — Self-contained TokensAccess tokens and refresh tokens are token artefacts requiring secure handling and validation.
Recommendation — Enforce secure session token lifetime, renewal and invalidation handling. Validate token lifecycle and reject stale or replayed token state.
NIST SP 800-63Session Management and ReauthenticationProvides identity-session guidance for keeping authenticated sessions usable and bounded.
Recommendation — Use reauthentication and session timeout rules that preserve usability without extending risk.
OWASP Non-Human Identity Top 10NHI-02 — Secret LeakageRefresh and access tokens are secrets that must not leak from mobile storage or logs.
NHI-07 — Long-Lived SecretsLong-lived refresh tokens increase replay and theft impact on mobile clients.
NHI-04 — Insecure AuthenticationToken refresh is an authentication flow that must fail safely and avoid stale credential use.
Recommendation — Keep tokens out of insecure storage and rotate them after each exchange. Shorten token lifetimes and rotate refresh tokens to reduce replay exposure. Handle refresh failures and invalidation without accepting stale credentials.

Practitioner Guidance

What to verify: Confirm that access and refresh tokens are written only to Keychain-backed storage and that refresh writes are replace-in-place, not append-only or deferred until app shutdown. Also verify that your refresh handler distinguishes transient transport or server failures from terminal token rejection.

Decision rule: If refresh succeeds, persist the new refresh token immediately and continue the session. If the server returns invalid_grant, clear local token state and force reauthentication; if the error is temporary, retain state and retry with bounded backoff.

Common mistake: Treating session persistence as a UI problem. In practice, the session state machine needs to be coordinated with token rotation, revocation handling, and failure recovery, or the app will either leak access or frustrate users with false sign-outs.

Practitioner takeaway: The safest pattern is to make the refresh token a rotating secret with strict replacement semantics, while making access-token expiry short enough that compromise or revocation takes effect quickly without turning normal network noise into a logout event.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 30, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org