Initial Java JSON serialization/deserialization.
This commit is contained in:
@@ -18,10 +18,14 @@
|
||||
|
||||
package com.lyndir.masterpassword.model;
|
||||
|
||||
import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*;
|
||||
|
||||
import com.google.common.primitives.UnsignedInteger;
|
||||
import com.lyndir.masterpassword.*;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.Instant;
|
||||
import org.joda.time.ReadableInstant;
|
||||
|
||||
|
||||
/**
|
||||
@@ -29,22 +33,23 @@ import org.joda.time.Instant;
|
||||
*/
|
||||
public class MPFileSite extends MPSite {
|
||||
|
||||
private final MPFileUser user;
|
||||
private String siteName;
|
||||
private final MPFileUser user;
|
||||
|
||||
private String siteName;
|
||||
@Nullable
|
||||
private String siteContent;
|
||||
private UnsignedInteger siteCounter;
|
||||
private MPResultType resultType;
|
||||
private MPAlgorithm algorithm;
|
||||
private String siteState;
|
||||
private UnsignedInteger siteCounter;
|
||||
private MPResultType resultType;
|
||||
private MPAlgorithm algorithm;
|
||||
|
||||
@Nullable
|
||||
private String loginContent;
|
||||
private String loginState;
|
||||
private MPResultType loginType;
|
||||
|
||||
@Nullable
|
||||
private String url;
|
||||
private int uses;
|
||||
private Instant lastUsed;
|
||||
private String url;
|
||||
private int uses;
|
||||
private ReadableInstant lastUsed;
|
||||
|
||||
public MPFileSite(final MPFileUser user, final String siteName) {
|
||||
this( user, siteName, null, null, user.getAlgorithm() );
|
||||
@@ -56,45 +61,43 @@ public class MPFileSite extends MPSite {
|
||||
null, null, null, 0, new Instant() );
|
||||
}
|
||||
|
||||
protected MPFileSite(final MPFileUser user, final String siteName, @Nullable final String siteContent,
|
||||
protected MPFileSite(final MPFileUser user, final String siteName, @Nullable final String siteState,
|
||||
@Nullable final UnsignedInteger siteCounter, @Nullable final MPResultType resultType, final MPAlgorithm algorithm,
|
||||
@Nullable final String loginContent, @Nullable final MPResultType loginType,
|
||||
@Nullable final String url, final int uses, final Instant lastUsed) {
|
||||
@Nullable final String loginState, @Nullable final MPResultType loginType,
|
||||
@Nullable final String url, final int uses, final ReadableInstant lastUsed) {
|
||||
this.user = user;
|
||||
this.siteName = siteName;
|
||||
this.siteContent = siteContent;
|
||||
this.siteCounter = (siteCounter == null)? user.getAlgorithm().mpw_default_counter(): siteCounter;
|
||||
this.resultType = (resultType == null)? user.getAlgorithm().mpw_default_type(): resultType;
|
||||
this.siteState = siteState;
|
||||
this.siteCounter = ifNotNullElse( siteCounter, user.getAlgorithm().mpw_default_counter() );
|
||||
this.resultType = ifNotNullElse( resultType, user.getAlgorithm().mpw_default_password_type() );
|
||||
this.algorithm = algorithm;
|
||||
this.loginContent = loginContent;
|
||||
this.loginType = (loginType == null)? MPResultType.GeneratedName: loginType;
|
||||
this.loginState = loginState;
|
||||
this.loginType = ifNotNullElse( loginType, getAlgorithm().mpw_default_login_type() );
|
||||
this.url = url;
|
||||
this.uses = uses;
|
||||
this.lastUsed = lastUsed;
|
||||
}
|
||||
|
||||
public String resultFor(final MPMasterKey masterKey)
|
||||
throws MPInvalidatedException {
|
||||
public String getResult()
|
||||
throws MPKeyUnavailableException {
|
||||
|
||||
return resultFor( masterKey, MPKeyPurpose.Authentication, null );
|
||||
return getResult( MPKeyPurpose.Authentication, null );
|
||||
}
|
||||
|
||||
public String resultFor(final MPMasterKey masterKey, final MPKeyPurpose keyPurpose, @Nullable final String keyContext)
|
||||
throws MPInvalidatedException {
|
||||
public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext)
|
||||
throws MPKeyUnavailableException {
|
||||
|
||||
return resultFor( masterKey, keyPurpose, keyContext, getSiteContent() );
|
||||
return getResult( keyPurpose, keyContext, siteState );
|
||||
}
|
||||
|
||||
public String loginFor(final MPMasterKey masterKey)
|
||||
throws MPInvalidatedException {
|
||||
public String getLogin()
|
||||
throws MPKeyUnavailableException {
|
||||
|
||||
if (loginType == null)
|
||||
loginType = MPResultType.GeneratedName;
|
||||
|
||||
return loginFor( masterKey, loginType, loginContent );
|
||||
return getLogin( loginState );
|
||||
}
|
||||
|
||||
public MPFileUser getUser() {
|
||||
@Override
|
||||
public MPUser<?> getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -109,18 +112,18 @@ public class MPFileSite extends MPSite {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getSiteContent() {
|
||||
return siteContent;
|
||||
public String getSiteState() {
|
||||
return siteState;
|
||||
}
|
||||
|
||||
public void setSitePassword(final MPMasterKey masterKey, final MPResultType resultType, @Nullable final String result)
|
||||
throws MPInvalidatedException {
|
||||
public void setSitePassword(final MPResultType resultType, @Nullable final String result)
|
||||
throws MPKeyUnavailableException {
|
||||
this.resultType = resultType;
|
||||
|
||||
if (result == null)
|
||||
this.siteContent = null;
|
||||
this.siteState = null;
|
||||
else
|
||||
this.siteContent = masterKey.siteState(
|
||||
this.siteState = user.getMasterKey().siteState(
|
||||
siteName, siteCounter, MPKeyPurpose.Authentication, null, resultType, result, algorithm );
|
||||
}
|
||||
|
||||
@@ -144,6 +147,17 @@ public class MPFileSite extends MPSite {
|
||||
this.resultType = resultType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPResultType getLoginType() {
|
||||
return loginType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoginType(@Nullable final MPResultType loginType) {
|
||||
this.loginType = ifNotNullElse( loginType, getAlgorithm().mpw_default_login_type() );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPAlgorithm getAlgorithm() {
|
||||
return algorithm;
|
||||
@@ -154,25 +168,17 @@ public class MPFileSite extends MPSite {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
public MPResultType getLoginType() {
|
||||
return loginType;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getLoginContent() {
|
||||
return loginContent;
|
||||
public String getLoginState() {
|
||||
return loginState;
|
||||
}
|
||||
|
||||
public void setLoginName(final MPMasterKey masterKey, @Nullable final MPResultType loginType, @Nullable final String result)
|
||||
throws MPInvalidatedException {
|
||||
public void setLoginName(@Nonnull final MPResultType loginType, @Nonnull final String loginName)
|
||||
throws MPKeyUnavailableException {
|
||||
this.loginType = loginType;
|
||||
if (this.loginType != null)
|
||||
if (result == null)
|
||||
this.loginContent = null;
|
||||
else
|
||||
this.loginContent = masterKey.siteState(
|
||||
siteName, algorithm.mpw_default_counter(), MPKeyPurpose.Identification, null, this.loginType, result,
|
||||
algorithm );
|
||||
this.loginState = user.getMasterKey().siteState(
|
||||
siteName, algorithm.mpw_default_counter(), MPKeyPurpose.Identification, null,
|
||||
this.loginType, loginName, algorithm );
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -188,7 +194,7 @@ public class MPFileSite extends MPSite {
|
||||
return uses;
|
||||
}
|
||||
|
||||
public Instant getLastUsed() {
|
||||
public ReadableInstant getLastUsed() {
|
||||
return lastUsed;
|
||||
}
|
||||
|
||||
|
@@ -42,9 +42,10 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
private final Collection<MPFileSite> sites = Sets.newHashSet();
|
||||
|
||||
@Nullable
|
||||
private byte[] keyID;
|
||||
private MPAlgorithm algorithm;
|
||||
private MPMarshalFormat format;
|
||||
private byte[] keyID;
|
||||
private MPAlgorithm algorithm;
|
||||
private MPMarshalFormat format;
|
||||
private MPMarshaller.ContentMode contentMode;
|
||||
|
||||
private int avatar;
|
||||
private MPResultType defaultType;
|
||||
@@ -55,11 +56,13 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
}
|
||||
|
||||
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPAlgorithm algorithm) {
|
||||
this( fullName, keyID, algorithm, 0, algorithm.mpw_default_type(), new Instant(), MPMarshalFormat.DEFAULT );
|
||||
this( fullName, keyID, algorithm, 0, algorithm.mpw_default_password_type(), new Instant(),
|
||||
MPMarshalFormat.DEFAULT, MPMarshaller.ContentMode.PROTECTED );
|
||||
}
|
||||
|
||||
public MPFileUser(final String fullName, @Nullable final byte[] keyID, final MPAlgorithm algorithm, final int avatar,
|
||||
final MPResultType defaultType, final ReadableInstant lastUsed, final MPMarshalFormat format) {
|
||||
final MPResultType defaultType, final ReadableInstant lastUsed,
|
||||
final MPMarshalFormat format, final MPMarshaller.ContentMode contentMode) {
|
||||
this.fullName = fullName;
|
||||
this.keyID = (keyID == null)? null: keyID.clone();
|
||||
this.algorithm = algorithm;
|
||||
@@ -67,6 +70,7 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
this.defaultType = defaultType;
|
||||
this.lastUsed = lastUsed;
|
||||
this.format = format;
|
||||
this.contentMode = contentMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,6 +78,11 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
return fullName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public byte[] getKeyID() {
|
||||
return (keyID == null)? null: keyID.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPAlgorithm getAlgorithm() {
|
||||
return algorithm;
|
||||
@@ -91,6 +100,14 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public MPMarshaller.ContentMode getContentMode() {
|
||||
return contentMode;
|
||||
}
|
||||
|
||||
public void setContentMode(final MPMarshaller.ContentMode contentMode) {
|
||||
this.contentMode = contentMode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvatar() {
|
||||
return avatar;
|
||||
@@ -164,13 +181,13 @@ public class MPFileUser extends MPUser<MPFileSite> implements Comparable<MPFileU
|
||||
|
||||
return key;
|
||||
}
|
||||
catch (final MPInvalidatedException e) {
|
||||
catch (final MPKeyUnavailableException e) {
|
||||
throw logger.bug( e );
|
||||
}
|
||||
}
|
||||
|
||||
void save()
|
||||
throws MPInvalidatedException {
|
||||
throws MPKeyUnavailableException {
|
||||
MPFileUserManager.get().save( this, getMasterKey() );
|
||||
}
|
||||
|
||||
|
@@ -78,7 +78,7 @@ public class MPFileUserManager extends MPUserManager {
|
||||
for (final MPMarshalFormat format : MPMarshalFormat.values())
|
||||
if (userFile.getName().endsWith( format.fileSuffix() ))
|
||||
try {
|
||||
MPFileUser user = format.unmarshaller().unmarshall( userFile );
|
||||
MPFileUser user = format.unmarshaller().unmarshall( userFile, null );
|
||||
MPFileUser previousUser = users.put( user.getFullName(), user );
|
||||
if ((previousUser != null) && (previousUser.getFormat().ordinal() > user.getFormat().ordinal()))
|
||||
users.put( previousUser.getFullName(), previousUser );
|
||||
@@ -86,6 +86,9 @@ public class MPFileUserManager extends MPUserManager {
|
||||
catch (final IOException | MPMarshalException e) {
|
||||
logger.err( e, "Couldn't read user from: %s", userFile );
|
||||
}
|
||||
catch (final MPKeyUnavailableException | MPIncorrectMasterPasswordException e) {
|
||||
logger.err( e, "Couldn't authenticate user for: %s", userFile );
|
||||
}
|
||||
|
||||
return users.values();
|
||||
}
|
||||
@@ -117,7 +120,7 @@ public class MPFileUserManager extends MPUserManager {
|
||||
* Write the current user state to disk.
|
||||
*/
|
||||
public void save(final MPFileUser user, final MPMasterKey masterKey)
|
||||
throws MPInvalidatedException {
|
||||
throws MPKeyUnavailableException {
|
||||
try {
|
||||
final MPMarshalFormat format = user.getFormat();
|
||||
new CharSink() {
|
||||
@@ -126,7 +129,7 @@ public class MPFileUserManager extends MPUserManager {
|
||||
throws IOException {
|
||||
return new OutputStreamWriter( new FileOutputStream( getUserFile( user, format ) ), Charsets.UTF_8 );
|
||||
}
|
||||
}.write( format.marshaller().marshall( user, masterKey, MPMarshaller.ContentMode.PROTECTED ) );
|
||||
}.write( format.marshaller().marshall( user ) );
|
||||
}
|
||||
catch (final MPMarshalException | IOException e) {
|
||||
logger.err( e, "Unable to save sites for user: %s", user );
|
||||
|
@@ -36,11 +36,11 @@ public class MPFlatMarshaller implements MPMarshaller {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String marshall(final MPFileUser user, final MPMasterKey masterKey, final ContentMode contentMode)
|
||||
throws MPInvalidatedException, MPMarshalException {
|
||||
public String marshall(final MPFileUser user)
|
||||
throws MPKeyUnavailableException, MPMarshalException {
|
||||
StringBuilder content = new StringBuilder();
|
||||
content.append( "# Master Password site export\n" );
|
||||
content.append( "# " ).append( contentMode.description() ).append( '\n' );
|
||||
content.append( "# " ).append( user.getContentMode().description() ).append( '\n' );
|
||||
content.append( "# \n" );
|
||||
content.append( "##\n" );
|
||||
content.append( "# Format: " ).append( FORMAT ).append( '\n' );
|
||||
@@ -51,18 +51,18 @@ public class MPFlatMarshaller implements MPMarshaller {
|
||||
content.append( "# Key ID: " ).append( user.exportKeyID() ).append( '\n' );
|
||||
content.append( "# Algorithm: " ).append( user.getAlgorithm().version().toInt() ).append( '\n' );
|
||||
content.append( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' );
|
||||
content.append( "# Passwords: " ).append( contentMode.name() ).append( '\n' );
|
||||
content.append( "# Passwords: " ).append( user.getContentMode().name() ).append( '\n' );
|
||||
content.append( "##\n" );
|
||||
content.append( "#\n" );
|
||||
content.append( "# Last Times Password Login\t Site\tSite\n" );
|
||||
content.append( "# used used type name\t name\tpassword\n" );
|
||||
|
||||
for (final MPFileSite site : user.getSites()) {
|
||||
String loginName = site.getLoginContent();
|
||||
String password = site.getSiteContent();
|
||||
if (!contentMode.isRedacted()) {
|
||||
loginName = site.loginFor( masterKey );
|
||||
password = site.resultFor( masterKey );
|
||||
String loginName = site.getLoginState();
|
||||
String password = site.getSiteState();
|
||||
if (!user.getContentMode().isRedacted()) {
|
||||
loginName = site.getLogin();
|
||||
password = site.getResult();
|
||||
}
|
||||
|
||||
content.append( strf( "%s %8d %8s %25s\t%25s\t%s\n", //
|
||||
|
@@ -29,6 +29,7 @@ import java.io.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
|
||||
@@ -46,17 +47,17 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MPFileUser unmarshall(@Nonnull final File file)
|
||||
throws IOException, MPMarshalException {
|
||||
public MPFileUser unmarshall(@Nonnull final File file, @Nullable final char[] masterPassword)
|
||||
throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException {
|
||||
try (Reader reader = new InputStreamReader( new FileInputStream( file ), Charsets.UTF_8 )) {
|
||||
return unmarshall( CharStreams.toString( reader ) );
|
||||
return unmarshall( CharStreams.toString( reader ), masterPassword );
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MPFileUser unmarshall(@Nonnull final String content)
|
||||
throws MPMarshalException {
|
||||
public MPFileUser unmarshall(@Nonnull final String content, @Nullable final char[] masterPassword)
|
||||
throws MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException {
|
||||
MPFileUser user = null;
|
||||
byte[] keyID = null;
|
||||
String fullName = null;
|
||||
@@ -74,7 +75,8 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
|
||||
else
|
||||
// Ends the header.
|
||||
user = new MPFileUser( fullName, keyID, MPMasterKey.Version.fromInt( mpVersion ).getAlgorithm(),
|
||||
avatar, defaultType, new DateTime( 0 ), MPMarshalFormat.Flat );
|
||||
avatar, defaultType, new DateTime( 0 ), MPMarshalFormat.Flat,
|
||||
clearContent? MPMarshaller.ContentMode.VISIBLE : MPMarshaller.ContentMode.PROTECTED );
|
||||
|
||||
// Comment.
|
||||
else if (line.startsWith( "#" )) {
|
||||
@@ -113,25 +115,31 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
|
||||
switch (importFormat) {
|
||||
case 0:
|
||||
site = new MPFileSite( user, //
|
||||
siteMatcher.group( 5 ), siteMatcher.group( 6 ),
|
||||
siteMatcher.group( 5 ), clearContent? null: siteMatcher.group( 6 ),
|
||||
user.getAlgorithm().mpw_default_counter(),
|
||||
MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
|
||||
MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN(
|
||||
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(),
|
||||
null, null, null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ),
|
||||
MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() );
|
||||
if (clearContent)
|
||||
site.setSitePassword( site.getResultType(), siteMatcher.group( 6 ) );
|
||||
break;
|
||||
|
||||
case 1:
|
||||
site = new MPFileSite( user, //
|
||||
siteMatcher.group( 7 ), siteMatcher.group( 8 ),
|
||||
siteMatcher.group( 7 ), clearContent? null: siteMatcher.group( 8 ),
|
||||
UnsignedInteger.valueOf( colon.matcher( siteMatcher.group( 5 ) ).replaceAll( "" ) ),
|
||||
MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ),
|
||||
MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN(
|
||||
colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(),
|
||||
siteMatcher.group( 6 ), MPResultType.GeneratedName, null,
|
||||
clearContent? null: siteMatcher.group( 6 ), MPResultType.GeneratedName, null,
|
||||
ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ),
|
||||
MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() );
|
||||
if (clearContent) {
|
||||
site.setSitePassword( site.getResultType(), siteMatcher.group( 8 ) );
|
||||
site.setLoginName( MPResultType.StoredPersonal, siteMatcher.group( 6 ) );
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@@ -25,8 +25,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.Instant;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
|
||||
/**
|
||||
@@ -34,74 +32,57 @@ import org.joda.time.format.ISODateTimeFormat;
|
||||
*/
|
||||
public class MPJSONFile {
|
||||
|
||||
private static final DateTimeFormatter dateFormatter = ISODateTimeFormat.dateTimeNoMillis();
|
||||
|
||||
Export export;
|
||||
User user;
|
||||
|
||||
public MPJSONFile(final MPFileUser user, final MPMasterKey masterKey, final MPMarshaller.ContentMode contentMode)
|
||||
throws MPInvalidatedException {
|
||||
// if (!user.fullName || !strlen( user.fullName )) {
|
||||
// *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." };
|
||||
// return false;
|
||||
// }
|
||||
// if (!user.masterPassword || !strlen( user.masterPassword )) {
|
||||
// *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Missing master password." };
|
||||
// return false;
|
||||
// }
|
||||
// if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user.algorithm, user.fullName, user.masterPassword )) {
|
||||
// *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public MPJSONFile(final MPFileUser user)
|
||||
throws MPKeyUnavailableException {
|
||||
// Section: "export"
|
||||
Export fileExport = this.export = new Export();
|
||||
fileExport.format = 1;
|
||||
fileExport.redacted = contentMode.isRedacted();
|
||||
fileExport.date = dateFormatter.print( new Instant() );
|
||||
fileExport.redacted = user.getContentMode().isRedacted();
|
||||
fileExport.date = MPConstant.dateTimeFormatter.print( new Instant() );
|
||||
|
||||
// Section: "user"
|
||||
User fileUser = this.user = new User();
|
||||
fileUser.avatar = user.getAvatar();
|
||||
fileUser.fullName = user.getFullName();
|
||||
fileUser.full_name = user.getFullName();
|
||||
|
||||
fileUser.lastUsed = dateFormatter.print( user.getLastUsed() );
|
||||
fileUser.keyId = CodeUtils.encodeHex( masterKey.getKeyID( user.getAlgorithm() ) );
|
||||
fileUser.last_used = MPConstant.dateTimeFormatter.print( user.getLastUsed() );
|
||||
fileUser.key_id = CodeUtils.encodeHex( user.getKeyID() );
|
||||
|
||||
fileUser.algorithm = user.getAlgorithm().version();
|
||||
fileUser.defaultType = user.getDefaultType();
|
||||
fileUser.default_type = user.getDefaultType();
|
||||
|
||||
// Section "sites"
|
||||
fileUser.sites = new LinkedHashMap<>();
|
||||
sites = new LinkedHashMap<>();
|
||||
for (final MPFileSite site : user.getSites()) {
|
||||
Site fileSite;
|
||||
String content = null, loginContent = null;
|
||||
if (!contentMode.isRedacted()) {
|
||||
if (!fileExport.redacted) {
|
||||
// Clear Text
|
||||
content = masterKey.siteResult( site.getSiteName(), site.getSiteCounter(),
|
||||
MPKeyPurpose.Authentication, null, site.getResultType(), site.getSiteContent(),
|
||||
site.getAlgorithm() );
|
||||
loginContent = masterKey.siteResult( site.getSiteName(), site.getAlgorithm().mpw_default_counter(),
|
||||
MPKeyPurpose.Identification, null, site.getLoginType(), site.getLoginContent(),
|
||||
site.getAlgorithm() );
|
||||
content = site.getResult();
|
||||
loginContent = user.getMasterKey().siteResult(
|
||||
site.getSiteName(), site.getAlgorithm().mpw_default_counter(),
|
||||
MPKeyPurpose.Identification, null, site.getLoginType(), site.getLoginState(), site.getAlgorithm() );
|
||||
} else {
|
||||
// Redacted
|
||||
if (site.getResultType().supportsTypeFeature( MPSiteFeature.ExportContent ))
|
||||
content = site.getSiteContent();
|
||||
content = site.getSiteState();
|
||||
if (site.getLoginType().supportsTypeFeature( MPSiteFeature.ExportContent ))
|
||||
loginContent = site.getLoginContent();
|
||||
loginContent = site.getLoginState();
|
||||
}
|
||||
|
||||
fileUser.sites.put( site.getSiteName(), fileSite = new Site() );
|
||||
sites.put( site.getSiteName(), fileSite = new Site() );
|
||||
fileSite.type = site.getResultType();
|
||||
fileSite.counter = site.getSiteCounter();
|
||||
fileSite.counter = site.getSiteCounter().longValue();
|
||||
fileSite.algorithm = site.getAlgorithm().version();
|
||||
fileSite.password = content;
|
||||
fileSite.login_name = loginContent;
|
||||
fileSite.loginType = site.getLoginType();
|
||||
fileSite.login_type = site.getLoginType();
|
||||
|
||||
fileSite.uses = site.getUses();
|
||||
fileSite.lastUsed = dateFormatter.print( site.getLastUsed() );
|
||||
fileSite.last_used = MPConstant.dateTimeFormatter.print( site.getLastUsed() );
|
||||
|
||||
fileSite._ext_mpw = new Site.Ext();
|
||||
fileSite._ext_mpw.url = site.getUrl();
|
||||
|
||||
fileSite.questions = new LinkedHashMap<>();
|
||||
// for (size_t q = 0; q < site.questions_count; ++q) {
|
||||
@@ -133,10 +114,44 @@ public class MPJSONFile {
|
||||
}
|
||||
}
|
||||
|
||||
public MPFileUser toUser() {
|
||||
return new MPFileUser( user.fullName, CodeUtils.decodeHex( user.keyId ), user.algorithm.getAlgorithm(), user.avatar, user.defaultType, dateFormatter.parseDateTime( user.lastUsed ), MPMarshalFormat.JSON );
|
||||
public MPFileUser toUser(@Nullable final char[] masterPassword)
|
||||
throws MPIncorrectMasterPasswordException, MPKeyUnavailableException {
|
||||
MPFileUser user = new MPFileUser(
|
||||
this.user.full_name, CodeUtils.decodeHex( this.user.key_id ), this.user.algorithm.getAlgorithm(),
|
||||
this.user.avatar, this.user.default_type, MPConstant.dateTimeFormatter.parseDateTime( this.user.last_used ),
|
||||
MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE );
|
||||
if (masterPassword != null)
|
||||
user.authenticate( masterPassword );
|
||||
|
||||
for (final Map.Entry<String, Site> siteEntry : sites.entrySet()) {
|
||||
String siteName = siteEntry.getKey();
|
||||
Site fileSite = siteEntry.getValue();
|
||||
MPFileSite site = new MPFileSite(
|
||||
user, siteName, export.redacted? fileSite.password: null, UnsignedInteger.valueOf( fileSite.counter ),
|
||||
fileSite.type, fileSite.algorithm.getAlgorithm(),
|
||||
export.redacted? fileSite.login_name: null, fileSite.login_type,
|
||||
fileSite._ext_mpw.url, fileSite.uses, MPConstant.dateTimeFormatter.parseDateTime( fileSite.last_used ) );
|
||||
|
||||
if (!export.redacted) {
|
||||
if (fileSite.password != null)
|
||||
site.setSitePassword( fileSite.type, fileSite.password );
|
||||
if (fileSite.login_name != null)
|
||||
site.setLoginName( fileSite.login_type, fileSite.login_name );
|
||||
}
|
||||
|
||||
user.addSite( site );
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
// -- Data
|
||||
|
||||
Export export;
|
||||
User user;
|
||||
Map<String, Site> sites;
|
||||
|
||||
|
||||
public static class Export {
|
||||
|
||||
int format;
|
||||
@@ -147,47 +162,44 @@ public class MPJSONFile {
|
||||
|
||||
public static class User {
|
||||
|
||||
String fullName;
|
||||
|
||||
int avatar;
|
||||
String full_name;
|
||||
String last_used;
|
||||
String key_id;
|
||||
MPMasterKey.Version algorithm;
|
||||
boolean redacted;
|
||||
|
||||
int avatar;
|
||||
MPResultType defaultType;
|
||||
String lastUsed;
|
||||
String keyId;
|
||||
|
||||
Map<String, Site> sites;
|
||||
MPResultType default_type;
|
||||
}
|
||||
|
||||
|
||||
public static class Site {
|
||||
|
||||
MPResultType type;
|
||||
long counter;
|
||||
MPMasterKey.Version algorithm;
|
||||
@Nullable
|
||||
String password;
|
||||
@Nullable
|
||||
String login_name;
|
||||
String name;
|
||||
String content;
|
||||
MPResultType type;
|
||||
UnsignedInteger counter;
|
||||
MPMasterKey.Version algorithm;
|
||||
|
||||
String loginContent;
|
||||
MPResultType loginType;
|
||||
|
||||
String url;
|
||||
int uses;
|
||||
String lastUsed;
|
||||
MPResultType login_type;
|
||||
int uses;
|
||||
String last_used;
|
||||
|
||||
Map<String, Question> questions;
|
||||
}
|
||||
|
||||
Ext _ext_mpw;
|
||||
|
||||
|
||||
public static class Question {
|
||||
public static class Ext {
|
||||
|
||||
String keyword;
|
||||
String content;
|
||||
MPResultType type;
|
||||
@Nullable
|
||||
String url;
|
||||
}
|
||||
|
||||
|
||||
public static class Question {
|
||||
|
||||
MPResultType type;
|
||||
String answer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -31,14 +31,14 @@ public class MPJSONMarshaller implements MPMarshaller {
|
||||
private final Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter( MPMasterKey.Version.class, new EnumOrdinalAdapter() )
|
||||
.registerTypeAdapter( MPResultType.class, new MPResultTypeAdapter() )
|
||||
.setFieldNamingStrategy( FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES )
|
||||
.setFieldNamingStrategy( FieldNamingPolicy.IDENTITY )
|
||||
.setPrettyPrinting().create();
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String marshall(final MPFileUser user, final MPMasterKey masterKey, final ContentMode contentMode)
|
||||
throws MPInvalidatedException, MPMarshalException {
|
||||
public String marshall(final MPFileUser user)
|
||||
throws MPKeyUnavailableException, MPMarshalException {
|
||||
|
||||
return gson.toJson( new MPJSONFile( user, masterKey, contentMode ) );
|
||||
return gson.toJson( new MPJSONFile( user ) );
|
||||
}
|
||||
}
|
||||
|
@@ -19,11 +19,11 @@
|
||||
package com.lyndir.masterpassword.model;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.lyndir.masterpassword.MPMasterKey;
|
||||
import com.lyndir.masterpassword.MPResultType;
|
||||
import com.lyndir.masterpassword.*;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
||||
/**
|
||||
@@ -39,19 +39,29 @@ public class MPJSONUnmarshaller implements MPUnmarshaller {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MPFileUser unmarshall(@Nonnull final File file)
|
||||
throws IOException, MPMarshalException {
|
||||
public MPFileUser unmarshall(@Nonnull final File file, @Nullable final char[] masterPassword)
|
||||
throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException {
|
||||
|
||||
try (Reader reader = new InputStreamReader( new FileInputStream( file ), StandardCharsets.UTF_8 )) {
|
||||
return gson.fromJson( reader, MPJSONFile.class ).toUser();
|
||||
try {
|
||||
return gson.fromJson( reader, MPJSONFile.class ).toUser( masterPassword );
|
||||
}
|
||||
catch (final JsonSyntaxException e) {
|
||||
throw new MPMarshalException( "Couldn't parse JSON in: " + file, e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MPFileUser unmarshall(@Nonnull final String content)
|
||||
throws MPMarshalException {
|
||||
public MPFileUser unmarshall(@Nonnull final String content, @Nullable final char[] masterPassword)
|
||||
throws MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException {
|
||||
|
||||
return gson.fromJson( content, MPJSONFile.class ).toUser();
|
||||
try {
|
||||
return gson.fromJson( content, MPJSONFile.class ).toUser( masterPassword );
|
||||
}
|
||||
catch (final JsonSyntaxException e) {
|
||||
throw new MPMarshalException( "Couldn't parse JSON", e );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,7 @@
|
||||
|
||||
package com.lyndir.masterpassword.model;
|
||||
|
||||
import com.lyndir.masterpassword.MPInvalidatedException;
|
||||
import com.lyndir.masterpassword.MPMasterKey;
|
||||
import com.lyndir.masterpassword.MPKeyUnavailableException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
|
||||
@@ -29,8 +28,8 @@ import javax.annotation.Nonnull;
|
||||
public interface MPMarshaller {
|
||||
|
||||
@Nonnull
|
||||
String marshall(MPFileUser user, MPMasterKey masterKey, ContentMode contentMode)
|
||||
throws MPInvalidatedException, MPMarshalException;
|
||||
String marshall(MPFileUser user)
|
||||
throws MPKeyUnavailableException, MPMarshalException;
|
||||
|
||||
enum ContentMode {
|
||||
PROTECTED( "Export of site names and stored passwords (unless device-private) encrypted with the master key.", true ),
|
||||
|
@@ -31,6 +31,8 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public abstract class MPSite {
|
||||
|
||||
public abstract MPUser<?> getUser();
|
||||
|
||||
public abstract String getSiteName();
|
||||
|
||||
public abstract void setSiteName(String siteName);
|
||||
@@ -43,24 +45,28 @@ public abstract class MPSite {
|
||||
|
||||
public abstract void setResultType(MPResultType resultType);
|
||||
|
||||
public abstract MPResultType getLoginType();
|
||||
|
||||
public abstract void setLoginType(@Nullable MPResultType loginType);
|
||||
|
||||
public abstract MPAlgorithm getAlgorithm();
|
||||
|
||||
public abstract void setAlgorithm(MPAlgorithm algorithm);
|
||||
|
||||
public String resultFor(final MPMasterKey masterKey, final MPKeyPurpose keyPurpose, @Nullable final String keyContext,
|
||||
public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext,
|
||||
@Nullable final String siteContent)
|
||||
throws MPInvalidatedException {
|
||||
throws MPKeyUnavailableException {
|
||||
|
||||
return masterKey.siteResult(
|
||||
return getUser().getMasterKey().siteResult(
|
||||
getSiteName(), getSiteCounter(), keyPurpose, keyContext, getResultType(), siteContent, getAlgorithm() );
|
||||
}
|
||||
|
||||
public String loginFor(final MPMasterKey masterKey, final MPResultType loginType, @Nullable final String loginContent)
|
||||
throws MPInvalidatedException {
|
||||
public String getLogin(@Nullable final String loginContent)
|
||||
throws MPKeyUnavailableException {
|
||||
|
||||
return masterKey.siteResult(
|
||||
getSiteName(), getAlgorithm().mpw_default_counter(), MPKeyPurpose.Identification, null, loginType, loginContent,
|
||||
getAlgorithm() );
|
||||
return getUser().getMasterKey().siteResult(
|
||||
getSiteName(), getAlgorithm().mpw_default_counter(), MPKeyPurpose.Identification, null,
|
||||
getLoginType(), loginContent, getAlgorithm() );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -18,9 +18,11 @@
|
||||
|
||||
package com.lyndir.masterpassword.model;
|
||||
|
||||
import com.lyndir.masterpassword.MPKeyUnavailableException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
||||
/**
|
||||
@@ -29,10 +31,10 @@ import javax.annotation.Nonnull;
|
||||
public interface MPUnmarshaller {
|
||||
|
||||
@Nonnull
|
||||
MPFileUser unmarshall(@Nonnull File file)
|
||||
throws IOException, MPMarshalException;
|
||||
MPFileUser unmarshall(@Nonnull File file, @Nullable char[] masterPassword)
|
||||
throws IOException, MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException;
|
||||
|
||||
@Nonnull
|
||||
MPFileUser unmarshall(@Nonnull String content)
|
||||
throws MPMarshalException;
|
||||
MPFileUser unmarshall(@Nonnull String content, @Nullable char[] masterPassword)
|
||||
throws MPMarshalException, MPIncorrectMasterPasswordException, MPKeyUnavailableException;
|
||||
}
|
||||
|
@@ -20,7 +20,6 @@ package com.lyndir.masterpassword.model;
|
||||
|
||||
import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.lyndir.lhunath.opal.system.CodeUtils;
|
||||
import com.lyndir.masterpassword.*;
|
||||
import java.util.Collection;
|
||||
@@ -44,12 +43,16 @@ public abstract class MPUser<S extends MPSite> {
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public MPMasterKey getMasterKey() {
|
||||
return Preconditions.checkNotNull( key, "User is not authenticated: %s", getFullName() );
|
||||
public MPMasterKey getMasterKey()
|
||||
throws MPKeyUnavailableException {
|
||||
if (key == null)
|
||||
throw new MPKeyUnavailableException();
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public String exportKeyID()
|
||||
throws MPInvalidatedException {
|
||||
throws MPKeyUnavailableException {
|
||||
return CodeUtils.encodeHex( getMasterKey().getKeyID( getAlgorithm() ) );
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user