Skip to main content

@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 SecRecord used has a Service value 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 in ApplicationData.Current.LocalSettings inside the [YOUR-APP-ID].microsoft.maui.essentials.preferences container, unpackaged apps store them in securestorage.dat in the app's data folder;
  • Web: browsers have no hardware-backed secure storage, so the web implementation falls back to localStorage as 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 your AndroidManifest.xml; or
  • Point android:fullBackupContent to a custom rule set that excludes ${applicationId}.microsoft.maui.essentials.preferences.xml from the sharedpref domain.

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.plist and 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 SecureStorage may 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.

ParamType
optionsGetOptions

Returns: Promise<GetResult>

set(options)​

Saves the value for the given key.

ParamType
optionsSetOptions

Returns: Promise<void>

remove(options)​

Removes the given key.

ParamType
optionsRemoveOptions

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​

PropTypeDescription
keystringThe key to read (must not be empty, otherwise INVALID_ARGUMENTS).

GetResult​

PropTypeDescription
valuestring | nullThe value for the key; null if never set or removed.

SetOptions​

PropTypeDescription
keystringThe key to write (must not be empty, otherwise INVALID_ARGUMENTS).
valuestringThe value to write (strings only; JSON.stringify complex objects).

RemoveOptions​

PropTypeDescription
keystringThe key to remove (must not be empty, otherwise INVALID_ARGUMENTS).

RemoveResult​

PropTypeDescription
successbooleanWhether the key existed and was removed (SecureStorage.Remove's return).