# `MobSecureStore`
[🔗](https://github.com/GenericJam/mob_secure_store/blob/main/lib/mob_secure_store.ex#L1)

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`.

# `reason`

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

Why the store couldn't do what was asked.

# `delete`

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

Removes `key`; removing an absent key is `:ok`.

# `get`

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

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

# `put`

```elixir
@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).

# `backend`

```elixir
@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`

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

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

# `get`

```elixir
@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 `t:reason/0`), so a failure is never mistaken for an
absent key.

# `put`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
