MobSecureStore behaviour (mob_secure_store v0.1.0)

Copy Markdown View Source

Small secrets (tokens, API keys) kept in the platform's secure store.

:ok = MobSecureStore.put("anthropic", token)
{:ok, ^token} = MobSecureStore.get("anthropic")
:ok = MobSecureStore.delete("anthropic")
:error = MobSecureStore.get("anthropic")

Where the values live depends on where the code runs (backend/0):

  • Android: EncryptedSharedPreferences under an Android Keystore master key (MobSecureStore.Native).
  • iOS: the Keychain, as generic-password items readable after the device's first unlock, never synced or backed up to another device (MobSecureStore.Native).
  • A phone whose app was built without this plugin's NIF (the plugin was activated after the last native build, and only BEAMs were deployed): every call is {:error, :secure_store_unavailable} (MobSecureStore.Unavailable). The store fails closed instead of writing secrets to plain files. Rebuild with mix mob.deploy --native.
  • The host (development, tests): one 0600 file per key in a 0700 directory under the app's data dir (MobSecureStore.File).

config :mob_secure_store, :backend, Module replaces the backend (a module implementing this behaviour), e.g. a fake in a host app's tests. On a phone MobSecureStore.File is refused even when configured.

Keys are non-empty UTF-8 strings; values are any binary. A key or value of another shape raises ArgumentError.

Summary

Types

Why the store couldn't do what was asked.

Callbacks

Removes key; removing an absent key is :ok.

The value at key, or :error when there is none.

Upserts key: :ok means written, not queued (see each backend for what survives a power cut).

Functions

The backend in effect: the configured one, else the one for where the code runs (detected once per VM). On a phone a configured MobSecureStore.File is refused: the backend is then MobSecureStore.Unavailable.

Removes key. Removing a key that isn't there is :ok.

The value stored at key.

Stores value at key, replacing what was there. :ok means it's written.

Types

reason()

@type reason() ::
  :secure_store_unavailable
  | :no_data
  | {:sec, integer()}
  | {:jni, atom()}
  | File.posix()
  | term()

Why the store couldn't do what was asked.

Callbacks

delete(key)

@callback delete(key :: String.t()) :: :ok | {:error, reason()}

Removes key; removing an absent key is :ok.

get(key)

@callback get(key :: String.t()) :: {:ok, binary()} | :error | {:error, reason()}

The value at key, or :error when there is none.

put(key, value)

@callback put(key :: String.t(), value :: binary()) :: :ok | {:error, reason()}

Upserts key: :ok means written, not queued (see each backend for what survives a power cut).

Functions

backend()

@spec backend() :: module()

The backend in effect: the configured one, else the one for where the code runs (detected once per VM). On a phone a configured MobSecureStore.File is refused: the backend is then MobSecureStore.Unavailable.

delete(key)

@spec delete(String.t()) :: :ok | {:error, reason()}

Removes key. Removing a key that isn't there is :ok.

get(key)

@spec get(String.t()) :: {:ok, binary()} | :error | {:error, reason()}

The value stored at key.

:error when nothing is stored there; {:error, reason} when the store itself failed (see reason/0), so a failure is never mistaken for an absent key.

put(key, value)

@spec put(String.t(), binary()) :: :ok | {:error, reason()}

Stores value at key, replacing what was there. :ok means it's written.