plbls-storage
v0.5.0
Published
## Overview
Downloads
73
Readme
Plbls Secure Storage Service
Overview
The Plbls Secure Storage Service is an Angular service designed to provide secure data storage in Ionic applications across multiple platforms: iOS, Android, and Web. It abstracts the platform-specific implementations and offers a simple API for storing, retrieving, and removing data securely.
Features
- Platform Detection: Automatically detects the running platform and applies the appropriate secure storage mechanism.
- Secure Storage:
- iOS & Android: Interacts with native APIs for secure data handling.
- Web: Utilizes
localStorage
with Base64 encoding for basic security.
- Unified API: Offers consistent methods for data manipulation regardless of the platform.
- Easy Integration: Simple to set up and integrate into your existing Angular/Ionic projects.
Installation
Install the package via npm:
npm install plbls-secure-storage-service
Usage
Importing the Service
First, import the PlblsStorageService
into your Angular component or service where you intend to use it.
import { PlblsStorageService } from 'plbls-secure-storage-service';
Injecting the Service
Inject the PlblsStorageService
through your component's or service's constructor.
constructor(private plblsStorageService: PlblsStorageService) {}
Storing Data
Use the setItem
method to store data securely.
this.plblsStorageService.setItem('key', 'value');
Retrieving Data
Use the getItem
method to retrieve stored data.
this.plblsStorageService.getItem('key').then((value) => {
console.log('Retrieved value:', value);
});
Removing Data
Use the removeItem
method to delete data from secure storage.
this.plblsStorageService.removeItem('key');
Getting Platform Information
You can retrieve the platform name using the getPlatform
method.
const platform = this.plblsStorageService.getPlatform();
console.log('Platform:', platform); // Outputs 'ios', 'android', or 'other'
Platform-Specific Implementation
iOS and Android
On iOS and Android, the service interacts with native code through WebView interfaces. Ensure that your native code exposes the following JavaScript interfaces:
iOS:
window.iOSSecureStorage
- Methods:
// Retrieve data window.iOSSecureStorage.getData(key: string): string; // Store data window.iOSSecureStorage.saveData(key: string, value: string): void; // Remove data window.iOSSecureStorage.removeData(key: string): void;
Android:
window.AndroidSecureStorage
- Methods:
// Retrieve data window.AndroidSecureStorage.getData(key: string): string; // Store data window.AndroidSecureStorage.saveData(key: string, value: string): void; // Remove data window.AndroidSecureStorage.removeData(key: string): void;
Implement these interfaces in your native iOS and Android code to enable secure storage functionality.
Web (Other Platforms)
For web platforms, the service uses localStorage
and applies Base64 encoding for basic data obfuscation.
Example
Below is an example demonstrating how to use the PlblsStorageService
in an Angular component.
import { Component, OnInit } from '@angular/core';
import { PlblsStorageService } from 'plbls-secure-storage-service';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
})
export class ExampleComponent implements OnInit {
constructor(private plblsStorageService: PlblsStorageService) {}
async ngOnInit() {
// Store data
this.plblsStorageService.setItem('username', 'john_doe');
// Retrieve data
const username = await this.plblsStorageService.getItem('username');
console.log('Username:', username);
// Remove data
this.plblsStorageService.removeItem('username');
}
}
Configuration
No additional configuration is required for the Angular service itself. However, ensure that:
- For iOS and Android: The native interfaces (
iOSSecureStorage
andAndroidSecureStorage
) are properly implemented and exposed to the WebView. - For Web: No special configuration is needed; the service will use
localStorage
by default.
Dependencies
- Angular
- Ionic Angular
Make sure these are installed and properly configured in your project.
Compatibility
- iOS: Requires implementation of
iOSSecureStorage
in native code. - Android: Requires implementation of
AndroidSecureStorage
in native code. - Web Browsers: Works on all modern browsers with support for
localStorage
.
Limitations
- The Web implementation uses Base64 encoding, which is not a secure encryption method. For- highly sensitive data, consider implementing additional encryption mechanisms.
- Native implementations must be provided for full functionality on iOS and Android.