Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node_modules | ||
|
||
.DS_Store | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Charles Crete | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONeNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# redux-persist-expo-securestore | ||
|
||
Storage engine for [redux-persist](https://github.com/rt2zz/redux-persist) for use with [Expo's SecureStorage ](https://docs.expo.io/versions/latest/sdk/securestore.html). | ||
|
||
> iOS: Values are stored using the keychain services as kSecClassGenericPassword. iOS has the additional option of being able to set the value’s kSecAttrAccessible attribute, which controls when the value is available to be fetched. | ||
> | ||
> Android: Values are stored in SharedPreferences, encrypted with Android’s Keystore system. | ||
## Installation | ||
|
||
Requires Expo SDK (automatically used when using [Expo](https://expo.io/) or [create-react-native-app](https://github.com/react-community/create-react-native-app)). | ||
|
||
Yarn: `yarn add redux-persist-expo-securestore` | ||
|
||
npm: `npm install --save redux-persist-expo-securestore` | ||
|
||
## Usage | ||
|
||
Use as a `redux-persist` global storage engine: | ||
|
||
```js | ||
import createSecureStore from "redux-persist-expo-securestore"; | ||
|
||
import { compose, applyMiddleware, createStore } from "redux"; | ||
import { persistStore, persistCombineReducers } from "redux-persist"; | ||
import reducers from "./reducers"; | ||
|
||
// Secure storage | ||
const storage = createSecureStore(); | ||
const config = { | ||
key: "root", | ||
storage | ||
}; | ||
|
||
const reducer = persistCombineReducers(config, reducers); | ||
|
||
function configureStore () { | ||
// ... | ||
let store = createStore(reducer); | ||
let persistor = persistStore(store); | ||
|
||
return { persistor, store }; | ||
} | ||
``` | ||
|
||
Use as an engine for only a reducer: | ||
|
||
```js | ||
import createSecureStore from "redux-persist-expo-securestore"; | ||
|
||
import { combineReducers } from "redux"; | ||
import { persistReducer } from "redux-persist"; | ||
import AsyncStorage from 'redux-persist/lib/storage'; | ||
|
||
import { mainReducer, secureReducer } from "./reducers"; | ||
|
||
// Secure storage | ||
const secureStorage = createSecureStore(); | ||
const securePersistConfig = { | ||
key: "secure", | ||
storage: secureStorage | ||
}; | ||
|
||
// Non-secure (AsyncStorage) storage | ||
const mainPersistConfig = { | ||
key: "main", | ||
storage: AsyncStorage | ||
}; | ||
|
||
// Combine them together | ||
const rootReducer = combineReducers({ | ||
main: persistReducer(mainPersistConfig, mainReducer), | ||
secure: persistReducer(securePersistConfig, secureReducer) | ||
}); | ||
|
||
function configureStore () { | ||
// ... | ||
let store = createStore(rootReducer); | ||
let persistor = persistStore(store); | ||
|
||
return { persistor, store }; | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### `createSecureStore([options])` | ||
|
||
#### `[options]`: `object` | ||
|
||
Options to pass to [Expo's SecureStore](https://docs.expo.io/versions/latest/sdk/securestore.html). | ||
|
||
##### `keychainService`: `string` | ||
|
||
iOS: The item’s service, equivalent to kSecAttrService | ||
|
||
Android: Equivalent of the public/private key pair Alias | ||
|
||
##### `keychainAccessible`: `enum` | ||
|
||
iOS only: Specifies when the stored entry is accessible, using iOS’s kSecAttrAccessible property. See Apple’s documentation on keychain item accessibility. The available options are: | ||
|
||
> Expo.SecureStore.WHEN_UNLOCKED: The data in the keychain item can be accessed only while the device is unlocked by the user. | ||
> Expo.SecureStore.AFTER_FIRST_UNLOCK: The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user. This may be useful if you need to access the item when the phone is locked. | ||
> Expo.SecureStore.ALWAYS: The data in the keychain item can always be accessed regardless of whether the device is locked. This is the least secure option. | ||
> Expo.SecureStore.WHEN_UNLOCKED_THIS_DEVICE_ONLY: Similar to WHEN_UNLOCKED, except the entry is not migrated to a new device when restoring from a backup. | ||
> Expo.SecureStore.WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: Similar to WHEN_UNLOCKED_THIS_DEVICE_ONLY, except the user must have set a passcode in order to store an entry. If the user removes their passcode, the entry will be deleted. | ||
> Expo.SecureStore.AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY: Similar to AFTER_FIRST_UNLOCK, except the entry is not migrated to a new device when restoring from a backup. | ||
> Expo.SecureStore.ALWAYS_THIS_DEVICE_ONLY: Similar to ALWAYS, except the entry is not migrated to a new device when restoring from a backup. | ||
##### `replaceCharacter`: `string` | ||
|
||
Default: `_` | ||
|
||
See `Caveat`. | ||
|
||
##### `replacer`: `function(key: string, replaceCharacter: string): string` | ||
|
||
Default: replace all illegal characters by `replaceCharacter` | ||
|
||
See `Caveat`. | ||
|
||
## Caveat | ||
|
||
Keys for SecureStorage only support `[A-Za-z0-9.-_]`, meaning all other characters are replaced by `options.replaceCharacter` (defaults to `_`). | ||
|
||
You may change this character by replacing `options.replaceCharacter`. | ||
|
||
You may also change the default key transformer by replacing `options.replacer`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "redux-persist-expo-securestore", | ||
"version": "0.1.0", | ||
"description": "redux-persist storage for Expo's SecureStore", | ||
"main": "src/index.js", | ||
"repository": "git@github.com:Cretezy/redux-persist-expo-securestore.git", | ||
"author": "Charles Crete <charles@cretezy.com>", | ||
"license": "MIT", | ||
"private": false, | ||
"peerDependencies": { | ||
"expo": ">=20" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { SecureStore } from "expo"; | ||
|
||
export default function createSecureStorage(options = {}) { | ||
const replaceCharacter = options.replaceCharacter || "_"; | ||
const replacer = options.replacer || defaultReplacer; | ||
|
||
return { | ||
getItem: key => | ||
SecureStore.getItemAsync(replacer(key, replaceCharacter), options), | ||
setItem: (key, value) => | ||
SecureStore.setItemAsync(replacer(key, replaceCharacter), value, options), | ||
removeItem: key => | ||
SecureStore.removeItemAsync(replacer(key, replaceCharacter), options) | ||
}; | ||
} | ||
|
||
function defaultReplacer(key, replaceCharacter) { | ||
return key.replace(/[^a-z0-9.\-_]/gi, replaceCharacter); | ||
} |