Secure Storage Adapter
Hardware-backed secure storage for sensitive data.
Overview
The Secure adapter provides encrypted storage using platform-specific secure mechanisms - Keychain on iOS and EncryptedSharedPreferences on Android.
Capabilities
| Feature | Support |
|---|---|
| Persistence | ✅ Yes |
| Synchronous | ❌ No (async) |
| Observable | ❌ No |
| Searchable | ✅ Yes (limited) |
| Iterable | ✅ Yes |
| Capacity | ~5MB |
| Performance | 🔒 Secure |
| TTL Support | ✅ Yes (manual) |
| Batch Support | ✅ Yes |
| Transaction Support | ❌ No |
Usage
import { Strata } from 'strata-storage';
const storage = new Strata();
await storage.initialize();
// Store sensitive data
await storage.set('api_key', secretKey, {
storage: 'secure'
});
Configuration
const storage = new Strata({
adapters: {
secure: {
accessibility: 'whenUnlockedThisDeviceOnly', // iOS
authenticationPrompt: 'Authenticate to access data',
biometricOnly: false
}
}
});
Platform Implementation
iOS (Keychain)
// Keychain Services implementation
import Security
class KeychainStorage {
func save(key: String, value: Data) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecValueData as String: value,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
]
SecItemAdd(query as CFDictionary, nil)
}
}
Android (EncryptedSharedPreferences)
// Android encrypted storage
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;
MasterKey masterKey = new MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
SharedPreferences prefs = EncryptedSharedPreferences.create(
context,
"secure_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
Use Cases
1. Authentication Tokens
class AuthTokenManager {
async saveToken(token: string) {
await storage.set('auth_token', token, {
storage: 'secure',
ttl: 3600000 // 1 hour
});
}
async getToken(): Promise<string | null> {
return await storage.get('auth_token', {
storage: 'secure'
});
}
async clearToken() {
await storage.remove('auth_token', {
storage: 'secure'
});
}
}
2. Biometric Protection
class BiometricStorage {
async saveSensitive(key: string, value: string) {
await storage.set(key, value, {
storage: 'secure',
biometricOnly: true,
authenticationPrompt: 'Authenticate to access your data'
});
}
async getSensitive(key: string) {
try {
return await storage.get(key, {
storage: 'secure',
authenticationPrompt: 'Authenticate to retrieve data'
});
} catch (error) {
if (error.code === 'UserCancel') {
console.log('User cancelled authentication');
}
throw error;
}
}
}
3. Encryption Keys
class EncryptionKeyManager {
async generateAndStore() {
const key = await crypto.subtle.generateKey(
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
const exported = await crypto.subtle.exportKey('raw', key);
const keyString = btoa(String.fromCharCode(...new Uint8Array(exported)));
await storage.set('encryption_key', keyString, {
storage: 'secure',
accessibility: 'whenUnlockedThisDeviceOnly'
});
}
}
Security Features
Accessibility Options (iOS)
enum Accessibility {
WhenUnlocked = 'whenUnlocked',
WhenUnlockedThisDeviceOnly = 'whenUnlockedThisDeviceOnly',
AfterFirstUnlock = 'afterFirstUnlock',
AfterFirstUnlockThisDeviceOnly = 'afterFirstUnlockThisDeviceOnly',
WhenPasscodeSetThisDeviceOnly = 'whenPasscodeSetThisDeviceOnly'
}
Hardware Security
- iOS: Hardware-encrypted Keychain
- Android: Hardware-backed Android Keystore
- Biometric: Touch ID / Face ID / Fingerprint
Best Practices
- Minimal Storage: Store only essential sensitive data
- Short TTLs: Use expiration for temporary secrets
- Biometric Protection: Enable for highly sensitive data
- Error Handling: Handle authentication failures gracefully
- Backup Exclusion: Exclude from iCloud/Google backups
Limitations
- Size Limits: ~5MB total capacity
- Performance: Slower due to encryption
- No Sync: Cannot sync across devices
- Platform Specific: Different behaviors per platform