ReviewOS

also looking at this

craft-native/craft

feat(android): the secure-storage quartet — ratchet 95 -> 91

#147
Merged chris wants to merge feat/android-secure-storage into main
6 files +501 -3
packages/android/templates/CraftNative.kt.templatemodified+73-0
Changes to packages/android/templates/CraftNative.kt.template
@@ -1,6 +1,8 @@
11package com.craft.runtime
22
33import android.app.Activity
4import android.content.SharedPreferences
5import org.json.JSONObject
46
57/**
68 * The Zig runtime's native methods.
@@ -64,6 +66,10 @@ object CraftNative {
6466 private external fun nativeOpenUrl(activity: Activity, url: String): Boolean
6567 private external fun nativeShare(activity: Activity, text: String, title: String): Boolean
6668 private external fun nativeGetNetworkStatus(activity: Activity): String?
69 private external fun nativeSecureSet(prefs: SharedPreferences, key: String, value: String): Boolean
70 private external fun nativeSecureGet(prefs: SharedPreferences, key: String): String?
71 private external fun nativeSecureRemove(prefs: SharedPreferences, key: String): Boolean
72 private external fun nativeSecureClear(prefs: SharedPreferences): Boolean
6773
6874 /**
6975 * `getDeviceInfo`, or null to use the Kotlin implementation.
@@ -176,4 +182,71 @@ object CraftNative {
176182 null
177183 }
178184 }
185
186 /**
187 * The three answers a secure read can give.
188 *
189 * `secureGet` returns `String?` to the page, where null means "no such
190 * key". The seam needs to say "Zig did not serve this" as well, and a
191 * nullable String cannot carry both — an absent key and a declined call
192 * lead to different code. There is no spare value to steal either: `""` is
193 * a legitimate stored value, and so is any sentinel a caller could have
194 * saved.
195 */
196 sealed class SecureRead {
197 /** Zig did not serve the call; run the Kotlin implementation. */
198 object Declined : SecureRead()
199
200 /** Zig served it and the key is not present. */
201 object NotFound : SecureRead()
202
203 /** Zig served it and the key holds [value], which may be empty. */
204 data class Found(val value: String) : SecureRead()
205 }
206
207 fun secureSet(prefs: SharedPreferences, key: String, value: String): Boolean {
208 if (!isAvailable) return false
209 return try {
210 nativeSecureSet(prefs, key, value)
211 } catch (e: UnsatisfiedLinkError) {
212 false
213 }
214 }
215
216 fun secureGet(prefs: SharedPreferences, key: String): SecureRead {
217 if (!isAvailable) return SecureRead.Declined
218 val envelope = try {
219 nativeSecureGet(prefs, key)
220 } catch (e: UnsatisfiedLinkError) {
221 null
222 } ?: return SecureRead.Declined
223
224 // A malformed envelope is Zig's bug, and declining is the safe answer:
225 // the Kotlin then reads the store itself and the page sees the truth.
226 return try {
227 val json = JSONObject(envelope)
228 if (json.getBoolean("found")) SecureRead.Found(json.getString("value"))
229 else SecureRead.NotFound
230 } catch (e: Exception) {
231 SecureRead.Declined
232 }
233 }
234
235 fun secureRemove(prefs: SharedPreferences, key: String): Boolean {
236 if (!isAvailable) return false
237 return try {
238 nativeSecureRemove(prefs, key)
239 } catch (e: UnsatisfiedLinkError) {
240 false
241 }
242 }
243
244 fun secureClear(prefs: SharedPreferences): Boolean {
245 if (!isAvailable) return false
246 return try {
247 nativeSecureClear(prefs)
248 } catch (e: UnsatisfiedLinkError) {
249 false
250 }
251 }
179252}