From 99ce6ec18cd48f2724b413ceeaf534d3e2a91269 Mon Sep 17 00:00:00 2001 From: Jo Rabin Date: Fri, 25 Aug 2023 19:21:52 +0100 Subject: [PATCH] More tudying --- .../linguafranca/pwdb/kdbx/ChooseFile.java | 6 ++-- .../converter/StringToDateConverter.java | 19 +++++------- .../jackson/converter/ValueDeserializer.java | 30 +++++-------------- .../jackson/converter/ValueSerializer.java | 9 ++---- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/example/src/main/java/org/linguafranca/pwdb/kdbx/ChooseFile.java b/example/src/main/java/org/linguafranca/pwdb/kdbx/ChooseFile.java index 80cbf8ae..e0f4f266 100644 --- a/example/src/main/java/org/linguafranca/pwdb/kdbx/ChooseFile.java +++ b/example/src/main/java/org/linguafranca/pwdb/kdbx/ChooseFile.java @@ -67,14 +67,12 @@ public String getDescription() { return; } logger.info("Opening {}", fc.getSelectedFile().getPath()); -/* Util.listXml(fc.getSelectedFile().getName(), new KdbxCreds(s.getBytes()), new PrintWriter(outputStream)); -*/ - try (InputStream is = Files.newInputStream(Paths.get(fc.getSelectedFile().getPath()))) { +/* try (InputStream is = Files.newInputStream(Paths.get(fc.getSelectedFile().getPath()))) { HexViewer.list(is); - } + }*/ } } } diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/StringToDateConverter.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/StringToDateConverter.java index d334a5bb..c215f90e 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/StringToDateConverter.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/StringToDateConverter.java @@ -16,6 +16,7 @@ package org.linguafranca.pwdb.kdbx.jackson.converter; import java.util.Date; +import java.util.Objects; import org.linguafranca.pwdb.kdbx.Helpers; @@ -25,17 +26,13 @@ public class StringToDateConverter extends StdConverter { @Override public Date convert(String value) { - Date result = null; - if(value != null) { - if(value.equals("${creationDate}")) { - result = new Date(); - } - try { - result = Helpers.toDate(value); - } catch(Exception e) { - result = new Date(); - } + // TODO: It would really be better if we could inhibit deserialize date elements that are not present + if (Objects.isNull(value) || value.isEmpty()) { + return null; } - return result; + if(value.equals("${creationDate}")) { + return new Date(); + } + return Helpers.toDate(value); } } diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueDeserializer.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueDeserializer.java index 8f7fda9b..926aaa8a 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueDeserializer.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueDeserializer.java @@ -31,15 +31,7 @@ public class ValueDeserializer extends StdDeserializer { - private StreamEncryptor encryptor; - - public ValueDeserializer() { - super(ValueDeserializer.class); - } - - public ValueDeserializer(Class v) { - super(v); - } + private final StreamEncryptor encryptor; public ValueDeserializer(StreamEncryptor encryptor) { super(ValueDeserializer.class); @@ -47,8 +39,7 @@ public ValueDeserializer(StreamEncryptor encryptor) { } @Override - public EntryClasses.StringProperty.Value deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { - + public EntryClasses.StringProperty.Value deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonNode node = p.getCodec().readTree(p); @@ -56,14 +47,12 @@ public EntryClasses.StringProperty.Value deserialize(JsonParser p, Deserializati if(node.isTextual()) { result.setText(node.textValue()); - } else if(node.isObject()) { - + return result; + } + if(node.isObject()) { + // TODO not clear what is happening here, looks like it's not exactly correct //We need to decrypt all Protected values - if(node.has("Protected")) { - - //Check if Protected=True - Boolean nodeEncrypted = Helpers.toBoolean(node.get("Protected").asText()); - if(nodeEncrypted) { + if(node.has("Protected") && Boolean.TRUE.equals(Helpers.toBoolean(node.get("Protected").asText()))) { if(node.has("")) { String cipherText = node.get("").asText(); if(cipherText != null && !cipherText.isEmpty()) { @@ -75,7 +64,6 @@ public EntryClasses.StringProperty.Value deserialize(JsonParser p, Deserializati result.setProtectOnOutput(true); } } - } } else { //If an element is not marked us Protected we need to copy the value as is if(node.has("ProtectInMemory")) { @@ -88,8 +76,6 @@ public EntryClasses.StringProperty.Value deserialize(JsonParser p, Deserializati } } } - return result; } - -} +} \ No newline at end of file diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueSerializer.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueSerializer.java index 4d0207d2..1e020390 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueSerializer.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/converter/ValueSerializer.java @@ -48,13 +48,13 @@ public void serialize(Value value, JsonGenerator gen, SerializerProvider provide //We need to encrypt and convert to base64 every protected element if (value.getProtectOnOutput()) { xmlGenerator.setNextIsAttribute(true); - gen.writeStringField("Protected", "True"); + xmlGenerator.writeStringField("Protected", "True"); String plain = value.getText(); if(plain == null) { plain = ""; } //Cipher - byte[] encrypted = encryptor.encrypt(plain.getBytes()); + byte[] encrypted = encryptor.encrypt(plain.getBytes()); //Convert to base64 stringToWrite = new String(Base64.encodeBase64(encrypted)); } @@ -62,9 +62,6 @@ public void serialize(Value value, JsonGenerator gen, SerializerProvider provide xmlGenerator.setNextIsAttribute(false); xmlGenerator.setNextIsUnwrapped(true); xmlGenerator.writeStringField("text", stringToWrite); - - gen.writeEndObject(); - + xmlGenerator.writeEndObject(); } - }