2015-05-20 15:33:48 +00:00
# react-native-keychain
2018-01-08 19:25:20 +00:00
[![Travis ](https://img.shields.io/travis/oblador/react-native-keychain.svg )](https://travis-ci.org/oblador/react-native-keychain) [![npm ](https://img.shields.io/npm/v/react-native-keychain.svg )](https://npmjs.com/package/react-native-keychain) [![npm ](https://img.shields.io/npm/dm/react-native-keychain.svg )](https://npmjs.com/package/react-native-keychain)
2017-07-21 20:33:19 +00:00
Keychain Access for React Native. Currently functionality is limited to just storing internet and generic passwords.
2015-05-20 18:39:52 +00:00
2018-01-08 21:23:24 +00:00
### New 2.0.0 with improved android implementation
2016-09-18 14:30:07 +00:00
2017-07-21 20:33:19 +00:00
The KeychainModule will now automatically use the appropriate CipherStorage implementation based on API level:
2016-09-18 14:30:07 +00:00
2017-07-21 20:33:19 +00:00
* API level 16-22 will en/de crypt using Facebook Conceal
* API level 23+ will en/de crypt using Android Keystore
2016-09-18 14:30:07 +00:00
2017-07-21 20:33:19 +00:00
Encrypted data is stored in SharedPreferences.
2016-09-18 14:30:07 +00:00
2015-05-20 18:39:52 +00:00
## Installation
2017-07-21 20:33:19 +00:00
1 . `$ npm install --save react-native-keychain`
2015-10-29 15:38:50 +00:00
2017-07-21 20:33:19 +00:00
or
2017-03-26 23:29:36 +00:00
2017-07-21 20:33:19 +00:00
`$ yarn add react-native-keychain`
2015-10-29 15:38:50 +00:00
2015-05-20 18:39:52 +00:00
2018-01-08 21:23:24 +00:00
2 . `$ react-native link react-native-keychain` and check `MainApplication.java` to verify the package was added.
2015-10-29 15:38:50 +00:00
2017-07-21 20:33:19 +00:00
3 . rebuild your project
* on Android, the `setInternetCredentials(server, username, password)` call will be resolved as call to `setGenericPassword(username, password, server)` . Use the `server` argument to distinguish between multiple entries.
Check out the "releases" tab for breaking changes and RN version compatibility. v1.0.0 is for RN >= 0.40
## ❗ Enable `Keychain Sharing` entitlement for iOS 10
For iOS 10 you'll need to enable the `Keychain Sharing` entitlement in the `Capabilities` section of your build target. (See screenshot). Otherwise you'll experience the error shown below.
![screen shot 2016-09-16 at 20 56 33 ](https://cloud.githubusercontent.com/assets/512692/18597833/15316342-7c50-11e6-92e7-781651e61563.png )
2015-10-29 15:38:50 +00:00
```
2017-07-21 20:33:19 +00:00
Error: {
code = "-34018";
domain = NSOSStatusErrorDomain;
message = "The operation couldn\U2019t be completed. (OSStatus error -34018.)";
}
2015-10-29 15:38:50 +00:00
```
2015-05-20 18:39:52 +00:00
2016-01-19 17:19:04 +00:00
2015-05-20 18:39:52 +00:00
## Usage
2015-05-20 18:41:10 +00:00
See `KeychainExample` for fully working project example.
2015-05-20 18:39:52 +00:00
```js
2017-01-10 21:25:04 +00:00
import * as Keychain from 'react-native-keychain';
2015-05-20 18:39:52 +00:00
2017-01-10 22:12:20 +00:00
const username = 'zuck';
const password = 'poniesRgr8';
2015-05-29 16:25:56 +00:00
// Generic Password, service argument optional
Keychain
.setGenericPassword(username, password)
.then(function() {
console.log('Credentials saved successfully!');
});
2016-06-05 17:17:48 +00:00
// service argument optional
2015-05-29 16:25:56 +00:00
Keychain
.getGenericPassword()
.then(function(credentials) {
console.log('Credentials successfully loaded for user ' + credentials.username);
2016-06-08 21:21:06 +00:00
}).catch(function(error) {
console.log('Keychain couldn\'t be accessed! Maybe no value set?', error);
2015-05-29 16:25:56 +00:00
});
2016-06-05 17:17:48 +00:00
// service argument optional
2015-05-29 16:25:56 +00:00
Keychain
.resetGenericPassword()
.then(function() {
console.log('Credentials successfully deleted');
});
// Internet Password, server argument required
2017-01-10 22:12:20 +00:00
const server = 'http://facebook.com';
2015-05-20 18:39:52 +00:00
Keychain
.setInternetCredentials(server, username, password)
.then(function() {
2015-05-29 16:25:56 +00:00
console.log('Credentials saved successfully!');
2015-05-20 18:39:52 +00:00
});
Keychain
.getInternetCredentials(server)
.then(function(credentials) {
2017-02-10 09:58:39 +00:00
if (credentials) {
console.log('Credentials successfully loaded for user ' + credentials.username);
}
2015-05-20 18:39:52 +00:00
});
Keychain
.resetInternetCredentials(server)
2015-05-29 16:25:56 +00:00
.then(function() {
console.log('Credentials successfully deleted');
2015-05-20 18:39:52 +00:00
});
2017-01-10 21:25:04 +00:00
Keychain
.requestSharedWebCredentials()
.then(function(credentials) {
if (credentials) {
console.log('Shared web credentials successfully loaded for user ' + credentials.username);
}
})
2017-01-10 22:12:20 +00:00
Keychain
.setSharedWebCredentials(server, username, password)
.then(function() {
console.log('Shared web credentials saved successfully!');
})
2015-05-20 18:39:52 +00:00
```
2017-07-21 20:33:19 +00:00
### Note on security
2016-05-31 22:25:51 +00:00
2017-07-21 20:33:19 +00:00
On API levels that do not support Android keystore, Facebook Conceal is used to en/decrypt stored data. The encrypted data is then stored in SharedPreferences. Since Conceal itself stores its encryption key in SharedPreferences, it follows that if the device is rooted (or if an attacker can somehow access the filesystem), the key can be obtained and the stored data can be decrypted. Therefore, on such a device, the conceal encryption is only an obscurity. On API level 23+ the key is stored in the Android Keystore, which makes the key non-exportable and therefore makes the entire process more secure. Follow best practices and do not store user credentials on a device. Instead use tokens or other forms of authentication and re-ask for user credentials before performing sensitive operations.
2016-08-26 06:40:09 +00:00
2017-07-21 20:33:19 +00:00
## Manual Installation
2016-08-26 06:40:09 +00:00
2017-07-21 20:33:19 +00:00
### iOS
2016-05-31 22:25:51 +00:00
2017-07-21 20:33:19 +00:00
#### Option: Manually
2016-08-26 06:40:09 +00:00
2017-07-21 20:33:19 +00:00
* Right click on Libraries, select **Add files to "…"** and select `node_modules/react-native-keychain/RNKeychain.xcodeproj`
* Select your project and under **Build Phases** -> **Link Binary With Libraries** , press the + and select `libRNKeychain.a` .
#### Option: With [CocoaPods](https://cocoapods.org/)
Add the following to your `Podfile` and run `pod update` :
```
2017-08-24 21:12:34 +00:00
pod 'RNKeychain', :path => '../node_modules/react-native-keychain'
2017-07-21 20:33:19 +00:00
```
### Android
2016-08-26 06:40:09 +00:00
2017-07-21 20:33:19 +00:00
#### Option: Manually
2016-08-26 06:40:09 +00:00
2016-05-31 22:25:51 +00:00
* Edit `android/settings.gradle` to look like this (without the +):
```diff
rootProject.name = 'MyApp'
include ':app'
+ include ':react-native-keychain'
+ project(':react-native-keychain').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keychain/android')
```
* Edit `android/app/build.gradle` (note: **app** folder) to look like this:
```diff
apply plugin: 'com.android.application'
android {
...
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.facebook.react:react-native:0.19.+'
+ compile project(':react-native-keychain')
}
```
2017-01-10 21:56:19 +00:00
* Edit your `MainApplication.java` (deep in `android/app/src/main/java/...` ) to look like this (note **two** places to edit):
2016-05-31 22:25:51 +00:00
```diff
package com.myapp;
+ import com.oblador.keychain.KeychainPackage;
....
public class MainActivity extends extends ReactActivity {
@Override
protected List< ReactPackage > getPackages() {
return Arrays.< ReactPackage > asList(
new MainReactPackage(),
+ new KeychainPackage()
);
}
...
}
```
2017-11-19 12:54:38 +00:00
#### Proguard Rules
On Android builds that use proguard (like release), you may see the following error:
```
RNKeychainManager: no keychain entry found for service:
JNI DETECTED ERROR IN APPLICATION: JNI FindClass called with pending exception java.lang.NoSuchFieldError: no "J" field "mCtxPtr" in class "Lcom/facebook/crypto/cipher/NativeGCMCipher;" or its superclasses
```
If so, add a proguard rule in `proguard-rules.pro` :
```
-keep class com.facebook.crypto.** {
*;
}
```
2016-05-31 22:25:51 +00:00
2017-07-21 20:36:20 +00:00
## Maintainers
< table >
< tbody >
< tr >
< td align = "center" >
< a href = "https://github.com/oblador" >
< img width = "150" height = "150" src = "https://github.com/oblador.png?v=3&s=150" >
< br >
< strong > Joel Arvidsson< / strong >
< / a >
< br >
Author
< / td >
< td align = "center" >
< a href = "https://github.com/vonovak" >
< img width = "150" height = "150" src = "https://github.com/vonovak.png?v=3&s=150" >
< / br >
< strong > Vojtech Novak< / strong >
< / a >
< br >
Maintainer
< / td >
< td align = "center" >
< a href = "https://github.com/pcoltau" >
< img width = "150" height = "150" src = "https://github.com/pcoltau.png?v=3&s=150" >
< / br >
< strong > Pelle Stenild Coltau< / strong >
< / a >
< br >
Maintainer
< / td >
< / tr >
< tbody >
< / table >
2015-05-20 18:39:52 +00:00
## License
2018-01-08 21:23:24 +00:00
MIT © Joel Arvidsson 2016-2018