Search in sources :

Example 6 with SharedPrefsBackedKeyChain

use of com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain in project LabDayApp by jakdor.

the class ProjectRepository method saveAccessToken.

/**
 * Saves access token encrypted with Conceal
 * @param token access token retrieved after successful login
 * @param context required for SharedPreferences/Conceal
 */
public void saveAccessToken(String token, Context context) {
    if (token.equals("-1")) {
        this.accessToken = token;
        Timber.e("bad access token");
        return;
    }
    KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    try {
        byte[] tokenBytes = token.getBytes(Charset.forName("UTF-8"));
        byte[] encryptedToken = crypto.encrypt(tokenBytes, Entity.create("token"));
        FileOutputStream outputStream = context.openFileOutput("lab", Context.MODE_PRIVATE);
        outputStream.write(encryptedToken);
        outputStream.close();
        Timber.i("Successful login, access token saved");
    } catch (Exception e) {
        Timber.wtf("unable to save access token");
    }
    this.accessToken = token;
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) FileOutputStream(java.io.FileOutputStream) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)

Aggregations

SharedPrefsBackedKeyChain (com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)6 KeyChain (com.facebook.crypto.keychain.KeyChain)5 Crypto (com.facebook.crypto.Crypto)3 Context (android.content.Context)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 CryptoManagerImpl (com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl)1 Provides (dagger.Provides)1 FileOutputStream (java.io.FileOutputStream)1 Singleton (javax.inject.Singleton)1 Before (org.junit.Before)1