Login and Session Architectures for Native Mobile Apps with IdP
- Objective and contextThis article describes clean architecture variants for authentication and session handling in native mobile apps that use an external identity provider (IdP) via OAuth 2.0 / OpenID Connect (OIDC).
The objective is:- clear separation of mechanisms
- comparable variants
- a robust basis for decision-making
- clear separation of mechanisms
- Architecture foundations (precise terminology)2.1 Components involved
- Mobile app (public client)
- Authorization server / IdP
- Resource server (API)
2.2 Token artefacts- Access token
- short-lived (typically: minutes)
- access to APIs
- short-lived (typically: minutes)
- Refresh token
- longer-lived
- used to renew access tokens
- highly sensitive
- longer-lived
2.3 Two strictly separated session layers
(A) IdP session- exists in the system browser / ASWebAuthenticationSession / Custom Tabs
- enables SSO
- not directly controlled by the app
- based on locally stored tokens
- fully under the control of the app
- Mobile app (public client)
- Design dimensions (orthogonal!)All meaningful architectures result from combining these dimensions:
3.1 Dimension 1: Session persistence (app side)
Variant 1: No persistence- tokens only in RAM
- lost after app restart
- refresh token in secure storage:
- iOS: Keychain
- Android: Keystore
- iOS: Keychain
- automatic token renewal
3.2 Dimension 2: Session lifetime model
Applies to refresh token / session:
Absolute lifetime- hard upper limit (e.g. 30 days)
- expiry after inactivity (e.g. 15 min)
- extended through usage
- typically combined with:
- rotation
- max lifetime
- rotation
3.3 Dimension 3: Token renewal
Without refresh token- access token expires → new login required
- silent refresh possible
- each refresh → new refresh token
- old token becomes invalid
3.4 Dimension 4: Access protection at app level
Independent of the IdP:No additional lock
Local re-authentication
- biometrics (Face ID / fingerprint)
- device PIN
- on app start
- on resume
- for sensitive actions (step-up)
3.5 Dimension 5: Token security level
Bearer tokens- possession is sufficient
(e.g. DPoP)- token bound to a cryptographic key
- protection against token theft
- key stored in Secure Enclave / TEE
- strongest device binding
3.6 Dimension 6: IdP session usage (SSO behaviour)
SSO active- renewed login possible without user interaction
- e.g. prompt=login
- tokens only in RAM
- Reference architecture variantsNow meaningfully cut, realistic combinations:Variant A: Ephemeral session (high security) ×
Configuration- no persistence
- no refresh token
- short access token
- app session ends when the app is closed
- IdP session can speed up login
- minimal risk in case of device loss
- no token storage
- very poor UX
- frequent redirects
- highly sensitive specialist applications
Variant B: Persistent session (standard)×
Configuration- refresh token in secure storage
- bearer tokens
- absolute lifetime
- «Stay logged in»
- silent refresh
- very good UX
- standard implementation
- risk in case of token exfiltration
- revocation is critical
Variant C: Persistent session with sliding + rotation (best practice)×
Configuration- refresh token + rotation
- sliding session + absolute cap
- secure storage
- active usage extends the session
- compromised tokens are invalidated quickly
- very good balance of UX / security
- widely used today
- higher complexity
- server must manage state
Variant D: Persistence + local re-authentication×
Configuration- like variant B or C
- in addition
- biometrics / PIN before access
- two protection layers:
- possession (token)
- user presence
- significantly increased security with good UX
- protects against «shoulder surfing» / unlocked device
- UX friction
- fallback logic required
- banking
- eGovernment
Variant E: Sender-constrained session (modern high security)×
Configuration- DPoP or similar mechanisms
- key in Secure Enclave / Keystore
- usually combined with C or D
- token alone is not sufficient
- device binding
- very high protection against token theft
- state of the art
- complex
- interoperability / debugging demanding
- no persistence
- Variant comparison
Variant UX Security Complexity Comment A Ephemeral ❌ ✅✅ ✅ rarely appropriate B Persistence ✅ ⚠️ ⚠️ baseline C Sliding + Rotation ✅ ✅ ⚠️⚠️ best practice D + Re-Auth ⚠️/✅ ✅✅ ⚠️⚠️ for sensitive data E Sender-Constrained ✅ 🚀 ❌ high-end - Critical detail topics (often underestimated) 6.1 Logout semantics
- local logout → delete tokens
- global logout → end IdP session (browser)
- revocation → invalidate refresh tokens server-side
6.2 Device loss
Questions:- are tokens protected?
- is biometrics enabled?
- is there remote revocation?
6.3 Offline behaviour- access token valid → offline possible?
- refresh → requires network
6.4 Multi-device- separate refresh tokens per device?
- central invalidation?
6.5 Step-up authentication- renewed authentication for critical actions
- independent of session
- local logout → delete tokens
- -Recommendations
- standard applications → variant C
- sensitive applications (e.g. eIAM / public authorities) → variant D + optional E
- maximum security → combine C + D + E
- standard applications → variant C
- ConclusionThe key insight is:There is not «one single login pattern», but rather a modular system of orthogonal decisions.
A clean architecture means:- do not mix dimensions
- choose the combination deliberately
- address risks explicitly
- do not mix dimensions
Recommendation: Authentication and session architecture for a highly sensitive mobile app
- Target stateThe application should:
- meet high security requirements (prevent misuse in case of device loss)
- remain efficient in day-to-day use
- function under different operating conditions (varying usage, possibly limited connectivity)
- meet high security requirements (prevent misuse in case of device loss)
- Recommended architecture (combination)2.1 Session strategy
→ Persistent session with sliding lifetime and rotation
Implementation- use of refresh tokens
- storage in platform-protected secure storage
- refresh token rotation on every renewal
- combination of:
- idle timeout (e.g. hours range)
- absolute lifetime (e.g. days range)
- idle timeout (e.g. hours range)
- avoids frequent logins during operation
- limits the impact of token compromise
- standard for modern, secure systems
2.2 Access protection at app level
→ Mandatory local re-authentication
Implementation- access to the session only after:
- biometrics (primary)
- device PIN (fallback)
- biometrics (primary)
- enforced:
- on app start
- after defined inactivity
- for sensitive actions
- on app start
- protection for:
- unlocked, unattended devices
- shared devices
- unlocked, unattended devices
- significant security increase without a full re-login
2.3 Token security level
→ Sender-constrained tokens (recommended)
Implementation- binding tokens to a device-specific key
- key storage in:
- Secure Enclave (iOS)
- Trusted Execution Environment (Android)
- Secure Enclave (iOS)
- prevents use of intercepted tokens on other devices
- reduces risk in case of malware or memory access
2.4 IdP integration
→ System-browser-based login (OIDC best practice)
Implementation- use of:
- ASWebAuthenticationSession (iOS)
- Custom Tabs (Android)
- ASWebAuthenticationSession (iOS)
- use of existing IdP session (SSO)
- avoids credential handling in the app
- leverages existing authentication strength (e.g. MFA)
2.5 Logout and revocation strategy
Implementation- local logout
- complete deletion of all tokens
- complete deletion of all tokens
- server-side revocation
- invalidation of refresh tokens
- invalidation of refresh tokens
- optional:
- global logout via IdP
- global logout via IdP
- controlled session termination in case of:
- device loss
- role change
- security incidents
- device loss
2.6 Offline behaviour
Recommendation- allow offline access to non-sensitive, already loaded data
- no token renewal without network
- allow sensitive actions only online
- use of refresh tokens
- Security level (classification)The recommended combination corresponds to:
- high protection against device loss
- high protection against token theft
- controlled session lifetime
- good operational usability
- high protection against device loss
- Deliberately rejected alternativesNo full re-login on every use
- too much friction
- not operationally practical
- insufficient protection in case of physical access
- increased risk in case of token exfiltration
- too much friction
- Additional measures (optional, depending on protection needs)
- step-up authentication for critical actions
- device binding with attestation
- remote session kill
- root/jailbreak detection (use with caution)
- step-up authentication for critical actions
- ConclusionA multi-layered security architecture is recommended:Persistent, rotating session
- local re-authentication
- device-bound tokens
This combination achieves a robust balance between security and usability and is suitable for applications with elevated protection requirements. - local re-authentication