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