How to secure token in Android ?

Dew
2 min readOct 6, 2023

--

Storing access tokens or any other tokens securely in an Android app is critical to protect user data and prevent unauthorized access to sensitive resources. There are several options for securely storing tokens in Android apps, each with its own advantages and use cases. The choice of where to store tokens depends on factors like the token’s lifespan and sensitivity. Here are some common storage options:

  1. SharedPreferences: It is a simple and convenient way to store small pieces of data, such as tokens. However, it’s not the most secure option, especially for sensitive tokens like access tokens. Use SharedPreferences for non-sensitive data or when you don’t have critical security requirements.
// Storing an access token in SharedPreferences
val sharedPreferences = context.getSharedPreferences("MyAppPrefs", Context.MODE_PRIVATE)
sharedPreferences.edit().putString("access_token", accessToken).apply()

2. Internal Storage: You can write tokens to internal storage as files. This provides better security than SharedPreferences but is still accessible to anyone with root access to the device. Consider encrypting the token data before writing it to internal storage for an additional layer of security.

// Storing an access token in internal storage
val fileOutputStream = context.openFileOutput("access_token.txt", Context.MODE_PRIVATE)
fileOutputStream.write(accessToken.toByteArray())
fileOutputStream.close()

3. External Storage (Not Recommended): Avoid storing tokens in external storage as it’s less secure and exposes tokens to potential tampering by other apps or malicious actors.

4. Android Keystore: Android Keystore provides a secure storage option for cryptographic keys, including keys used for encrypting tokens. You can encrypt the token and store it in the Keystore for enhanced security.

val keystore = KeyStore.getInstance("AndroidKeyStore")
keystore.load(null)
val secretKeyEntry = keystore.getEntry("my_access_token", null) as KeyStore.SecretKeyEntry
val encryptedToken = secretKeyEntry.secretKey

5. Custom Encrypted Database: For long-lived tokens or scenarios where additional security is required, consider storing tokens in a custom encrypted database using libraries like SQLCipher.

6. OAuth 2.0 Token Stores: If you’re working with OAuth 2.0, consider using libraries and frameworks like AppAuth or Okta’s OIDC Android library, which provide built-in token storage mechanisms that are compliant with OAuth 2.0 security best practices.

7. Token Rotation and Refresh Tokens: Use refresh tokens for long-lived sessions and implement token rotation to periodically refresh access tokens without requiring the user to re-enter their credentials.

8. Biometric Authentication (Fingerprint/Face Recognition): For additional security, you can require biometric authentication (e.g., fingerprint or face recognition) before retrieving and using sensitive tokens.

In summary, the choice of where to store access tokens or any other tokens in your Android app depends on your specific security requirements and the sensitivity of the tokens. For sensitive tokens, consider using Android Keystore, encrypted databases, or OAuth 2.0-compliant token storage mechanisms provided by OAuth libraries. Always follow security best practices and guidelines to protect user data.

Thanks for reading.. HAPPY CODING!!

--

--

Dew

Passionate Android developer with a deep interest in crafting elegant and efficient mobile applications. https://letmedo.in/