如何在React Native代码中存储敏感数据?

时间:2016-11-25 02:50:11

标签: javascript security react-native oauth oauth-2.0

我看到了几个问题"如何存储敏感数据和React Native" (例如thisthis),但是,所有这些案例都在谈论动态地(例如从服务器)获取一些敏感数据,然后使用AsyncStorage存储它。 但是,如果你需要在CODE中写一个敏感的TOKEN / PASSWORD呢?

例如,我想实现这个库:https://github.com/fullstackreact/react-native-oauth 正如您在第一个示例中所看到的,我必须在代码中写入秘密令牌。

在所有react-native项目目录中是否有一个文件,我可以放置我的令牌然后在应用程序中获取它? 在应用程序中操纵那些安全令牌有多安全?

由于

1 个答案:

答案 0 :(得分:2)

  

如何在React Native代码中存储敏感数据?

是的,有办法实现这一点,您可以使用react-native-keychain存储react-native

的敏感数据

对于iOS,它使用钥匙串共享功能

对于Android,它使用:

  • API级别16-22使用Facebook Conceal
  • API级别23+使用Android Keystore

你可以这样使用它:

// Generic Password, service argument optional
Keychain
  .setGenericPassword(username, password)
  .then(function() {
    console.log('Credentials saved successfully!');
  });

// service argument optional
Keychain
  .getGenericPassword()
  .then(function(credentials) {
    console.log('Credentials successfully loaded for user ' + credentials.username);
  }).catch(function(error) {
    console.log('Keychain couldn\'t be accessed! Maybe no value set?', error);
  });