From 513840e2c4c7ecd612e42caa5a8817e2beaa4cae Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 23 Jul 2018 23:59:11 -0400 Subject: [PATCH] Read in site questions from json & don't serialize incomplete MPFileUser --- .../masterpassword/model/impl/MPFileUser.java | 12 +++++++++++- .../model/impl/MPFlatMarshaller.java | 3 +++ .../model/impl/MPFlatUnmarshaller.java | 1 + .../masterpassword/model/impl/MPJSONFile.java | 18 ++++++++++++++++-- .../model/impl/MPJSONMarshaller.java | 3 +++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java index 9b40aefc..fd826e99 100755 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java @@ -42,6 +42,7 @@ public class MPFileUser extends MPBasicUser { private MPResultType defaultType; private ReadableInstant lastUsed; + private boolean complete; public MPFileUser(final String fullName) { this( fullName, null, MPAlgorithm.Version.CURRENT.getAlgorithm() ); @@ -131,6 +132,14 @@ public class MPFileUser extends MPBasicUser { setChanged(); } + protected boolean isComplete() { + return complete; + } + + protected void setComplete() { + complete = true; + } + public File getFile() { return new File( path, getFullName() + getFormat().fileSuffix() ); } @@ -157,7 +166,8 @@ public class MPFileUser extends MPBasicUser { @Override protected void onChanged() { try { - getFormat().marshaller().marshall( this ); + if (isComplete()) + getFormat().marshaller().marshall( this ); } catch (final MPKeyUnavailableException e) { logger.wrn( e, "Cannot write out changes for unauthenticated user: %s.", this ); diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java index 552c5590..3e5265af 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java @@ -41,6 +41,9 @@ public class MPFlatMarshaller implements MPMarshaller { @Override public void marshall(final MPFileUser user) throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException { + if (!user.isComplete()) + throw new IllegalStateException( "Cannot marshall an incomplete user: " + user ); + StringBuilder content = new StringBuilder(); content.append( "# Master Password site export\n" ); content.append( "# " ).append( user.getContentMode().description() ).append( '\n' ); diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java index 6aa8be48..976f5858 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java @@ -188,6 +188,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { throw new MPMarshalException( "No full header found in import file." ); } + user.setComplete(); user.endChanges(); } } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java index ecc7a207..acbea3e4 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java @@ -155,8 +155,8 @@ public class MPJSONFile extends MPJSONAnyObject { String siteName = siteEntry.getKey(); Site fileSite = siteEntry.getValue(); MPFileSite site = new MPFileSite( - user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ), fileSite.type, - export.redacted? fileSite.password: null, + user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ), + fileSite.type, export.redacted? fileSite.password: null, fileSite.login_type, export.redacted? fileSite.login_name: null, (fileSite._ext_mpw != null)? fileSite._ext_mpw.url: null, fileSite.uses, (fileSite.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( fileSite.last_used ): new Instant() ); @@ -169,9 +169,23 @@ public class MPJSONFile extends MPJSONAnyObject { fileSite.login_name ); } + if (fileSite.questions != null) + for (final Map.Entry questionEntry : fileSite.questions.entrySet()) { + Site.Question fileQuestion = questionEntry.getValue(); + MPFileQuestion question = new MPFileQuestion( site, questionEntry.getKey(), + fileQuestion.type, export.redacted? fileQuestion.answer: null ); + + if (!export.redacted && (fileQuestion.answer != null)) + question.setAnswer( (fileQuestion.type != null)? fileQuestion.type: MPResultType.StoredPersonal, + fileQuestion.answer ); + + site.addQuestion( question ); + } + user.addSite( site ); } + user.setComplete(); user.endChanges(); } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java index 2e39fc7d..0b5dd2bd 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java @@ -37,6 +37,9 @@ public class MPJSONMarshaller implements MPMarshaller { public void marshall(final MPFileUser user) throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException { + if (!user.isComplete()) + throw new IllegalStateException( "Cannot marshall an incomplete user: " + user ); + try { objectMapper.writerWithDefaultPrettyPrinter().writeValue( user.getFile(), new MPJSONFile( user ) ); }