Skip to main content

ADR-002: Token Cache Secure Storage Implementation

Status​

Partly implemented - Phase 1 (the localStorage-compatible token cache bridge) shipped in v2.5.9 and is in use. Phase 2 (encryption with Electron safeStorage) never took effect and its code was removed in November 2026.

safeStorage is a main-process module, and the token cache runs in the renderer's preload script, where require('electron').safeStorage is undefined. The availability check therefore always came back false and every value went to plain localStorage. Moving the encryption to the main process would not have helped: the values the bridge handles are the ones Teams already keeps in its own storage, some of them encrypted by its sign-in library with a key in the msal.cache.encryption cookie, so a second, separately encrypted copy would not change who can read them. The sections below describe the original decision and are kept as the record of it; where they describe encryption, read them as what was intended, not what the application does. Current behaviour is in the Security Architecture.

Context​

Microsoft Teams for Linux users experienced frequent authentication interruptions requiring manual re-login approximately every 24 hours, after system sleep/wake cycles, and after application restarts. Investigation revealed that the Electron wrapper lacked a proper token cache interface, preventing Teams' authentication provider from performing silent token refresh despite having valid refresh tokens in localStorage.

Problem Analysis​

  1. Missing Interface: Teams authentication provider expects a _tokenCache interface that wasn't provided
  2. Silent Refresh Failure: Without the cache interface, refresh tokens couldn't be used automatically
  3. User Experience Impact: Frequent re-authentication disrupted productivity
  4. Security Concerns: localStorage tokens stored in plain text

Technical Investigation​

  • Web Teams works in browsers due to native session/cache handling
  • Electron apps must explicitly provide storage interfaces expected by web applications
  • Teams authentication provider performs runtime checks for _tokenCache availability
  • MSAL (Microsoft Authentication Library) patterns suggest localStorage-compatible interface requirement

Decision​

We will implement a two-phase token cache solution:

Phase 1: localStorage-Compatible Token Cache Bridge​

  • Implement Storage interface (getItem, setItem, removeItem, clear)
  • Inject cache into Teams authentication provider at runtime
  • Use existing localStorage for storage backend
  • Enable immediate silent token refresh capability

Phase 2: Secure Storage with Electron safeStorage​

  • Integrate Electron's safeStorage API for OS-level encryption
  • Use natural transition approach (new tokens secure, existing tokens via fallback)
  • Maintain backward compatibility with graceful fallback mechanisms
  • Use platform-native secure storage (Keychain/DPAPI/kwallet)

Architectural Decisions​

1. Unified Implementation (Revised from initial multi-module approach)​

  • Chosen: Single tokenCache.js file with integrated secure storage
  • Rejected: Separate modules (secureTokenStorage.js, tokenMigration.js)
  • Rationale: Simplified architecture reduces complexity, easier maintenance, same functionality

2. Storage Backend Selection​

  • Chosen: Electron safeStorage API
  • Rejected: External libraries (keytar, node-keychain)
  • Rationale: Native Electron support, no external dependencies, cross-platform compatibility

3. Transition Strategy​

  • Chosen: Natural transition (new tokens secure, existing via fallback)
  • Rejected: Complex migration system with backup/rollback
  • Rationale: Eliminates migration complexity, zero risk, tokens refresh naturally

4. Fallback Mechanisms​

  • Chosen: Graceful degradation (secure storage → localStorage → memory)
  • Rejected: Fail-fast approach
  • Rationale: Ensures authentication works on all platforms regardless of secure storage availability

Consequences​

Positive​

  1. Immediate Problem Resolution: Silent token refresh eliminates re-authentication prompts
  2. Enhanced Security: OS-level encryption protects stored authentication tokens
  3. Cross-Platform Compatibility: Works on macOS (Keychain), Windows (DPAPI), Linux (kwallet/gnome)
  4. Zero Configuration: Automatic detection and migration, no user setup required
  5. Backward Compatibility: Graceful fallback ensures compatibility with all environments
  6. Maintainable Code: Simplified single-file architecture (425 lines vs 1400+ lines)

Negative​

  1. Platform Dependencies: Secure storage quality varies by platform (Linux has variable security)
  2. Gradual Security: Security improves gradually as tokens refresh naturally
  3. Async Interface: Teams cache interface needed to be made async-compatible
  4. Storage Size Limitation: Encrypted tokens stored in localStorage, subject to quota limits
  5. Security Trade-off: Application requires disabled Electron isolation (contextIsolation: false, sandbox: false) for DOM access functionality

Risks and Mitigations​

RiskImpactMitigation
Secure storage unavailableMediumAutomatic fallback to localStorage
Storage unavailableLowAutomatic fallback to localStorage, no disruption

Implementation Details​

Core Architecture​

Security Model​

  • Encryption: Platform-native encryption using OS cryptographic APIs
  • Key Management: Operating system manages encryption keys
  • Access Control: Application-specific token access
  • Fallback Security: Clear documentation of security levels per storage type

Performance Characteristics​

  • Initialization: Less than 50ms for secure storage setup
  • Token Operations: Less than 5ms for encrypt/decrypt operations
  • Memory Usage: Minimal overhead, no token caching in memory

Alternatives Considered​

1. External Keychain Libraries​

  • Keytar: Mature but requires native compilation
  • node-keychain: macOS only
  • Rejected: External dependencies, compilation complexity

2. File-Based Encryption​

  • Custom encryption with stored keys
  • Rejected: Key management complexity, security risks

3. Database Storage​

  • SQLite with encryption
  • Rejected: Overhead for simple key-value storage

4. No Secure Storage​

  • localStorage only implementation
  • Rejected: Security concerns for authentication tokens. It is ok but we can do better

Monitoring and Success Metrics​

Primary Success Criteria​

  • ✅ Zero re-authentication for 48+ hours continuous use
  • ✅ 100% authentication retention after system sleep cycles
  • ✅ 100% authentication persistence across application restarts
  • ✅ Less than 100ms performance overhead for token operations

Monitoring Indicators​

  • Authentication failure rate reduction
  • User support requests for re-authentication issues
  • Silent token refresh success rate
  • Cross-platform compatibility reports

Future Considerations​

Version 1.x (Current Implementation)​

  • Simplified secure storage with migration
  • Cross-platform compatibility
  • Graceful fallback mechanisms

Version 2.x (Future Enhancements)​

  • Hardware Security Module Integration: Leverage HSM for enterprise-grade key management and cryptographic operations

Decision Record Metadata​

  • Authors: Teams for Linux Development Team
  • Decision Date: September 2025
  • Implementation Date: September 2025
  • Review Date: December 2025 (planned)
  • Status: Implemented and Active
  • Related Issues: #1357 (Authentication refresh failures)
  • Related ADRs: None (first ADR in project)

[!NOTE] This ADR documents the complete decision process for implementing secure token storage in Teams for Linux. The simplified architecture achieved the same security and functionality goals with significantly reduced complexity.

[!IMPORTANT] The natural transition approach eliminates migration complexity while providing security benefits without user disruption.