|
16 | 16 | import com.google.android.apps.forscience.whistlepunk.accounts.AppAccount; |
17 | 17 | import com.google.android.apps.forscience.whistlepunk.remote.StringUtils; |
18 | 18 |
|
| 19 | +import java.io.File; |
19 | 20 | import java.io.IOException; |
20 | 21 | import java.security.GeneralSecurityException; |
| 22 | +import java.security.KeyStore; |
21 | 23 |
|
22 | 24 | public class ArduinoAccountProvider extends AbstractAccountsProvider { |
23 | 25 |
|
24 | 26 | private static final String LOG_TAG = "ArduinoAccountProvider"; |
| 27 | + private static final String SHARED_PREFS_FILENAME = "ArduinoSharedPreferences"; |
25 | 28 |
|
26 | 29 | private ArduinoAccount arduinoAccount; |
27 | 30 |
|
28 | 31 | public ArduinoAccountProvider(final Context context) { |
29 | 32 | super(context); |
| 33 | + |
30 | 34 | final SharedPreferences prefs = getSharedPreferences(); |
31 | 35 | final String jsonToken = prefs.getString("token", null); |
32 | 36 | if (!StringUtils.isEmpty(jsonToken)) { |
@@ -100,27 +104,59 @@ public void showAddAccountDialog(Activity activity) { |
100 | 104 | public void showAccountSwitcherDialog(Fragment fragment, int requestCode) { |
101 | 105 | } |
102 | 106 |
|
103 | | - private SharedPreferences getSharedPreferences() { |
| 107 | + private SharedPreferences getBrandNewSharedPreferences() { |
104 | 108 | MasterKey masterKey = null; |
| 109 | + |
105 | 110 | try { |
| 111 | + File sharedPrefsFile = new File(applicationContext.getFilesDir().getParent() + "/shared_prefs/" + SHARED_PREFS_FILENAME + ".xml"); |
| 112 | + boolean deleted = sharedPrefsFile.delete(); |
| 113 | + |
| 114 | + Log.d(LOG_TAG, String.format("Shared prefs file deleted: %s", deleted)); |
| 115 | + |
| 116 | + // delete MasterKey |
| 117 | + KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); |
| 118 | + keyStore.load(null); |
| 119 | + keyStore.deleteEntry(MasterKey.DEFAULT_MASTER_KEY_ALIAS); |
| 120 | + |
| 121 | + // build MasterKey |
106 | 122 | masterKey = new MasterKey.Builder(applicationContext, MasterKey.DEFAULT_MASTER_KEY_ALIAS) |
107 | 123 | .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
108 | 124 | .build(); |
109 | 125 |
|
| 126 | + // create shared preferences |
110 | 127 | return EncryptedSharedPreferences.create( |
111 | 128 | applicationContext, |
112 | | - "ArduinoSharedPreferences", |
| 129 | + SHARED_PREFS_FILENAME, |
113 | 130 | masterKey, |
114 | 131 | EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
115 | 132 | EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
116 | 133 | ); |
117 | | - } catch (GeneralSecurityException e) { |
118 | | - Log.e(LOG_TAG, "Unable to retrieve encrypted shared preferences", e); |
119 | | - throw new RuntimeException("Unable to retrieve encrypted shared preferences", e); |
120 | | - } catch (IOException e) { |
| 134 | + } catch (GeneralSecurityException | IOException e) { |
121 | 135 | Log.e(LOG_TAG, "Unable to retrieve encrypted shared preferences", e); |
122 | 136 | throw new RuntimeException("Unable to retrieve encrypted shared preferences", e); |
123 | 137 | } |
124 | 138 | } |
125 | 139 |
|
| 140 | + private SharedPreferences getSharedPreferences() { |
| 141 | + MasterKey masterKey = null; |
| 142 | + try { |
| 143 | + // build MasterKey |
| 144 | + masterKey = new MasterKey.Builder(applicationContext, MasterKey.DEFAULT_MASTER_KEY_ALIAS) |
| 145 | + .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
| 146 | + .build(); |
| 147 | + |
| 148 | + // get or create shared preferences |
| 149 | + return EncryptedSharedPreferences.create( |
| 150 | + applicationContext, |
| 151 | + SHARED_PREFS_FILENAME, |
| 152 | + masterKey, |
| 153 | + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
| 154 | + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
| 155 | + ); |
| 156 | + } catch (GeneralSecurityException | IOException e) { |
| 157 | + Log.e(LOG_TAG, "Unable to retrieve encrypted shared preferences, regenerating master key.", e); |
| 158 | + return this.getBrandNewSharedPreferences(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
126 | 162 | } |
0 commit comments