@napacitor/security
Secure key-value storage plugin for storing sensitive values (auth tokens, encryption keys, etc.) using OS-level security facilities.
Installation
To use npm:
npm install @napacitor/security
To use yarn:
yarn add @napacitor/security
Sync native files:
npx nap sync
Usage
import { Security } from '@napacitor/security';
await Security.set({ key: 'oauth_token', value: 'secret-oauth-token-value' });
const { value } = await Security.get({ key: 'oauth_token' }); // 'secret-oauth-token-value'
// value is null when the key does not exist
const { success } = await Security.remove({ key: 'oauth_token' }); // { success: true }
await Security.clear(); // Removes all secure storage values
How It Works
Wraps the APIs described in the .NET MAUI SecureStorage documentation: SecureStorage.Default.GetAsync / SetAsync / Remove / RemoveAll.
Underlying platform implementations:
- iOS / Mac Catalyst: Keychain (the
SecRecordused has aServicevalue of[YOUR-APP-BUNDLE-ID].microsoft.maui.essentials.preferences); - Android:
EncryptedSharedPreferences(Android Security library) — keys are deterministically encrypted, values are non-deterministically encrypted using AES-256 GCM, with keys protected by the Android Keystore; - Windows: values are encrypted with
DataProtectionProvider; packaged apps store them inApplicationData.Current.LocalSettingsinside the[YOUR-APP-ID].microsoft.maui.essentials.preferencescontainer, unpackaged apps store them insecurestorage.datin the app's data folder; - Web: browsers have no hardware-backed secure storage, so the web implementation falls back to
localStorageas a placeholder (plaintext, not secure; for development/debugging only — never store secrets in it in production).
The API is designed to store small amounts of text; storing large amounts of text may impact performance.
Permissions
Android
No additional configuration required. However, be aware of Auto Backup: app data (including the encrypted SharedPreferences) is restored from Google cloud backups, but the encryption keys are not transferred with it, so restored values cannot be decrypted — MAUI automatically removes the affected key, but calling get() on a still-cached corrupted value may throw. Recommended mitigations (choose one):
- Set
android:allowBackup="false"in yourAndroidManifest.xml; or - Point
android:fullBackupContentto a custom rule set that excludes${applicationId}.microsoft.maui.essentials.preferences.xmlfrom thesharedprefdomain.
iOS
- When developing on the iOS simulator, enable the Keychain entitlement and add a keychain access group for the app's bundle identifier (edit
Entitlements.plistand set it as Custom Entitlements under iOS Bundle Signing); - When deploying to an iOS device, the entitlement is not required and should be removed;
- Uninstalling an iOS app does not remove its Keychain entries; a reinstall can still read previously stored values. Before selling or sharing a device, check a first-launch flag on first run and call
clear():
import { Preferences } from '@napacitor/preferences';
import { Security } from '@napacitor/security';
const { value } = await Preferences.get({ key: 'app_initialized' });
if (value === null) {
await Security.clear();
await Preferences.set({ key: 'app_initialized', value: 'true' });
}
- If the device user has iCloud Keychain enabled, Keychain values written by
SecureStoragemay silently sync to their other Apple devices (not controllable from .NET MAUI).
Windows
No setup is required. Note the LocalSettings restrictions: setting names may be at most 255 characters, each setting up to 8K bytes, and each composite setting up to 64K bytes.
Error Handling
get() / set() may throw (error code ERROR, with guidance in the message) when the device does not support secure storage, encryption keys change, or data is corrupted. Catch and call clear() to wipe unreadable values, then re-enter or re-fetch them:
try {
const { value } = await Security.get({ key: 'auth_token' });
} catch (e) {
// Stored values can't be decrypted on this device (e.g. after backup restore) — clear and re-acquire
await Security.clear();
}
API
get(options)
Retrieves the value for the given key.
| Param | Type |
|---|---|
options | GetOptions |
Returns: Promise<GetResult>
set(options)
Saves the value for the given key.
| Param | Type |
|---|---|
options | SetOptions |
Returns: Promise<void>
remove(options)
Removes the given key.
| Param | Type |
|---|---|
options | RemoveOptions |
Returns: Promise<RemoveResult>
clear()
Removes all secure storage values (note: maps directly to SecureStorage.RemoveAll(), which clears the entire SecureStorage domain).
Returns: Promise<void>
Interfaces
GetOptions
| Prop | Type | Description |
|---|---|---|
key | string | The key to read (must not be empty, otherwise INVALID_ARGUMENTS). |
GetResult
| Prop | Type | Description |
|---|---|---|
value | string | null | The value for the key; null if never set or removed. |
SetOptions
| Prop | Type | Description |
|---|---|---|
key | string | The key to write (must not be empty, otherwise INVALID_ARGUMENTS). |
value | string | The value to write (strings only; JSON.stringify complex objects). |
RemoveOptions
| Prop | Type | Description |
|---|---|---|
key | string | The key to remove (must not be empty, otherwise INVALID_ARGUMENTS). |
RemoveResult
| Prop | Type | Description |
|---|---|---|
success | boolean | Whether the key existed and was removed (SecureStorage.Remove's return). |