2019-12-25 19:21:34 +01:00
|
|
|
package com.beemdevelopment.aegis.vault;
|
2019-02-07 22:39:33 +01:00
|
|
|
|
|
|
|
import com.beemdevelopment.aegis.encoding.Base64Exception;
|
|
|
|
import com.beemdevelopment.aegis.otp.OtpInfoException;
|
Introduce UUIDMap for storing objects that are keyed by a UUID
This patch introduces the new ``UUIDMap`` type, reducing code duplication and
making UUID lookups faster. We currently already use UUIDs as the identifier for
the ``DatabaseEntry`` and ``Slot`` types, but the way lookups by UUID work are
kind of ugly, as we simply iterate over the list until we find a match. As we're
probably going to have more types like this soon (groups and icons, for
example), I figured it'd be good to abstract this away into a separate type and
make it a map instead of a list.
The only thing that has gotten slower is the ``swap`` method. The internal
``LinkedHashMap`` retains insertion order with a linked list, but does not know
about the position of the values, so we basically have to copy the entire map to
simply swap two values. I don't think it's too big of a deal, because swap
operations still take less than a millisecond even with large vaults, but
suggestions for improving this are welcome.
I had to update gradle and JUnit to be able to use the new ``assertThrows``
assertion method, so this patch includes that as well.
2019-06-10 18:25:44 +02:00
|
|
|
import com.beemdevelopment.aegis.util.UUIDMap;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
import org.json.JSONArray;
|
2018-03-19 18:00:53 +01:00
|
|
|
import org.json.JSONException;
|
2016-11-13 18:21:00 +01:00
|
|
|
import org.json.JSONObject;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2019-12-25 19:21:34 +01:00
|
|
|
public class Vault {
|
2017-08-26 21:15:53 +02:00
|
|
|
private static final int VERSION = 1;
|
2019-12-25 19:21:34 +01:00
|
|
|
private UUIDMap<VaultEntry> _entries = new UUIDMap<>();
|
2016-11-13 18:21:00 +01:00
|
|
|
|
2018-10-06 22:23:38 +02:00
|
|
|
public JSONObject toJson() {
|
2018-03-19 18:00:53 +01:00
|
|
|
try {
|
|
|
|
JSONArray array = new JSONArray();
|
2019-12-25 19:21:34 +01:00
|
|
|
for (VaultEntry e : _entries) {
|
2018-10-06 22:23:38 +02:00
|
|
|
array.put(e.toJson());
|
2018-03-19 18:00:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
obj.put("version", VERSION);
|
|
|
|
obj.put("entries", array);
|
|
|
|
return obj;
|
|
|
|
} catch (JSONException e) {
|
2018-06-06 16:15:31 +02:00
|
|
|
throw new RuntimeException(e);
|
2016-11-13 18:21:00 +01:00
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 19:21:34 +01:00
|
|
|
public static Vault fromJson(JSONObject obj) throws VaultException {
|
|
|
|
Vault vault = new Vault();
|
|
|
|
UUIDMap<VaultEntry> entries = vault.getEntries();
|
2018-10-06 22:23:38 +02:00
|
|
|
|
2018-03-19 18:00:53 +01:00
|
|
|
try {
|
|
|
|
int ver = obj.getInt("version");
|
|
|
|
if (ver != VERSION) {
|
2019-12-25 19:21:34 +01:00
|
|
|
throw new VaultException("Unsupported version");
|
2018-03-19 18:00:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONArray array = obj.getJSONArray("entries");
|
|
|
|
for (int i = 0; i < array.length(); i++) {
|
2019-12-25 19:21:34 +01:00
|
|
|
VaultEntry entry = VaultEntry.fromJson(array.getJSONObject(i));
|
Introduce UUIDMap for storing objects that are keyed by a UUID
This patch introduces the new ``UUIDMap`` type, reducing code duplication and
making UUID lookups faster. We currently already use UUIDs as the identifier for
the ``DatabaseEntry`` and ``Slot`` types, but the way lookups by UUID work are
kind of ugly, as we simply iterate over the list until we find a match. As we're
probably going to have more types like this soon (groups and icons, for
example), I figured it'd be good to abstract this away into a separate type and
make it a map instead of a list.
The only thing that has gotten slower is the ``swap`` method. The internal
``LinkedHashMap`` retains insertion order with a linked list, but does not know
about the position of the values, so we basically have to copy the entire map to
simply swap two values. I don't think it's too big of a deal, because swap
operations still take less than a millisecond even with large vaults, but
suggestions for improving this are welcome.
I had to update gradle and JUnit to be able to use the new ``assertThrows``
assertion method, so this patch includes that as well.
2019-06-10 18:25:44 +02:00
|
|
|
entries.add(entry);
|
2018-03-19 18:00:53 +01:00
|
|
|
}
|
2018-06-07 12:27:42 +02:00
|
|
|
} catch (Base64Exception | OtpInfoException | JSONException e) {
|
2019-12-25 19:21:34 +01:00
|
|
|
throw new VaultException(e);
|
2016-11-13 18:21:00 +01:00
|
|
|
}
|
2018-10-06 22:23:38 +02:00
|
|
|
|
2019-12-25 19:21:34 +01:00
|
|
|
return vault;
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 19:21:34 +01:00
|
|
|
public UUIDMap<VaultEntry> getEntries() {
|
Introduce UUIDMap for storing objects that are keyed by a UUID
This patch introduces the new ``UUIDMap`` type, reducing code duplication and
making UUID lookups faster. We currently already use UUIDs as the identifier for
the ``DatabaseEntry`` and ``Slot`` types, but the way lookups by UUID work are
kind of ugly, as we simply iterate over the list until we find a match. As we're
probably going to have more types like this soon (groups and icons, for
example), I figured it'd be good to abstract this away into a separate type and
make it a map instead of a list.
The only thing that has gotten slower is the ``swap`` method. The internal
``LinkedHashMap`` retains insertion order with a linked list, but does not know
about the position of the values, so we basically have to copy the entire map to
simply swap two values. I don't think it's too big of a deal, because swap
operations still take less than a millisecond even with large vaults, but
suggestions for improving this are welcome.
I had to update gradle and JUnit to be able to use the new ``assertThrows``
assertion method, so this patch includes that as well.
2019-06-10 18:25:44 +02:00
|
|
|
return _entries;
|
2018-09-22 14:12:42 +02:00
|
|
|
}
|
2017-05-03 21:08:38 +02:00
|
|
|
}
|