From fe63a2756a8d1352059be8fb60ce2c55d95d9b0e Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sun, 29 Jul 2018 15:10:45 -0400 Subject: [PATCH] Fix default type for new sites & site UI updating. --- .../lyndir/masterpassword/MPAlgorithm.java | 14 +++++++++++ .../masterpassword/impl/MPAlgorithmV0.java | 14 +++++++++++ .../masterpassword/impl/MPAlgorithmV1.java | 2 ++ .../masterpassword/impl/MPAlgorithmV2.java | 2 ++ .../masterpassword/impl/MPAlgorithmV3.java | 2 ++ .../masterpassword/gui/model/MPNewSite.java | 2 +- .../gui/view/UserContentPanel.java | 15 ++++------- .../lyndir/masterpassword/model/MPUser.java | 5 ++++ .../model/impl/MPBasicSite.java | 25 ++++++++++--------- .../masterpassword/model/impl/MPFileSite.java | 13 +++------- .../masterpassword/model/impl/MPFileUser.java | 1 + 11 files changed, 62 insertions(+), 33 deletions(-) diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java index 0e87ee8e..cb0051a7 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/MPAlgorithm.java @@ -28,6 +28,7 @@ import com.lyndir.lhunath.opal.system.MessageDigests; import com.lyndir.masterpassword.impl.*; import java.nio.ByteOrder; import java.nio.charset.Charset; +import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -88,46 +89,55 @@ public abstract class MPAlgorithm { /** * The linear version identifier of this algorithm's implementation. */ + @Nonnull public abstract Version version(); /** * mpw: defaults: initial counter value. */ + @Nonnull public abstract UnsignedInteger mpw_default_counter(); /** * mpw: defaults: password result type. */ + @Nonnull public abstract MPResultType mpw_default_result_type(); /** * mpw: defaults: login result type. */ + @Nonnull public abstract MPResultType mpw_default_login_type(); /** * mpw: defaults: answer result type. */ + @Nonnull public abstract MPResultType mpw_default_answer_type(); /** * mpw: Input character encoding. */ + @Nonnull public abstract Charset mpw_charset(); /** * mpw: Platform-agnostic byte order. */ + @Nonnull public abstract ByteOrder mpw_byteOrder(); /** * mpw: Key ID hash. */ + @Nonnull public abstract MessageDigests mpw_hash(); /** * mpw: Site digest. */ + @Nonnull public abstract MessageAuthenticationDigests mpw_digest(); /** @@ -167,12 +177,16 @@ public abstract class MPAlgorithm { // Utilities + @Nonnull protected abstract byte[] toBytes(int number); + @Nonnull protected abstract byte[] toBytes(UnsignedInteger number); + @Nonnull protected abstract byte[] toBytes(char[] characters); + @Nonnull protected abstract byte[] toID(byte[] bytes); @Override diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV0.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV0.java index 946b3e74..f1afaf37 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV0.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV0.java @@ -27,6 +27,7 @@ import com.lyndir.masterpassword.*; import java.nio.*; import java.nio.charset.*; import java.util.Arrays; +import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -122,46 +123,55 @@ public class MPAlgorithmV0 extends MPAlgorithm { // Configuration + @Nonnull @Override public Version version() { return MPAlgorithm.Version.V0; } + @Nonnull @Override public UnsignedInteger mpw_default_counter() { return UnsignedInteger.ONE; } + @Nonnull @Override public MPResultType mpw_default_result_type() { return MPResultType.GeneratedLong; } + @Nonnull @Override public MPResultType mpw_default_login_type() { return MPResultType.GeneratedName; } + @Nonnull @Override public MPResultType mpw_default_answer_type() { return MPResultType.GeneratedPhrase; } + @Nonnull @Override public Charset mpw_charset() { return Charsets.UTF_8; } + @Nonnull @Override public ByteOrder mpw_byteOrder() { return ByteOrder.BIG_ENDIAN; } + @Nonnull @Override public MessageDigests mpw_hash() { return MessageDigests.SHA256; } + @Nonnull @Override public MessageAuthenticationDigests mpw_digest() { return MessageAuthenticationDigests.HmacSHA256; @@ -211,16 +221,19 @@ public class MPAlgorithmV0 extends MPAlgorithm { // Utilities + @Nonnull @Override public byte[] toBytes(final int number) { return ByteBuffer.allocate( Integer.SIZE / Byte.SIZE ).order( mpw_byteOrder() ).putInt( number ).array(); } + @Nonnull @Override public byte[] toBytes(final UnsignedInteger number) { return ByteBuffer.allocate( Integer.SIZE / Byte.SIZE ).order( mpw_byteOrder() ).putInt( number.intValue() ).array(); } + @Nonnull @Override public byte[] toBytes(final char[] characters) { ByteBuffer byteBuffer = mpw_charset().encode( CharBuffer.wrap( characters ) ); @@ -232,6 +245,7 @@ public class MPAlgorithmV0 extends MPAlgorithm { return bytes; } + @Nonnull @Override public byte[] toID(final byte[] bytes) { return mpw_hash().of( bytes ); diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV1.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV1.java index f5064eb4..81c1a12b 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV1.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV1.java @@ -19,6 +19,7 @@ package com.lyndir.masterpassword.impl; import com.lyndir.masterpassword.*; +import javax.annotation.Nonnull; /** @@ -29,6 +30,7 @@ public class MPAlgorithmV1 extends MPAlgorithmV0 { // Configuration + @Nonnull @Override public Version version() { return MPAlgorithm.Version.V1; diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV2.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV2.java index 35a70333..dc6c77f0 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV2.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV2.java @@ -19,6 +19,7 @@ package com.lyndir.masterpassword.impl; import com.lyndir.masterpassword.MPAlgorithm; +import javax.annotation.Nonnull; /** @@ -29,6 +30,7 @@ public class MPAlgorithmV2 extends MPAlgorithmV1 { // Configuration + @Nonnull @Override public Version version() { return MPAlgorithm.Version.V2; diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV3.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV3.java index a5e9dafa..2891980e 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV3.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/MPAlgorithmV3.java @@ -19,6 +19,7 @@ package com.lyndir.masterpassword.impl; import com.lyndir.masterpassword.MPAlgorithm; +import javax.annotation.Nonnull; /** @@ -29,6 +30,7 @@ public class MPAlgorithmV3 extends MPAlgorithmV2 { // Configuration + @Nonnull @Override public Version version() { return MPAlgorithm.Version.V3; diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/model/MPNewSite.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/model/MPNewSite.java index 8cc864d7..0f0396ed 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/model/MPNewSite.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/model/MPNewSite.java @@ -10,6 +10,6 @@ import com.lyndir.masterpassword.model.impl.*; public class MPNewSite extends MPBasicSite, MPQuestion> { public MPNewSite(final MPUser user, final String siteName) { - super( user, siteName, user.getAlgorithm() ); + super( user, siteName ); } } diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java index aa064d7e..4629f7a2 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/view/UserContentPanel.java @@ -472,16 +472,11 @@ public class UserContentPanel extends JPanel implements MasterPassword.Listener, sitesModel.registerList( sitesList ); add( Box.createGlue() ); - addComponentListener( new ComponentAdapter() { - @Override - public void componentShown(final ComponentEvent e) { - user.addListener( AuthenticatedUserPanel.this ); - } - - @Override - public void componentHidden(final ComponentEvent e) { - user.removeListener( AuthenticatedUserPanel.this ); - } + addHierarchyListener( e -> { + if (null != SwingUtilities.windowForComponent( this )) + user.addListener( this ); + else + user.removeListener( this ); } ); } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java index f8242940..834dae1e 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/MPUser.java @@ -46,6 +46,11 @@ public interface MPUser> extends Comparable> { void setAlgorithm(MPAlgorithm algorithm); + @Nullable + default MPResultType getDefaultType() { + return null; + } + @Nullable byte[] getKeyID(); diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java index f4face78..8a7d9b68 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPBasicSite.java @@ -44,19 +44,20 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext private MPResultType resultType; private MPResultType loginType; - protected MPBasicSite(final U user, final String siteName, final MPAlgorithm algorithm) { - this( user, siteName, algorithm, null, null, null ); + protected MPBasicSite(final U user, final String siteName) { + this( user, siteName, null, null, null, null ); } - protected MPBasicSite(final U user, final String siteName, final MPAlgorithm algorithm, - @Nullable final UnsignedInteger counter, + protected MPBasicSite(final U user, final String siteName, + @Nullable final MPAlgorithm algorithm, @Nullable final UnsignedInteger counter, @Nullable final MPResultType resultType, @Nullable final MPResultType loginType) { this.user = user; this.siteName = siteName; - 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; + this.algorithm = (algorithm != null)? algorithm: this.user.getAlgorithm(); + this.counter = (counter != null)? counter: this.algorithm.mpw_default_counter(); + this.resultType = (resultType != null)? resultType: + ifNotNullElse( this.user.getDefaultType(), this.algorithm.mpw_default_result_type() ); + this.loginType = (loginType != null)? loginType: this.algorithm.mpw_default_login_type(); } @Nonnull @@ -73,7 +74,7 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext @Override public void setAlgorithm(final MPAlgorithm algorithm) { - if (Objects.equals(this.algorithm, algorithm)) + if (Objects.equals( this.algorithm, algorithm )) return; this.algorithm = algorithm; @@ -88,7 +89,7 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext @Override public void setCounter(final UnsignedInteger counter) { - if (Objects.equals(this.counter, counter)) + if (Objects.equals( this.counter, counter )) return; this.counter = counter; @@ -103,7 +104,7 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext @Override public void setResultType(final MPResultType resultType) { - if (Objects.equals(this.resultType, resultType)) + if (Objects.equals( this.resultType, resultType )) return; this.resultType = resultType; @@ -118,7 +119,7 @@ public abstract class MPBasicSite, Q extends MPQuestion> ext @Override public void setLoginType(@Nullable final MPResultType loginType) { - if (Objects.equals(this.loginType, loginType)) + if (Objects.equals( this.loginType, loginType )) return; this.loginType = ifNotNullElse( loginType, getAlgorithm().mpw_default_login_type() ); diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java index bb6200b1..8437e96a 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java @@ -45,14 +45,8 @@ public class MPFileSite extends MPBasicSite { 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() ); + this( user, name, null, null, null, null, null, null, + null, 0, new Instant() ); } protected MPFileSite(final MPFileUser user, final String name, @@ -60,8 +54,7 @@ public class MPFileSite extends MPBasicSite { @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( user, name, (algorithm == null)? user.getAlgorithm(): algorithm, counter, - (resultType == null)? user.getDefaultType(): resultType, loginType ); + super( user, name, algorithm, counter, resultType, loginType ); this.resultState = resultState; this.loginState = loginState; diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java index f8fd88a2..6e979561 100755 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java @@ -135,6 +135,7 @@ public class MPFileUser extends MPBasicUser { setChanged(); } + @Override public MPResultType getDefaultType() { return defaultType; }