Skip to content

3.3.0

Choose a tag to compare

@ioannisa ioannisa released this 03 Oct 05:27
· 10 commits to main since this release

KSafe can now sit behind your own interface: every typed call also accepts the serializer explicitly, so a plain fake can replace it in unit tests. Plus the four known issues from the 3.2.0 notes, fixed. Drop-in upgrade: existing data is untouched, nothing migrates, and the API change is additions only.

implementation("eu.anifantakis:ksafe:3.3.0")
implementation("eu.anifantakis:ksafe-compose:3.3.0")     // optional
implementation("eu.anifantakis:ksafe-biometrics:3.3.0")  // optional

New

  • Explicit-serializer overloads for get, getDirect, getFlow, getStateFlow, put and putDirect — requested in #37, thanks @abdulrahman-b0. Own section below. 👇

KSafe behind your own interface

You want your repositories to depend on an interface you own, backed by KSafe in the app and by a simple fake in unit tests. Until now that wrapper could not be written: KSafe's typed calls are inline with a reified type, and a generic interface method has no reified T to pass on.

override fun <T> get(key: String, defaultValue: T): T =
    safe.getDirect(key, defaultValue)   // ❌ Cannot use 'T' as reified type parameter

In 3.3.0 every typed call also takes a KSerializer<T> right after the value, so the interface simply passes it through:

interface SecureStore {
    fun <T> get(key: String, defaultValue: T, serializer: KSerializer<T>): T
    fun <T> put(key: String, value: T, serializer: KSerializer<T>)
}

class KSafeSecureStore(private val safe: KSafe) : SecureStore {
    override fun <T> get(key: String, defaultValue: T, serializer: KSerializer<T>): T =
        safe.getDirect(key, defaultValue, serializer)

    override fun <T> put(key: String, value: T, serializer: KSerializer<T>) =
        safe.putDirect(key, value, serializer)
}

// In unit tests: a FakeSecureStore backed by a map — no device, no Keystore, no Context
val repository = AuthRepository(FakeSecureStore())

You can also call the new forms directly: ksafe.put("profile", user, User.serializer()). The suspend get / put, getFlow, getStateFlow, and the mode and onWriteFailed options all have the same form.

Worth knowing:

  • Both forms share one store. A value written with a serializer reads back through the reified call, and the other way round.
  • A nullable T needs a nullable serializer: String.serializer().nullable. To keep short calls such as store.get("age", 0), add a two-line reified extension to your interface — shown in the docs.
  • Mode-typed views and delegates stay reified. Their type is fixed where you declare them; pass a KSafeWriteMode to put / putDirect instead.
  • KSerializer, not KClass. A KClass loses type arguments such as List<User> and nullability.

📖 README · docs/USAGE.md

Fixes

The four issues listed under Known, fixed in 3.2.1 in the 3.2.0 notes. They ship here because this release also adds API.

  • JVM: a failed namespace carry-forward no longer touches another app's data. When the one-time copy into the appNamespace directory failed, 3.2.0 ran the session from the shared un-namespaced store, so its writes and clearAll() could reach another app's values. The session now starts empty in its own directory, and the next launch whose copy succeeds brings the data back. A clearAll() made in that session stays in effect.
  • JVM: a key minted while the OS key vault could not load no longer replaces the real key when that real key still sits under an older vault namespace. Before, every value written before the failure could become unreadable.
  • Android: the android_lock_screen_absent note no longer outlives an Auto Backup restore. A properly bound key minted on the new device now clears the restored record.
  • Desktop biometrics: a pass-through no longer opens a prompt-free window. A permissive verifyBiometric that returns true because Windows Hello is unavailable, or because the native bridge failed to load, still returns true, but no longer fills the authorization cache.

Details: CHANGELOG.md

Full Changelog: 3.2.0...3.3.0