flush credential store file

This commit is contained in:
Daniel Gultsch 2023-01-19 15:29:47 +01:00
parent 944c48e00b
commit 27952c00ed
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -173,7 +173,7 @@ public class CredentialStore {
final Map<String, Credential> store;
try {
store = load();
} catch (final GeneralSecurityException | IOException e) {
} catch (final Exception e) {
return ImmutableMap.of();
}
return store == null ? ImmutableMap.of() : store;
@ -188,19 +188,31 @@ public class CredentialStore {
private void store(final Map<String, Credential> store)
throws GeneralSecurityException, IOException {
final EncryptedFile encryptedFile = getEncryptedFile();
final FileOutputStream outputStream = encryptedFile.openFileOutput();
final File file = getCredentialStoreFile();
file.delete();
final EncryptedFile encryptedFile = getEncryptedFile(file);
try (final FileOutputStream outputStream = encryptedFile.openFileOutput()) {
GSON.toJson(store, new OutputStreamWriter(outputStream));
outputStream.flush();
}
}
private EncryptedFile getEncryptedFile() throws GeneralSecurityException, IOException {
return getEncryptedFile(getCredentialStoreFile());
}
private EncryptedFile getEncryptedFile(final File file) throws GeneralSecurityException, IOException {
final KeyGenParameterSpec keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC;
final String mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec);
return new EncryptedFile.Builder(
new File(context.getFilesDir(), FILENAME),
file,
context,
mainKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB)
.build();
}
private File getCredentialStoreFile() {
return new File(context.getFilesDir(), FILENAME);
}
}