File size: 7,324 Bytes
e079a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**

 * @fileoverview The platform pieces of a credential vault: expo-secure-store for the secure store, and MMKV for

 * the move record and the pre-move copies.

 *

 * expo-secure-store keeps a value in the iOS Keychain, and on Android encrypts it with an AES-256-GCM key held in

 * the Android Keystore before storing the ciphertext in the app's private SharedPreferences. Items are readable

 * after the first unlock since boot (`kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly`), so a screen-off or

 * background launch for Bluetooth can still sign in to Core, and they never leave this device in a backup.

 */
import {createMMKV, type MMKV} from "react-native-mmkv"

import {
  createCredentialVault,
  type CredentialVault,
  type LegacyCredentials,
  type MoveRecordStore,
  type SecureItemStore,
} from "./credentialVault"

/** The Keychain service (iOS) and Keystore alias (Android) of every Mentra credential item. */
export const CREDENTIAL_KEYCHAIN_SERVICE = "com.mentra.credentials"

/** The MMKV instance holding only the vaults' move records. Logout's `clearAll()` of the default instance spares it. */
export const CREDENTIAL_MOVE_RECORD_MMKV_ID = "mentra-credentials"

type ExpoSecureStore = typeof import("expo-secure-store")

let secureStoreModule: ExpoSecureStore | null = null

/** Loaded on first use, so importing the engine never touches the native module. */
function expoSecureStore(): ExpoSecureStore {
  secureStoreModule ??= require("expo-secure-store") as ExpoSecureStore
  return secureStoreModule
}

function itemOptions(store: ExpoSecureStore) {
  return {
    keychainService: CREDENTIAL_KEYCHAIN_SERVICE,
    // iOS only; Android Keystore keys without user authentication are usable while the screen is locked.
    keychainAccessible: store.AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY,
  }
}

export const platformSecureItems: SecureItemStore = {
  getItem(key) {
    const store = expoSecureStore()
    return store.getItemAsync(key, itemOptions(store))
  },
  setItem(key, value) {
    const store = expoSecureStore()
    return store.setItemAsync(key, value, itemOptions(store))
  },
  deleteItem(key) {
    const store = expoSecureStore()
    return store.deleteItemAsync(key, itemOptions(store))
  },
}

let moveRecordStore: MMKV | null = null

export function credentialMoveRecord(): MoveRecordStore {
  moveRecordStore ??= createMMKV({id: CREDENTIAL_MOVE_RECORD_MMKV_ID})
  return moveRecordStore
}

type LegacyMmkv = Pick<MMKV, "getString" | "set" | "remove" | "getAllKeys" | "trim">

/** react-native-mmkv's id for the default instance, which is also its file name. */
const DEFAULT_MMKV_ID = "mmkv.default"

/** Where react-native-mmkv keeps an instance: `Documents/mmkv/<id>` on iOS, `files/mmkv/<id>` on Android. */
function mmkvFileSize(id: string): number | null {
  const {File, Paths} = require("expo-file-system") as typeof import("expo-file-system")
  const file = new File(Paths.document, "mmkv", id)
  return file.exists ? file.size : null
}

/** Never build a filler this large in memory; such a file is not one of ours. */
const MAX_SCRUB_BYTES = 16 * 1024 * 1024
const SCRUB_KEY = "mentra.credentials.scrub"

/**

 * Overwrites what an MMKV file still holds of removed values.

 *

 * MMKV appends: removing a key appends a tombstone, and a full write-back moves the live values to the front without

 * clearing what they leave behind, so a removed token's bytes stay in the file. Writing a value longer than the

 * whole file makes MMKV write back first (the value cannot fit), then write the value over everything past the live

 * values. Removing it and trimming leaves the live values and filler.

 */
function scrubRemovedValues(store: LegacyMmkv, fileSize: number): void {
  store.set(SCRUB_KEY, "0".repeat(fileSize + 1))
  store.remove(SCRUB_KEY)
  store.trim()
}

export interface MmkvLegacyCredentialsOptions {
  /** The MMKV instance id; the default instance when omitted. */
  id?: string
  /** The credential keys, or every key when the instance holds nothing else. */
  keys: readonly string[] | "every key"
  /** `json`: written with the app's `storage.save`, which JSON-encodes. `raw`: the string itself. */
  encoding: "json" | "raw"
  /** Credentials of retired sign-in providers: deleted whenever found, never read. */
  isRetiredKey?: (key: string) => boolean
  /** Tests inject a fake instance and file size. */
  open?: () => LegacyMmkv
  fileSize?: () => number | null
}

/**

 * Decodes one `storage.save` value. Strings come back as themselves. Anything else (a JSON object such as a saved

 * session) is kept as its JSON text, which is what its owner stores in the vault.

 */
function decodeJsonValue(raw: string): string {
  try {
    const value: unknown = JSON.parse(raw)
    return typeof value === "string" ? value : raw
  } catch {
    return raw
  }
}

export function mmkvLegacyCredentials(options: MmkvLegacyCredentialsOptions): LegacyCredentials {
  let instance: LegacyMmkv | null = null
  const open = (): LegacyMmkv => {
    instance ??= options.open?.() ?? createMMKV(options.id === undefined ? undefined : {id: options.id})
    return instance
  }
  // The scrub's filler is never a credential, even when a crash left it behind.
  const keys = (): readonly string[] =>
    options.keys === "every key"
      ? open()
          .getAllKeys()
          .filter((key) => key !== SCRUB_KEY)
      : options.keys
  const decode = (raw: string | undefined): string | null => {
    if (raw === undefined || raw === "") return null
    return options.encoding === "json" ? decodeJsonValue(raw) : raw
  }

  return {
    readAll() {
      const copies: Record<string, string> = {}
      for (const key of keys()) {
        const value = decode(open().getString(key))
        if (value !== null) copies[key] = value
      }
      return copies
    },
    get(key) {
      return decode(open().getString(key))
    },
    set(key, value) {
      open().set(key, options.encoding === "json" ? JSON.stringify(value) : value)
    },
    remove(key) {
      open().remove(key)
    },
    hasRetired() {
      const isRetired = options.isRetiredKey
      if (!isRetired) return false
      return open()
        .getAllKeys()
        .some((key) => isRetired(key))
    },
    eraseAll() {
      const store = open()
      for (const key of keys()) store.remove(key)
      store.remove(SCRUB_KEY)
      const isRetired = options.isRetiredKey
      if (!isRetired) return
      for (const key of store.getAllKeys()) {
        if (isRetired(key)) store.remove(key)
      }
    },
    scrub() {
      const fileSize = options.fileSize ? options.fileSize() : mmkvFileSize(options.id ?? DEFAULT_MMKV_ID)
      if (fileSize === null) return false
      if (fileSize > MAX_SCRUB_BYTES) return false
      scrubRemovedValues(open(), fileSize)
      return true
    },
  }
}

/** A vault in the platform secure store whose credentials used to live in MMKV. */
export function createPlatformCredentialVault(name: string, legacy: LegacyCredentials): CredentialVault {
  return createCredentialVault({name, secure: platformSecureItems, moveRecord: credentialMoveRecord, legacy})
}