From bda1ac3bd461f3ec38d68eeb3cc72ef9dd0d8363 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Tue, 15 May 2018 13:53:27 -0400 Subject: [PATCH] Refactor masterpassword-model --- core/java/algorithm/build.gradle | 4 +- .../lyndir/masterpassword/MPAlgorithm.java | 2 +- .../lyndir/masterpassword/MPAlgorithmV0.java | 2 +- .../masterpassword/model/MPFileSite.java | 212 ------------------ .../MPIncorrectMasterPasswordException.java | 7 +- .../masterpassword/model/MPQuestion.java | 31 ++- .../lyndir/masterpassword/model/MPSite.java | 82 ++----- .../masterpassword/model/MPSiteResult.java | 24 +- .../lyndir/masterpassword/model/MPUser.java | 110 ++++----- .../masterpassword/model/MPUserManager.java | 17 +- .../model/impl/MPBasicQuestion.java | 93 ++++++++ .../model/impl/MPBasicSite.java | 165 ++++++++++++++ .../model/impl/MPBasicUser.java | 186 +++++++++++++++ .../model/{ => impl}/MPFileQuestion.java | 67 +++--- .../masterpassword/model/impl/MPFileSite.java | 150 +++++++++++++ .../model/{ => impl}/MPFileUser.java | 122 +++------- .../model/{ => impl}/MPFileUserManager.java | 7 +- .../model/{ => impl}/MPFlatMarshaller.java | 8 +- .../model/{ => impl}/MPFlatUnmarshaller.java | 17 +- .../model/{ => impl}/MPJSONAnyObject.java | 2 +- .../model/{ => impl}/MPJSONFile.java | 28 +-- .../model/{ => impl}/MPJSONMarshaller.java | 4 +- .../model/{ => impl}/MPJSONUnmarshaller.java | 5 +- .../model/{ => impl}/MPMarshalException.java | 2 +- .../model/{ => impl}/MPMarshalFormat.java | 2 +- .../model/{ => impl}/MPMarshaller.java | 2 +- .../model/{ => impl}/MPUnmarshaller.java | 3 +- .../model/impl/package-info.java | 25 +++ .../lyndir/masterpassword/Preferences.java | 2 +- .../gui/model/IncognitoSite.java | 75 +------ .../gui/model/IncognitoUser.java | 44 +--- .../gui/view/ModelAuthenticationPanel.java | 4 +- .../gui/view/PasswordFrame.java | 17 +- .../masterpassword/gui/view/UnlockFrame.java | 7 +- 34 files changed, 878 insertions(+), 650 deletions(-) delete mode 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileSite.java mode change 100755 => 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java create mode 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java create mode 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java create mode 100755 core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPFileQuestion.java (64%) create mode 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPFileUser.java (54%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPFileUserManager.java (95%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPFlatMarshaller.java (94%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPFlatUnmarshaller.java (94%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPJSONAnyObject.java (96%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPJSONFile.java (89%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPJSONMarshaller.java (92%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPJSONUnmarshaller.java (93%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPMarshalException.java (96%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPMarshalFormat.java (97%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPMarshaller.java (97%) rename core/java/model/src/main/java/com/lyndir/masterpassword/model/{ => impl}/MPUnmarshaller.java (92%) create mode 100644 core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/package-info.java diff --git a/core/java/algorithm/build.gradle b/core/java/algorithm/build.gradle index e3e11937..d1b6442c 100644 --- a/core/java/algorithm/build.gradle +++ b/core/java/algorithm/build.gradle @@ -5,10 +5,10 @@ plugins { description = 'Master Password Algorithm Implementation' dependencies { - compile( group: 'com.lyndir.lhunath.opal', name: 'opal-system', version: '1.6-p11' ) { + compile( group: 'com.lyndir.lhunath.opal', name: 'opal-system', version: 'GIT-SNAPSHOT' ) { exclude( module: 'joda-time' ) } - compile group: 'com.lyndir.lhunath.opal', name: 'opal-crypto', version: '1.6-p11' + compile group: 'com.lyndir.lhunath.opal', name: 'opal-crypto', version: 'GIT-SNAPSHOT' compile group: 'com.lambdaworks', name: 'scrypt', version: '1.4.0' compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.5' diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java index 00496abe..ed042cd7 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java @@ -59,7 +59,7 @@ public abstract class MPAlgorithm { /** * mpw: defaults: password result type. */ - public abstract MPResultType mpw_default_password_type(); + public abstract MPResultType mpw_default_result_type(); /** * mpw: defaults: login result type. diff --git a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithmV0.java b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithmV0.java index ad4706bd..931b01f4 100644 --- a/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithmV0.java +++ b/core/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithmV0.java @@ -247,7 +247,7 @@ public class MPAlgorithmV0 extends MPAlgorithm { } @Override - public MPResultType mpw_default_password_type() { + public MPResultType mpw_default_result_type() { return MPResultType.GeneratedLong; } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileSite.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileSite.java deleted file mode 100644 index db6e8233..00000000 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileSite.java +++ /dev/null @@ -1,212 +0,0 @@ -//============================================================================== -// This file is part of Master Password. -// Copyright (c) 2011-2017, Maarten Billemont. -// -// Master Password is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Master Password is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You can find a copy of the GNU General Public License in the -// LICENSE file. Alternatively, see . -//============================================================================== - -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 java.util.*; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.joda.time.Instant; -import org.joda.time.ReadableInstant; - - -/** - * @author lhunath, 14-12-05 - */ -public class MPFileSite extends MPSite { - - private final MPFileUser user; - - private String siteName; - private UnsignedInteger siteCounter; - @Nullable - private String siteState; - private MPResultType resultType; - private MPAlgorithm algorithm; - - @Nullable - private String loginState; - private MPResultType loginType; - - @Nullable - private String url; - private int uses; - private ReadableInstant lastUsed; - - private final Collection questions = new LinkedHashSet<>(); - - public MPFileSite(final MPFileUser user, final String siteName) { - this( user, siteName, null, null, user.getAlgorithm() ); - } - - public MPFileSite(final MPFileUser user, final String siteName, @Nullable final UnsignedInteger siteCounter, - @Nullable final MPResultType resultType, final MPAlgorithm algorithm) { - this( user, siteName, null, siteCounter, resultType, algorithm, - null, null, null, 0, new Instant() ); - } - - 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 loginState, @Nullable final MPResultType loginType, - @Nullable final String url, final int uses, final ReadableInstant lastUsed) { - this.user = user; - this.algorithm = algorithm; - - this.siteName = siteName; - this.siteState = siteState; - this.siteCounter = ifNotNullElse( siteCounter, getAlgorithm().mpw_default_counter() ); - this.resultType = ifNotNullElse( resultType, getAlgorithm().mpw_default_password_type() ); - this.loginState = loginState; - this.loginType = ifNotNullElse( loginType, getAlgorithm().mpw_default_login_type() ); - this.url = url; - this.uses = uses; - this.lastUsed = lastUsed; - } - - public String getResult() - throws MPKeyUnavailableException { - - return getResult( MPKeyPurpose.Authentication, null ); - } - - public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext) - throws MPKeyUnavailableException { - - return getResult( keyPurpose, keyContext, siteState ); - } - - public String getLogin() - throws MPKeyUnavailableException { - - return getLogin( loginState ); - } - - @Override - public MPUser getUser() { - return user; - } - - @Override - public String getSiteName() { - return siteName; - } - - @Override - public void setSiteName(final String siteName) { - this.siteName = siteName; - } - - @Nullable - public String getSiteState() { - return siteState; - } - - public void setSitePassword(final MPResultType resultType, @Nullable final String password) - throws MPKeyUnavailableException { - this.resultType = resultType; - - if (password == null) - this.siteState = null; - else - this.siteState = getState( - MPKeyPurpose.Authentication, null, siteCounter, resultType, password ); - } - - @Override - public UnsignedInteger getSiteCounter() { - return siteCounter; - } - - @Override - public void setSiteCounter(final UnsignedInteger siteCounter) { - this.siteCounter = siteCounter; - } - - @Override - public MPResultType getResultType() { - return resultType; - } - - @Override - public void setResultType(final MPResultType resultType) { - 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; - } - - @Override - public void setAlgorithm(final MPAlgorithm algorithm) { - this.algorithm = algorithm; - } - - @Override - public Collection getQuestions() { - return Collections.unmodifiableCollection( questions ); - } - - @Nullable - public String getLoginState() { - return loginState; - } - - public void setLoginName(@Nonnull final MPResultType loginType, @Nonnull final String loginName) - throws MPKeyUnavailableException { - this.loginType = loginType; - this.loginState = getState( MPKeyPurpose.Identification, null, null, this.loginType, loginName ); - } - - @Nullable - public String getUrl() { - return url; - } - - public void setUrl(@Nullable final String url) { - this.url = url; - } - - public int getUses() { - return uses; - } - - public ReadableInstant getLastUsed() { - return lastUsed; - } - - public void use() { - uses++; - lastUsed = new Instant(); - user.use(); - } -} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPIncorrectMasterPasswordException.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPIncorrectMasterPasswordException.java index 7d899b4f..ab1497b8 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPIncorrectMasterPasswordException.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPIncorrectMasterPasswordException.java @@ -18,20 +18,21 @@ package com.lyndir.masterpassword.model; + /** * @author lhunath, 14-12-17 */ public class MPIncorrectMasterPasswordException extends Exception { - private final MPFileUser user; + private final MPUser user; - public MPIncorrectMasterPasswordException(final MPFileUser user) { + public MPIncorrectMasterPasswordException(final MPUser user) { super( "Incorrect master password for user: " + user.getFullName() ); this.user = user; } - public MPFileUser getUser() { + public MPUser getUser() { return user; } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPQuestion.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPQuestion.java index 13540e8b..4f57ce9c 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPQuestion.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPQuestion.java @@ -18,24 +18,35 @@ package com.lyndir.masterpassword.model; -import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.MPKeyUnavailableException; +import com.lyndir.masterpassword.MPResultType; +import javax.annotation.Nonnull; import javax.annotation.Nullable; /** - * @author lhunath, 2018-05-14 + * @author lhunath, 2018-05-15 */ -public abstract class MPQuestion { +public interface MPQuestion extends Comparable { - public abstract MPSite getSite(); + // -- Meta - public abstract String getKeyword(); + @Nonnull + String getKeyword(); - public abstract MPResultType getType(); + // -- Algorithm - public String getAnswer(@Nullable final String state) - throws MPKeyUnavailableException { + @Nonnull + MPResultType getType(); - return getSite().getResult( MPKeyPurpose.Recovery, getKeyword(), null, getType(), state ); - } + void setType(MPResultType type); + + @Nonnull + String getAnswer(@Nullable String state) + throws MPKeyUnavailableException; + + // -- Relationship + + @Nonnull + MPSite getSite(); } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java index 9da33720..e8a327f0 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSite.java @@ -18,90 +18,50 @@ package com.lyndir.masterpassword.model; -import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; -import static com.lyndir.lhunath.opal.system.util.StringUtils.*; - import com.google.common.primitives.UnsignedInteger; import com.lyndir.masterpassword.*; import java.util.Collection; -import java.util.Objects; import javax.annotation.Nullable; /** - * @author lhunath, 14-12-16 + * @author lhunath, 2018-05-14 */ -public abstract class MPSite { +public interface MPSite extends Comparable { - public abstract MPUser getUser(); + // - Meta - public abstract String getSiteName(); + String getName(); - public abstract void setSiteName(String siteName); + void setName(String name); - public abstract UnsignedInteger getSiteCounter(); + // - Algorithm - public abstract void setSiteCounter(UnsignedInteger siteCounter); + MPAlgorithm getAlgorithm(); - public abstract MPResultType getResultType(); + void setAlgorithm(MPAlgorithm algorithm); - public abstract void setResultType(MPResultType resultType); + UnsignedInteger getCounter(); - public abstract MPResultType getLoginType(); + void setCounter(UnsignedInteger counter); - public abstract void setLoginType(@Nullable MPResultType loginType); + MPResultType getResultType(); - public abstract MPAlgorithm getAlgorithm(); + void setResultType(MPResultType resultType); - public abstract void setAlgorithm(MPAlgorithm algorithm); + MPResultType getLoginType(); - public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, - @Nullable final String state) - throws MPKeyUnavailableException { + void setLoginType(@Nullable MPResultType loginType); - return getResult( keyPurpose, keyContext, getSiteCounter(), getResultType(), state ); - } + String getResult(MPKeyPurpose keyPurpose, @Nullable String keyContext, @Nullable String state) + throws MPKeyUnavailableException; - protected String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, - @Nullable final UnsignedInteger siteCounter, final MPResultType type, - @Nullable final String state) - throws MPKeyUnavailableException { + String getLogin(@Nullable String state) + throws MPKeyUnavailableException; - return getUser().getMasterKey().siteResult( - getSiteName(), ifNotNullElse( siteCounter, getAlgorithm().mpw_default_counter() ), keyPurpose, keyContext, - type, state, getAlgorithm() ); - } + // - Relations - protected String getState(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, - @Nullable final UnsignedInteger siteCounter, final MPResultType type, - @Nullable final String state) - throws MPKeyUnavailableException { + MPUser getUser(); - return getUser().getMasterKey().siteState( - getSiteName(), ifNotNullElse( siteCounter, getAlgorithm().mpw_default_counter() ), keyPurpose, keyContext, - type, state, getAlgorithm() ); - } - - public String getLogin(@Nullable final String loginContent) - throws MPKeyUnavailableException { - - return getResult( MPKeyPurpose.Identification, null,null, getLoginType(), loginContent ); - } - - public abstract Collection getQuestions(); - - @Override - public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPSite) && Objects.equals( getSiteName(), ((MPSite) obj).getSiteName() )); - } - - @Override - public int hashCode() { - return Objects.hashCode( getSiteName() ); - } - - @Override - public String toString() { - return strf( "{%s: %s}", getClass().getSimpleName(), getSiteName() ); - } + Collection getQuestions(); } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java index b9af9ec2..c5845e22 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteResult.java @@ -21,35 +21,41 @@ package com.lyndir.masterpassword.model; import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import java.util.Objects; +import org.jetbrains.annotations.NotNull; /** * @author lhunath, 14-12-07 */ -public class MPSiteResult { +public class MPSiteResult implements Comparable { - private final MPFileSite site; + private final MPSite site; - public MPSiteResult(final MPFileSite site) { + public MPSiteResult(final MPSite site) { this.site = site; } - public MPFileSite getSite() { + public MPSite getSite() { return site; } @Override - public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPSiteResult) && Objects.equals( site, ((MPSiteResult) obj).site )); + public int hashCode() { + return Objects.hashCode( getSite() ); } @Override - public int hashCode() { - return Objects.hashCode( site ); + public boolean equals(final Object obj) { + return (this == obj) || ((obj instanceof MPSiteResult) && Objects.equals( getSite(), ((MPSiteResult) obj).getSite() )); + } + + @Override + public int compareTo(@NotNull final MPSiteResult o) { + return getSite().compareTo( o.getSite() ); } @Override public String toString() { - return strf( "{MPSiteResult: %s}", site ); + return strf( "{%s: %s}", getClass().getSimpleName(), getSite() ); } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java old mode 100755 new mode 100644 index e5ae697b..63be260c --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java @@ -18,72 +18,78 @@ package com.lyndir.masterpassword.model; -import static com.lyndir.lhunath.opal.system.util.StringUtils.*; - -import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.masterpassword.*; import java.util.Collection; -import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; /** - * @author lhunath, 2014-06-08 + * @author lhunath, 2018-05-14 */ -public abstract class MPUser { +public interface MPUser extends Comparable> { + + // - Meta + + int getAvatar(); + + void setAvatar(int avatar); + + @Nonnull + String getFullName(); + + // - Algorithm + + @Nonnull + MPAlgorithm getAlgorithm(); + + void setAlgorithm(MPAlgorithm algorithm); @Nullable - protected MPMasterKey key; + byte[] getKeyID(); - public abstract String getFullName(); + @Nullable + String exportKeyID(); - public boolean isMasterKeyAvailable() { - return key != null; - } - - @Nonnull - public MPMasterKey getMasterKey() - throws MPKeyUnavailableException { - if (key == null) - throw new MPKeyUnavailableException(); - - return key; - } - - public String exportKeyID() - throws MPKeyUnavailableException { - return CodeUtils.encodeHex( getMasterKey().getKeyID( getAlgorithm() ) ); - } - - public abstract MPAlgorithm getAlgorithm(); - - public int getAvatar() { - return 0; - } - - public abstract void addSite(S site); - - public abstract void deleteSite(S site); - - public abstract Collection findSites(String query); - - @Nonnull - public abstract MPMasterKey authenticate(char[] masterPassword) + /** + * Performs an authentication attempt against the keyID for this user. + * + * Note: If a keyID is not set, authentication will always succeed and the keyID will be set to match the given master password. + * + * @param masterPassword The password to authenticate with. + * + * @throws MPIncorrectMasterPasswordException If authentication fails due to the given master password not matching the user's keyID. + */ + void authenticate(char[] masterPassword) throws MPIncorrectMasterPasswordException; - @Override - public int hashCode() { - return Objects.hashCode( getFullName() ); - } + /** + * Performs an authentication attempt against the keyID for this user. + * + * Note: If a keyID is not set, authentication will always succeed and the keyID will be set to match the given key. + * + * @param masterKey The master key to authenticate with. + * + * @throws MPIncorrectMasterPasswordException If authentication fails due to the given master password not matching the user's keyID. + */ + void authenticate(MPMasterKey masterKey) + throws MPIncorrectMasterPasswordException, MPKeyUnavailableException; - @Override - public boolean equals(final Object obj) { - return (this == obj) || ((obj instanceof MPUser) && Objects.equals( getFullName(), ((MPUser) obj).getFullName() )); - } + boolean isMasterKeyAvailable(); - @Override - public String toString() { - return strf( "{%s: %s}", getClass().getSimpleName(), getFullName() ); - } + @Nonnull + MPMasterKey getMasterKey() + throws MPKeyUnavailableException; + + // - Relations + + void addSite(S site); + + void deleteSite(S site); + + @Nonnull + Collection getSites(); + + @Nonnull + Collection findSites(String query); } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java index 9e8261d6..1590161a 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUserManager.java @@ -19,6 +19,7 @@ package com.lyndir.masterpassword.model; import com.google.common.collect.*; +import com.lyndir.masterpassword.model.impl.MPFileUser; import java.util.Map; import java.util.SortedSet; @@ -26,28 +27,28 @@ import java.util.SortedSet; /** * @author lhunath, 14-12-05 */ -public abstract class MPUserManager { +public abstract class MPUserManager> { - private final Map usersByName = Maps.newHashMap(); + private final Map usersByName = Maps.newHashMap(); - protected MPUserManager(final Iterable users) { - for (final MPFileUser user : users) + protected MPUserManager(final Iterable users) { + for (final U user : users) usersByName.put( user.getFullName(), user ); } - public SortedSet getUsers() { + public SortedSet getUsers() { return FluentIterable.from( usersByName.values() ).toSortedSet( Ordering.natural() ); } - public MPFileUser getUserNamed(final String fullName) { + public U getUserNamed(final String fullName) { return usersByName.get( fullName ); } - public void addUser(final MPFileUser user) { + public void addUser(final U user) { usersByName.put( user.getFullName(), user ); } - public void deleteUser(final MPFileUser user) { + public void deleteUser(final U user) { usersByName.remove( user.getFullName() ); } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java new file mode 100644 index 00000000..a4c7780b --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicQuestion.java @@ -0,0 +1,93 @@ +//============================================================================== +// This file is part of Master Password. +// Copyright (c) 2011-2017, Maarten Billemont. +// +// Master Password is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Master Password is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You can find a copy of the GNU General Public License in the +// LICENSE file. Alternatively, see . +//============================================================================== + +package com.lyndir.masterpassword.model.impl; + +import static com.lyndir.lhunath.opal.system.util.StringUtils.strf; + +import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.MPQuestion; +import java.util.Objects; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.jetbrains.annotations.NotNull; + + +/** + * @author lhunath, 2018-05-14 + */ +public abstract class MPBasicQuestion implements MPQuestion { + + private final String keyword; + private MPResultType type; + + protected MPBasicQuestion(final String keyword, final MPResultType type) { + this.keyword = keyword; + this.type = type; + } + + @Nonnull + @Override + public String getKeyword() { + return keyword; + } + + @Nonnull + @Override + public MPResultType getType() { + return type; + } + + @Override + public void setType(final MPResultType type) { + this.type = type; + } + + @Nonnull + @Override + public String getAnswer(@Nullable final String state) + throws MPKeyUnavailableException { + + return getSite().getResult( MPKeyPurpose.Recovery, getKeyword(), null, getType(), state ); + } + + @Nonnull + @Override + public abstract MPBasicSite getSite(); + + + @Override + public int hashCode() { + return Objects.hashCode( getKeyword() ); + } + + @Override + public boolean equals(final Object obj) { + return (this == obj) || ((obj instanceof MPQuestion) && Objects.equals( getKeyword(), ((MPQuestion) obj).getKeyword() )); + } + + @Override + public int compareTo(@NotNull final MPQuestion o) { + return getKeyword().compareTo( o.getKeyword() ); + } + + @Override + public String toString() { + return strf( "{%s: %s}", getClass().getSimpleName(), getKeyword() ); + } +} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java new file mode 100644 index 00000000..3ea52221 --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java @@ -0,0 +1,165 @@ +//============================================================================== +// This file is part of Master Password. +// Copyright (c) 2011-2017, Maarten Billemont. +// +// Master Password is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Master Password is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You can find a copy of the GNU General Public License in the +// LICENSE file. Alternatively, see . +//============================================================================== + +package com.lyndir.masterpassword.model.impl; + +import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; +import static com.lyndir.lhunath.opal.system.util.StringUtils.*; + +import com.google.common.primitives.UnsignedInteger; +import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.MPQuestion; +import com.lyndir.masterpassword.model.MPSite; +import java.util.*; +import javax.annotation.Nullable; +import org.jetbrains.annotations.NotNull; + + +/** + * @author lhunath, 14-12-16 + */ +public abstract class MPBasicSite implements MPSite { + + private String name; + private MPAlgorithm algorithm; + private UnsignedInteger counter; + private MPResultType resultType; + private MPResultType loginType; + + private final Collection questions = new LinkedHashSet<>(); + + protected MPBasicSite(final String name, final MPAlgorithm algorithm) { + this( name, algorithm, null, null, null ); + } + + protected MPBasicSite(final String name, final MPAlgorithm algorithm, @Nullable final UnsignedInteger counter, + @Nullable final MPResultType resultType, @Nullable final MPResultType loginType) { + this.name = name; + this.algorithm = algorithm; + this.counter = (counter == null)? algorithm.mpw_default_counter(): counter; + this.resultType = (resultType == null)? algorithm.mpw_default_result_type(): resultType; + this.loginType = (loginType == null)? algorithm.mpw_default_login_type(): loginType; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(final String name) { + this.name = name; + } + + @Override + public MPAlgorithm getAlgorithm() { + return algorithm; + } + + @Override + public void setAlgorithm(final MPAlgorithm algorithm) { + this.algorithm = algorithm; + } + + @Override + public UnsignedInteger getCounter() { + return counter; + } + + @Override + public void setCounter(final UnsignedInteger counter) { + this.counter = counter; + } + + @Override + public MPResultType getResultType() { + return resultType; + } + + @Override + public void setResultType(final MPResultType resultType) { + 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 String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, @Nullable final String state) + throws MPKeyUnavailableException { + + return getResult( keyPurpose, keyContext, getCounter(), getResultType(), state ); + } + + protected String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, + @Nullable final UnsignedInteger counter, final MPResultType type, @Nullable final String state) + throws MPKeyUnavailableException { + + return getUser().getMasterKey().siteResult( + getName(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ), keyPurpose, keyContext, + type, state, getAlgorithm() ); + } + + protected String getState(final MPKeyPurpose keyPurpose, @Nullable final String keyContext, + @Nullable final UnsignedInteger counter, final MPResultType type, @Nullable final String state) + throws MPKeyUnavailableException { + + return getUser().getMasterKey().siteState( + getName(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ), keyPurpose, keyContext, + type, state, getAlgorithm() ); + } + + @Override + public String getLogin(@Nullable final String state) + throws MPKeyUnavailableException { + + return getResult( MPKeyPurpose.Identification, null, null, getLoginType(), state ); + } + + @Override + public Collection getQuestions() { + return Collections.unmodifiableCollection( questions ); + } + + @Override + public int hashCode() { + return Objects.hashCode( getName() ); + } + + @Override + public boolean equals(final Object obj) { + return (this == obj) || ((obj instanceof MPSite) && Objects.equals( getName(), ((MPSite) obj).getName() )); + } + + @Override + public int compareTo(@NotNull final MPSite o) { + return getName().compareTo( o.getName() ); + } + + @Override + public String toString() { + return strf( "{%s: %s}", getClass().getSimpleName(), getName() ); + } +} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java new file mode 100755 index 00000000..f73aa33e --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicUser.java @@ -0,0 +1,186 @@ +//============================================================================== +// This file is part of Master Password. +// Copyright (c) 2011-2017, Maarten Billemont. +// +// Master Password is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Master Password is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You can find a copy of the GNU General Public License in the +// LICENSE file. Alternatively, see . +//============================================================================== + +package com.lyndir.masterpassword.model.impl; + +import static com.lyndir.lhunath.opal.system.util.StringUtils.*; + +import com.google.common.collect.ImmutableList; +import com.lyndir.lhunath.opal.system.CodeUtils; +import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; +import com.lyndir.masterpassword.model.MPUser; +import java.util.*; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + + +/** + * @author lhunath, 2014-06-08 + */ +public abstract class MPBasicUser implements MPUser { + + private int avatar; + private final String fullName; + private MPAlgorithm algorithm; + @Nullable + protected MPMasterKey masterKey; + + private final Collection sites = new LinkedHashSet<>(); + + protected MPBasicUser(final String fullName, final MPAlgorithm algorithm) { + this( 0, fullName, algorithm ); + } + + protected MPBasicUser(final int avatar, final String fullName, final MPAlgorithm algorithm) { + this.avatar = avatar; + this.fullName = fullName; + this.algorithm = algorithm; + } + + @Override + public int getAvatar() { + return avatar; + } + + @Override + public void setAvatar(final int avatar) { + this.avatar = avatar; + } + + @Nonnull + @Override + public String getFullName() { + return fullName; + } + + @Nonnull + @Override + public MPAlgorithm getAlgorithm() { + return algorithm; + } + + @Override + public void setAlgorithm(final MPAlgorithm algorithm) { + this.algorithm = algorithm; + } + + @Nullable + @Override + public byte[] getKeyID() { + try { + return getMasterKey().getKeyID( getAlgorithm() ); + } + catch (final MPKeyUnavailableException ignore) { + return null; + } + } + + @Nullable + @Override + public String exportKeyID() { + return CodeUtils.encodeHex( getKeyID() ); + } + + @Override + public void authenticate(final char[] masterPassword) + throws MPIncorrectMasterPasswordException { + try { + authenticate( new MPMasterKey( getFullName(), masterPassword ) ); + } + catch (final MPKeyUnavailableException e) { + throw new IllegalStateException( e ); + } + } + + @Override + public void authenticate(final MPMasterKey masterKey) + throws MPIncorrectMasterPasswordException, MPKeyUnavailableException { + if (!masterKey.getFullName().equals( getFullName() )) + throw new IllegalArgumentException( + "Master key (for " + masterKey.getFullName() + ") is not for this user (" + getFullName() + ")." ); + + byte[] keyID = getKeyID(); + if ((keyID != null) && !Arrays.equals( masterKey.getKeyID( getAlgorithm() ), keyID )) + throw new MPIncorrectMasterPasswordException( this ); + + this.masterKey = masterKey; + } + + @Override + public boolean isMasterKeyAvailable() { + return masterKey != null; + } + + @Nonnull + @Override + public MPMasterKey getMasterKey() + throws MPKeyUnavailableException { + if (masterKey == null) + throw new MPKeyUnavailableException(); + + return masterKey; + } + + @Override + public void addSite(final S site) { + sites.add( site ); + } + + @Override + public void deleteSite(final S site) { + sites.remove( site ); + } + + @Nonnull + @Override + public Collection getSites() { + return Collections.unmodifiableCollection( sites ); + } + + @Nonnull + @Override + public Collection findSites(final String query) { + ImmutableList.Builder results = ImmutableList.builder(); + for (final S site : getSites()) + if (site.getName().startsWith( query )) + results.add( site ); + + return results.build(); + } + + @Override + public int hashCode() { + return Objects.hashCode( getFullName() ); + } + + @Override + public boolean equals(final Object obj) { + return (this == obj) || ((obj instanceof MPUser) && Objects.equals( getFullName(), ((MPUser) obj).getFullName() )); + } + + @Override + public int compareTo(final MPUser o) { + return getFullName().compareTo( o.getFullName() ); + } + + @Override + public String toString() { + return strf( "{%s: %s}", getClass().getSimpleName(), getFullName() ); + } +} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileQuestion.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileQuestion.java similarity index 64% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileQuestion.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileQuestion.java index 9aff3cb8..8e2da324 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileQuestion.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileQuestion.java @@ -16,69 +16,52 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse; import com.lyndir.masterpassword.*; +import javax.annotation.Nonnull; import javax.annotation.Nullable; /** * @author lhunath, 2018-05-14 */ -public class MPFileQuestion extends MPQuestion { +public class MPFileQuestion extends MPBasicQuestion { - private final MPSite site; + private final MPFileSite site; - private String keyword; @Nullable private String state; - private MPResultType type; - public MPFileQuestion(final MPSite site, final String keyword, @Nullable final String state, @Nullable final MPResultType type) { + public MPFileQuestion(final MPFileSite site, final String keyword, + @Nullable final MPResultType type, @Nullable final String state) { + super( keyword, ifNotNullElse( type, site.getAlgorithm().mpw_default_answer_type() ) ); + this.site = site; - this.keyword = keyword; this.state = state; - this.type = ifNotNullElse( type, site.getAlgorithm().mpw_default_answer_type() ); - } - - @Override - public MPSite getSite() { - return site; - } - - @Override - public String getKeyword() { - return keyword; - } - - public void setKeyword(final String keyword) { - this.keyword = keyword; - } - - public void setAnswer(final MPResultType type, @Nullable final String answer) - throws MPKeyUnavailableException { - this.type = type; - - if (answer == null) - this.state = null; - else - this.state = getSite().getState( - MPKeyPurpose.Recovery, getKeyword(), null, type, answer ); - } - - @Override - public MPResultType getType() { - return type; - } - - public void setType(final MPResultType type) { - this.type = type; } public String getAnswer() throws MPKeyUnavailableException { return getAnswer( state ); } + + public void setAnswer(final MPResultType type, @Nullable final String answer) + throws MPKeyUnavailableException { + setType( type ); + + if (answer == null) + this.state = null; + else + this.state = getSite().getState( + MPKeyPurpose.Recovery, getKeyword(), null, getType(), answer ); + } + + @Nonnull + @Override + public MPFileSite getSite() { + return site; + } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java new file mode 100644 index 00000000..26a54786 --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java @@ -0,0 +1,150 @@ +//============================================================================== +// This file is part of Master Password. +// Copyright (c) 2011-2017, Maarten Billemont. +// +// Master Password is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Master Password is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You can find a copy of the GNU General Public License in the +// LICENSE file. Alternatively, see . +//============================================================================== + +package com.lyndir.masterpassword.model.impl; + +import com.google.common.primitives.UnsignedInteger; +import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.MPUser; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.joda.time.Instant; +import org.joda.time.ReadableInstant; + + +/** + * @author lhunath, 14-12-05 + */ +public class MPFileSite extends MPBasicSite { + + private final MPFileUser user; + + @Nullable + private String url; + private int uses; + private ReadableInstant lastUsed; + + @Nullable + private String resultState; + @Nullable + private String loginState; + + public MPFileSite(final MPFileUser user, final String name) { + this( user, name, null, null, null ); + } + + public MPFileSite(final MPFileUser user, final String name, + @Nullable final MPAlgorithm algorithm, @Nullable final UnsignedInteger counter, + @Nullable final MPResultType resultType) { + this( user, name, algorithm, counter, resultType, null, + null, null, null, 0, new Instant() ); + } + + protected MPFileSite(final MPFileUser user, final String name, + @Nullable final MPAlgorithm algorithm, @Nullable final UnsignedInteger counter, + @Nullable final MPResultType resultType, @Nullable final String resultState, + @Nullable final MPResultType loginType, @Nullable final String loginState, + @Nullable final String url, final int uses, final ReadableInstant lastUsed) { + super( name, (algorithm == null)? user.getAlgorithm(): algorithm, counter, resultType, loginType ); + + this.user = user; + this.resultState = resultState; + this.loginState = loginState; + this.url = url; + this.uses = uses; + this.lastUsed = lastUsed; + } + + @Nullable + public String getUrl() { + return url; + } + + public void setUrl(@Nullable final String url) { + this.url = url; + } + + public int getUses() { + return uses; + } + + public ReadableInstant getLastUsed() { + return lastUsed; + } + + public void use() { + uses++; + lastUsed = new Instant(); + user.use(); + } + + public String getResult() + throws MPKeyUnavailableException { + + return getResult( MPKeyPurpose.Authentication, null ); + } + + public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext) + throws MPKeyUnavailableException { + + return getResult( keyPurpose, keyContext, getResultState() ); + } + + public String getLogin() + throws MPKeyUnavailableException { + + return getLogin( getLoginState() ); + } + + @Nullable + public String getResultState() { + return resultState; + } + + public void setSitePassword(final MPResultType resultType, @Nullable final String password) + throws MPKeyUnavailableException { + setResultType( resultType ); + + if (password == null) + this.resultState = null; + else + this.resultState = getState( + MPKeyPurpose.Authentication, null, getCounter(), getResultType(), password ); + } + + @Nullable + public String getLoginState() { + return loginState; + } + + public void setLoginName(@Nonnull final MPResultType loginType, @Nullable final String loginName) + throws MPKeyUnavailableException { + setLoginType( loginType ); + + if (loginName == null) + this.loginState = null; + else + this.loginState = getState( + MPKeyPurpose.Identification, null, null, getLoginType(), loginName ); + } + + @Override + public MPUser getUser() { + return user; + } +} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUser.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java similarity index 54% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUser.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java index 6aaddfbd..16706110 100755 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUser.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java @@ -16,13 +16,12 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Sets; import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.masterpassword.*; -import java.util.*; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; +import com.lyndir.masterpassword.model.MPUser; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.joda.time.Instant; @@ -33,21 +32,16 @@ import org.joda.time.ReadableInstant; * @author lhunath, 14-12-07 */ @SuppressWarnings("ComparableImplementedButEqualsNotOverridden") -public class MPFileUser extends MPUser implements Comparable { +public class MPFileUser extends MPBasicUser { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MPFileUser.class ); - private final String fullName; - private final Collection sites = new LinkedHashSet<>(); - @Nullable private byte[] keyID; - private MPAlgorithm algorithm; private MPMarshalFormat format; private MPMarshaller.ContentMode contentMode; - private int avatar; private MPResultType defaultType; private ReadableInstant lastUsed; @@ -59,40 +53,43 @@ public class MPFileUser extends MPUser implements Comparable implements Comparable implements Comparable getSites() { - return Collections.unmodifiableCollection( sites ); - } - - @Override - public void addSite(final MPFileSite site) { - sites.add( site ); - } - - @Override - public void deleteSite(final MPFileSite site) { - sites.remove( site ); - } - - @Override - public Collection findSites(final String query) { - ImmutableList.Builder results = ImmutableList.builder(); - for (final MPFileSite site : getSites()) - if (site.getSiteName().startsWith( query )) - results.add( site ); - - return results.build(); - } - public void setJSON(final MPJSONFile json) { this.json = json; } @@ -169,33 +133,13 @@ public class MPFileUser extends MPUser implements Comparable implements Comparable o) { + int comparison = (o instanceof MPFileUser)? getLastUsed().compareTo( ((MPFileUser) o).getLastUsed() ): 0; + if (comparison != 0) + return comparison; - return comparison; + return super.compareTo( o ); } } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUserManager.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUserManager.java similarity index 95% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUserManager.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUserManager.java index 9de7b6e9..52a2539a 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFileUserManager.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUserManager.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; @@ -25,6 +25,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.io.CharSink; import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.*; import java.io.*; import java.util.HashMap; import java.util.Map; @@ -37,7 +38,7 @@ import javax.annotation.Nonnull; * @author lhunath, 14-12-07 */ @SuppressWarnings("CallToSystemGetenv") -public class MPFileUserManager extends MPUserManager { +public class MPFileUserManager extends MPUserManager { @SuppressWarnings("UnusedDeclaration") private static final Logger logger = Logger.get( MPFileUserManager.class ); @@ -137,7 +138,7 @@ public class MPFileUserManager extends MPUserManager { } @Nonnull - private File getUserFile(final MPFileUser user, final MPMarshalFormat format) { + private File getUserFile(final MPUser user, final MPMarshalFormat format) { return new File( path, user.getFullName() + format.fileSuffix() ); } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatMarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java similarity index 94% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatMarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java index 794f6dd3..876872aa 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatMarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatMarshaller.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; import static com.lyndir.lhunath.opal.system.util.StringUtils.*; @@ -59,7 +59,7 @@ public class MPFlatMarshaller implements MPMarshaller { for (final MPFileSite site : user.getSites()) { String loginName = site.getLoginState(); - String password = site.getSiteState(); + String password = site.getResultState(); if (!user.getContentMode().isRedacted()) { loginName = site.getLogin(); password = site.getResult(); @@ -71,9 +71,9 @@ public class MPFlatMarshaller implements MPMarshaller { strf( "%d:%d:%d", // site.getResultType().getType(), // type site.getAlgorithm().version().toInt(), // algorithm - site.getSiteCounter().intValue() ), // counter + site.getCounter().intValue() ), // counter ifNotNullElse( loginName, "" ), // loginName - site.getSiteName(), // siteName + site.getName(), // siteName ifNotNullElse( password, "" ) // password ) ); } diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatUnmarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java similarity index 94% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatUnmarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java index d0760467..6b87a47c 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPFlatUnmarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import com.google.common.base.*; import com.google.common.io.CharStreams; @@ -25,6 +25,7 @@ import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.lhunath.opal.system.util.ConversionUtils; import com.lyndir.masterpassword.*; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -115,11 +116,11 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { switch (importFormat) { case 0: site = new MPFileSite( user, // - siteMatcher.group( 5 ), clearContent? null: siteMatcher.group( 6 ), + siteMatcher.group( 5 ), MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN( + colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), user.getAlgorithm().mpw_default_counter(), MPResultType.forType( ConversionUtils.toIntegerNN( siteMatcher.group( 3 ) ) ), - MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN( - colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), + clearContent? null: siteMatcher.group( 6 ), null, null, null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); if (clearContent) @@ -128,12 +129,12 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { case 1: site = new MPFileSite( user, // - siteMatcher.group( 7 ), clearContent? null: siteMatcher.group( 8 ), + siteMatcher.group( 7 ), MPMasterKey.Version.fromInt( ConversionUtils.toIntegerNN( + colon.matcher( siteMatcher.group( 4 ) ).replaceAll( "" ) ) ).getAlgorithm(), 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(), - clearContent? null: siteMatcher.group( 6 ), MPResultType.GeneratedName, null, + clearContent? null: siteMatcher.group( 8 ), + MPResultType.GeneratedName, clearContent? null: siteMatcher.group( 6 ), null, ConversionUtils.toIntegerNN( siteMatcher.group( 2 ) ), MPConstant.dateTimeFormatter.parseDateTime( siteMatcher.group( 1 ) ).toInstant() ); if (clearContent) { diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONAnyObject.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONAnyObject.java similarity index 96% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONAnyObject.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONAnyObject.java index 50385e88..03082051 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONAnyObject.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONAnyObject.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONFile.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java similarity index 89% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONFile.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java index 97cd8db7..b0863e71 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONFile.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java @@ -16,20 +16,16 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; import com.fasterxml.jackson.annotation.*; -import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.google.common.primitives.UnsignedInteger; import com.lyndir.lhunath.opal.system.CodeUtils; import com.lyndir.masterpassword.*; -import java.io.IOException; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; import java.util.*; import javax.annotation.Nullable; import org.joda.time.Instant; @@ -62,7 +58,7 @@ public class MPJSONFile extends MPJSONAnyObject { user.avatar = modelUser.getAvatar(); user.full_name = modelUser.getFullName(); user.last_used = MPConstant.dateTimeFormatter.print( modelUser.getLastUsed() ); - user.key_id = CodeUtils.encodeHex( modelUser.getKeyID() ); + user.key_id = modelUser.exportKeyID(); user.algorithm = modelUser.getAlgorithm().version(); user.default_type = modelUser.getDefaultType(); @@ -75,21 +71,21 @@ public class MPJSONFile extends MPJSONAnyObject { // Clear Text content = modelSite.getResult(); loginContent = modelUser.getMasterKey().siteResult( - modelSite.getSiteName(), modelSite.getAlgorithm().mpw_default_counter(), + modelSite.getName(), modelSite.getAlgorithm().mpw_default_counter(), MPKeyPurpose.Identification, null, modelSite.getLoginType(), modelSite.getLoginState(), modelSite.getAlgorithm() ); } else { // Redacted if (modelSite.getResultType().supportsTypeFeature( MPSiteFeature.ExportContent )) - content = modelSite.getSiteState(); + content = modelSite.getResultState(); if (modelSite.getLoginType().supportsTypeFeature( MPSiteFeature.ExportContent )) loginContent = modelSite.getLoginState(); } - Site site = sites.get( modelSite.getSiteName() ); + Site site = sites.get( modelSite.getName() ); if (site == null) - sites.put( modelSite.getSiteName(), site = new Site() ); + sites.put( modelSite.getName(), site = new Site() ); site.type = modelSite.getResultType(); - site.counter = modelSite.getSiteCounter().longValue(); + site.counter = modelSite.getCounter().longValue(); site.algorithm = modelSite.getAlgorithm().version(); site.password = content; site.login_name = loginContent; @@ -140,7 +136,7 @@ public class MPJSONFile extends MPJSONAnyObject { MPAlgorithm algorithm = ifNotNullElse( user.algorithm, MPMasterKey.Version.CURRENT ).getAlgorithm(); MPFileUser model = new MPFileUser( user.full_name, CodeUtils.decodeHex( user.key_id ), algorithm, user.avatar, - (user.default_type != null)? user.default_type: algorithm.mpw_default_password_type(), + (user.default_type != null)? user.default_type: algorithm.mpw_default_result_type(), (user.last_used != null)? MPConstant.dateTimeFormatter.parseDateTime( user.last_used ): new Instant(), MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE ); model.setJSON( this ); @@ -151,9 +147,9 @@ public class MPJSONFile extends MPJSONAnyObject { String siteName = siteEntry.getKey(); Site fileSite = siteEntry.getValue(); MPFileSite site = new MPFileSite( - model, siteName, export.redacted? fileSite.password: null, UnsignedInteger.valueOf( fileSite.counter ), - fileSite.type, fileSite.algorithm.getAlgorithm(), - export.redacted? fileSite.login_name: null, fileSite.login_type, + model, 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)? MPConstant.dateTimeFormatter.parseDateTime( fileSite.last_used ): new Instant() ); diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONMarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java similarity index 92% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONMarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java index 2c5fd380..de8dee2b 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONMarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONMarshaller.java @@ -16,9 +16,9 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; -import static com.lyndir.masterpassword.model.MPJSONFile.objectMapper; +import static com.lyndir.masterpassword.model.impl.MPJSONFile.objectMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.lyndir.masterpassword.MPKeyUnavailableException; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONUnmarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java similarity index 93% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONUnmarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java index 28dcc53e..6e31d06a 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPJSONUnmarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONUnmarshaller.java @@ -16,13 +16,14 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; -import static com.lyndir.masterpassword.model.MPJSONFile.objectMapper; +import static com.lyndir.masterpassword.model.impl.MPJSONFile.objectMapper; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.lyndir.masterpassword.MPKeyUnavailableException; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; import java.io.File; import java.io.IOException; import javax.annotation.Nonnull; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalException.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalException.java similarity index 96% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalException.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalException.java index 08a9570f..bf16f60a 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalException.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalException.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; /** * @author lhunath, 2018-04-26 diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalFormat.java similarity index 97% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalFormat.java index 14060751..6a1a7381 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshalFormat.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; /** * @author lhunath, 2017-09-20 diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshaller.java similarity index 97% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshaller.java index 4b954bf8..7f6c84cc 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPMarshaller.java @@ -16,7 +16,7 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import com.lyndir.masterpassword.MPKeyUnavailableException; import javax.annotation.Nonnull; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUnmarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPUnmarshaller.java similarity index 92% rename from core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUnmarshaller.java rename to core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPUnmarshaller.java index d07555eb..033e0b23 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPUnmarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPUnmarshaller.java @@ -16,9 +16,10 @@ // LICENSE file. Alternatively, see . //============================================================================== -package com.lyndir.masterpassword.model; +package com.lyndir.masterpassword.model.impl; import com.lyndir.masterpassword.MPKeyUnavailableException; +import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; import java.io.File; import java.io.IOException; import javax.annotation.Nonnull; diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/package-info.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/package-info.java new file mode 100644 index 00000000..dccaad7d --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/impl/package-info.java @@ -0,0 +1,25 @@ +//============================================================================== +// This file is part of Master Password. +// Copyright (c) 2011-2017, Maarten Billemont. +// +// Master Password is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Master Password is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You can find a copy of the GNU General Public License in the +// LICENSE file. Alternatively, see . +//============================================================================== + +/** + * @author lhunath, 2018-05-14 + */ +@ParametersAreNonnullByDefault +package com.lyndir.masterpassword.model.impl; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java b/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java index 5cbc7eb2..746bb0eb 100644 --- a/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java +++ b/platform-android/src/main/java/com/lyndir/masterpassword/Preferences.java @@ -148,7 +148,7 @@ public final class Preferences { @Nonnull public MPResultType getDefaultResultType() { - return MPResultType.values()[prefs().getInt( PREF_RESULT_TYPE, getDefaultVersion().getAlgorithm().mpw_default_password_type().ordinal() )]; + return MPResultType.values()[prefs().getInt( PREF_RESULT_TYPE, getDefaultVersion().getAlgorithm().mpw_default_result_type().ordinal() )]; } public boolean setDefaultVersion(final MPMasterKey.Version value) { diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java index 0f381cee..f4dbe7b8 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoSite.java @@ -18,97 +18,32 @@ package com.lyndir.masterpassword.gui.model; -import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse; - -import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; -import com.google.common.primitives.UnsignedInteger; -import com.lyndir.masterpassword.MPAlgorithm; -import com.lyndir.masterpassword.MPResultType; import com.lyndir.masterpassword.model.*; +import com.lyndir.masterpassword.model.impl.MPBasicSite; import java.util.Collection; -import javax.annotation.Nullable; /** * @author lhunath, 14-12-16 */ -public class IncognitoSite extends MPSite { +public class IncognitoSite extends MPBasicSite { private final IncognitoUser user; - private String siteName; - private UnsignedInteger siteCounter; - private MPResultType resultType; - private MPResultType loginType; - private MPAlgorithm algorithm; + public IncognitoSite(final IncognitoUser user, final String siteName) { + super( siteName, user.getAlgorithm() ); - public IncognitoSite(final IncognitoUser user, final String siteName, final UnsignedInteger siteCounter, final MPResultType resultType, - final MPAlgorithm algorithm) { this.user = user; - this.siteName = siteName; - this.siteCounter = siteCounter; - this.resultType = resultType; - this.algorithm = algorithm; } @Override - public MPUser getUser() { + public MPUser getUser() { return user; } - @Override - public String getSiteName() { - return siteName; - } - - @Override - public void setSiteName(final String siteName) { - this.siteName = siteName; - } - - @Override - public MPResultType getResultType() { - return resultType; - } - - @Override - public void setResultType(final MPResultType resultType) { - 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; - } - - @Override - public void setAlgorithm(final MPAlgorithm algorithm) { - this.algorithm = algorithm; - } - @Override public Collection getQuestions() { return ImmutableList.of(); } - - @Override - public UnsignedInteger getSiteCounter() { - return siteCounter; - } - - @Override - public void setSiteCounter(final UnsignedInteger siteCounter) { - this.siteCounter = siteCounter; - } } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java index 8b7eccfe..e6b52b3c 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/model/IncognitoUser.java @@ -18,53 +18,23 @@ package com.lyndir.masterpassword.gui.model; -import com.google.common.collect.ImmutableList; -import com.lyndir.masterpassword.MPAlgorithm; import com.lyndir.masterpassword.MPMasterKey; -import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; -import com.lyndir.masterpassword.model.MPUser; -import java.util.Collection; -import javax.annotation.Nonnull; +import com.lyndir.masterpassword.model.impl.MPBasicUser; +import javax.annotation.Nullable; /** * @author lhunath, 2014-06-08 */ -public class IncognitoUser extends MPUser { - - private final String fullName; +public class IncognitoUser extends MPBasicUser { public IncognitoUser(final String fullName) { - this.fullName = fullName; + super(fullName, MPMasterKey.Version.CURRENT.getAlgorithm()); } + @Nullable @Override - public String getFullName() { - return fullName; - } - - @Override - public MPAlgorithm getAlgorithm() { - return MPMasterKey.Version.CURRENT.getAlgorithm(); - } - - @Override - public void addSite(final IncognitoSite site) { - } - - @Override - public void deleteSite(final IncognitoSite site) { - } - - @Override - public Collection findSites(final String query) { - return ImmutableList.of(); - } - - @Nonnull - @Override - public MPMasterKey authenticate(final char[] masterPassword) - throws MPIncorrectMasterPasswordException { - return key = new MPMasterKey( getFullName(), masterPassword ); + public byte[] getKeyID() { + return null; } } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java index 146b94a9..5f88ea39 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/ModelAuthenticationPanel.java @@ -29,6 +29,8 @@ import com.lyndir.masterpassword.MPResultType; import com.lyndir.masterpassword.gui.Res; import com.lyndir.masterpassword.gui.util.Components; import com.lyndir.masterpassword.model.*; +import com.lyndir.masterpassword.model.impl.*; +import com.lyndir.masterpassword.model.impl.MPFileSite; import java.awt.*; import java.awt.event.*; import javax.annotation.Nonnull; @@ -214,7 +216,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel im protected MPFileSite createSite(final MPFileUser user, final String siteName, final UnsignedInteger siteCounter, final MPResultType resultType, final MPAlgorithm algorithm) { - return new MPFileSite( user, siteName, siteCounter, resultType, algorithm ); + return new MPFileSite( user, siteName, algorithm, siteCounter, resultType ); } }; } diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java index 8ce8d883..63bea042 100755 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/PasswordFrame.java @@ -31,6 +31,9 @@ import com.lyndir.masterpassword.gui.Res; import com.lyndir.masterpassword.gui.util.Components; import com.lyndir.masterpassword.gui.util.UnsignedIntegerModel; import com.lyndir.masterpassword.model.*; +import com.lyndir.masterpassword.model.impl.MPBasicSite; +import com.lyndir.masterpassword.model.impl.MPFileSite; +import com.lyndir.masterpassword.model.impl.MPFileUser; import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; @@ -45,7 +48,7 @@ import javax.swing.event.*; /** * @author lhunath, 2014-06-08 */ -public abstract class PasswordFrame, S extends MPSite> extends JFrame implements DocumentListener { +public abstract class PasswordFrame, S extends MPBasicSite> extends JFrame implements DocumentListener { @SuppressWarnings("FieldCanBeLocal") private final Components.GradientPanel root; @@ -147,7 +150,7 @@ public abstract class PasswordFrame, S extends MPSite> exten siteCounterField = Components.spinner( siteCounterModel ) ); sitePanel.add( siteSettings ); resultTypeField.setFont( Res.valueFont().deriveFont( resultTypeField.getFont().getSize2D() ) ); - resultTypeField.setSelectedItem( user.getAlgorithm().mpw_default_password_type() ); + resultTypeField.setSelectedItem( user.getAlgorithm().mpw_default_result_type() ); resultTypeField.addItemListener( new ItemListener() { @Override public void itemStateChanged(final ItemEvent e) { @@ -245,15 +248,15 @@ public abstract class PasswordFrame, S extends MPSite> exten siteResults = FluentIterable.from( siteResults ).filter( new Predicate() { @Override public boolean apply(@Nullable final S siteResult) { - return (siteResult != null) && siteNameQuery.equals( siteResult.getSiteName() ); + return (siteResult != null) && siteNameQuery.equals( siteResult.getName() ); } } ); final S site = ifNotNullElse( Iterables.getFirst( siteResults, null ), createSite( user, siteNameQuery, siteCounter, resultType, siteAlgorithm ) ); - if ((currentSite != null) && currentSite.getSiteName().equals( site.getSiteName() )) { + if ((currentSite != null) && currentSite.getName().equals( site.getName() )) { site.setResultType( resultType ); site.setAlgorithm( siteAlgorithm ); - site.setSiteCounter( siteCounter ); + site.setCounter( siteCounter ); } ListenableFuture passwordFuture = Res.execute( this, new Callable() { @@ -278,8 +281,8 @@ public abstract class PasswordFrame, S extends MPSite> exten siteActionButton.setText( "Add Site" ); resultTypeField.setSelectedItem( currentSite.getResultType() ); siteVersionField.setSelectedItem( currentSite.getAlgorithm() ); - siteCounterField.setValue( currentSite.getSiteCounter() ); - siteNameField.setText( currentSite.getSiteName() ); + siteCounterField.setValue( currentSite.getCounter() ); + siteNameField.setText( currentSite.getName() ); if (siteNameField.getText().startsWith( siteNameQuery )) siteNameField.select( siteNameQuery.length(), siteNameField.getText().length() ); diff --git a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java index f6a10481..b0175500 100644 --- a/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java +++ b/platform-independent/gui-java/src/main/java/com/lyndir/masterpassword/gui/view/UnlockFrame.java @@ -24,8 +24,7 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import com.lyndir.masterpassword.MPIdenticon; import com.lyndir.masterpassword.gui.Res; import com.lyndir.masterpassword.gui.util.Components; -import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException; -import com.lyndir.masterpassword.model.MPUser; +import com.lyndir.masterpassword.model.*; import java.awt.*; import java.awt.event.*; import java.util.concurrent.Future; @@ -49,7 +48,7 @@ public class UnlockFrame extends JFrame { private Future identiconFuture; private boolean incognito; @Nullable - private MPUser user; + private MPUser user; public UnlockFrame(final SignInCallback signInCallback) { super( "Unlock Master Password" ); @@ -158,7 +157,7 @@ public class UnlockFrame extends JFrame { } ); } - void updateUser(@Nullable final MPUser user) { + void updateUser(@Nullable final MPUser user) { this.user = user; checkSignIn(); }