()
| 26 | export const macOsKeychainStorage = { |
| 27 | name: 'keychain', |
| 28 | read(): SecureStorageData | null { |
| 29 | const prev = keychainCacheState.cache |
| 30 | if (Date.now() - prev.cachedAt < KEYCHAIN_CACHE_TTL_MS) { |
| 31 | return prev.data |
| 32 | } |
| 33 | |
| 34 | try { |
| 35 | const storageServiceName = getMacOsKeychainStorageServiceName( |
| 36 | CREDENTIALS_SERVICE_SUFFIX, |
| 37 | ) |
| 38 | const username = getUsername() |
| 39 | const result = execSyncWithDefaults_DEPRECATED( |
| 40 | `security find-generic-password -a "${username}" -w -s "${storageServiceName}"`, |
| 41 | ) |
| 42 | if (result) { |
| 43 | const data = jsonParse(result) |
| 44 | keychainCacheState.cache = { data, cachedAt: Date.now() } |
| 45 | return data |
| 46 | } |
| 47 | } catch (_e) { |
| 48 | // fall through |
| 49 | } |
| 50 | // Stale-while-error: if we had a value before and the refresh failed, |
| 51 | // keep serving the stale value rather than caching null. Since #23192 |
| 52 | // clears the upstream memoize on every API request (macOS path), a |
| 53 | // single transient `security` spawn failure would otherwise poison the |
| 54 | // cache and surface as "Not logged in" across all subsystems until the |
| 55 | // next user interaction. clearKeychainCache() sets data=null, so |
| 56 | // explicit invalidation (logout, delete) still reads through. |
| 57 | if (prev.data !== null) { |
| 58 | logForDebugging('[keychain] read failed; serving stale cache', { |
| 59 | level: 'warn', |
| 60 | }) |
| 61 | keychainCacheState.cache = { data: prev.data, cachedAt: Date.now() } |
| 62 | return prev.data |
| 63 | } |
| 64 | keychainCacheState.cache = { data: null, cachedAt: Date.now() } |
| 65 | return null |
| 66 | }, |
| 67 | async readAsync(): Promise<SecureStorageData | null> { |
| 68 | const prev = keychainCacheState.cache |
| 69 | if (Date.now() - prev.cachedAt < KEYCHAIN_CACHE_TTL_MS) { |
nothing calls this directly
no test coverage detected