简体   繁体   English

如何在android中安全地保存Oauth Access令牌

[英]How to save Oauth Access token securely in android

I have access token from the server after authentication lets say "uyhjjfjfgg567f8fhjkkf" now I want to save it in the device securely. 我在身份验证后说"uyhjjfjfgg567f8fhjkkf"现在要安全地将其保存在设备中,我从服务器访问令牌。 I looked in Keystore and Keychain in android developer sites. 我查看了android开发者网站中的Keystore和Keychain。 I dont clearly understand how it works and how we should retrieve the token from the keystore. 我不清楚它是如何工作的以及我们应该如何从密钥库中检索令牌。

KeyPairGenerator kpg = KeyPairGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder(
        alias,
        KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
        .setDigests(KeyProperties.DIGEST_SHA256,
            KeyProperties.DIGEST_SHA512)
        .build());

KeyPair kp = kpg.generateKeyPair();


/*
 * Load the Android KeyStore instance using the the
 * "AndroidKeyStore" provider to list out what entries are
 * currently stored.
 */

KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
Enumeration<String> aliases = ks.aliases();

You don't need to save the access token, since it has short life anyway. 您无需保存访问令牌,因为它的生命周期很短。 Keeping it in memory is good enough. 将它保存在内存中就足够了。

You do need to keep the refresh token, and you have a few options for that: 您确实需要保留刷新令牌,并且您有以下几种选择:

  • In a file 在一个文件中
    • Either directly in a file in the internal storage 直接在内部存储中的文件中
    • or using SharedPreferences 或使用SharedPreferences
    • or in a Database 或在数据库中
  • Using the AccountManager 使用AccountManager

Consider using the StoredCredential . 考虑使用StoredCredential For the flow itself, I recommend you to use Google AppAuth library . 对于流本身,我建议您使用Google AppAuth库

Of course, you can also encrypt the key using a cipher: 当然,您也可以使用密码加密密钥:

private static byte[] encrypt(byte[] key, byte[] text) throws GeneralSecurityException {
    final SecretKeySpec skeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
    final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, sInitVectorSpec);
    return cipher.doFinal(text);
}

And the key can be stored in the KeyStore . 密钥可以存储在KeyStore

Here you can find a really good article by Androidauthority regarding the possibilities available for Android Security. 在这里,您可以找到Androidauthority关于Android安全可用性的非常好的文章。

A comprehensive example of Android keystore implementation can be found here . 可以在此处找到Android密钥库实现的综合示例。

And another good option is Google's keyczar that you can follow on the git repository for samples and details. 另一个不错的选择是Google的keyczar ,您可以在git存储库中查看示例和详细信息。 There you can also find a detailed list of the Known Security Issues , so you can see if it suits your further implementation. 在那里,您还可以找到已知安全问题的详细列表,以便了解它是否适​​合您的进一步实施。

For your current issue I would recommend going on Android Keystore following the example implementation in the second link above. 对于您当前的问题,我建议按照上面第二个链接中的示例实现继续使用Android Keystore。

Good luck ! 祝好运 !

We use a custom SharedPreference instance that encrypts the keys and values when adding, and decrypts when requesting. 我们使用自定义SharedPreference实例,在添加时加密键和值,并在请求时解密。

SecurePreferences preferences = ...
preferences.edit().putString( "key", "value" ).apply(); // key and value are encrypted automatically

String value = preferences.getString( "key", null ); // key and value are decrypted automatically

I would only recommend using SharedPreferences if the values are encrypted, because even though the xml file is only available to the app, it can be accessed on rooted devices. 如果值是加密的,我只建议使用SharedPreferences,因为即使xml文件仅对应用程序可用,也可以在root设备上访问它。

If you already using a SqlLiteDB, I would probably use that. 如果您已经使用了SqlLiteDB,我可能会使用它。 If not, it's bit heavy for just saving a token. 如果没有,仅仅保存令牌就有点沉重。

EDIT: 编辑:

An oauth token is completely unrelated to the key and keystore used to sign the app. oauth令牌与用于签署应用程序的密钥和密钥库完全无关。

The oauth token is a token provided by the server after validating the user's credentials, within the app. oauth令牌是服务器在验证用户凭据后在应用程序中提供的令牌。

The keystore contains 1 or more certificates that is used to digitally sign the app. 密钥库包含一个或多个用于对应用程序进行数字签名的证书。 This is to prevent someone else from uploading an app that has the same package name as yours and replacing it. 这是为了防止其他人上传与您的包名相同的应用并替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM