2
0

Replace Version API with MPAlgorithm, make configuration instance-specific.

This commit is contained in:
Maarten Billemont
2018-04-26 15:56:12 -04:00
parent 33f2e0edda
commit 82e2d0b5ac
29 changed files with 397 additions and 226 deletions

View File

@@ -19,8 +19,7 @@
package com.lyndir.masterpassword.gui.model;
import com.google.common.primitives.UnsignedInteger;
import com.lyndir.masterpassword.MPMasterKey;
import com.lyndir.masterpassword.MPResultType;
import com.lyndir.masterpassword.*;
import com.lyndir.masterpassword.model.MPSite;
@@ -29,17 +28,17 @@ import com.lyndir.masterpassword.model.MPSite;
*/
public class IncognitoSite extends MPSite {
private String siteName;
private UnsignedInteger siteCounter;
private MPResultType resultType;
private MPMasterKey.Version algorithmVersion;
private String siteName;
private UnsignedInteger siteCounter;
private MPResultType resultType;
private MPAlgorithm algorithm;
public IncognitoSite(final String siteName, final UnsignedInteger siteCounter, final MPResultType resultType,
final MPMasterKey.Version algorithmVersion) {
final MPAlgorithm algorithm) {
this.siteName = siteName;
this.siteCounter = siteCounter;
this.resultType = resultType;
this.algorithmVersion = algorithmVersion;
this.algorithm = algorithm;
}
@Override
@@ -63,13 +62,13 @@ public class IncognitoSite extends MPSite {
}
@Override
public MPMasterKey.Version getAlgorithmVersion() {
return algorithmVersion;
public MPAlgorithm getAlgorithm() {
return algorithm;
}
@Override
public void setAlgorithmVersion(final MPMasterKey.Version algorithmVersion) {
this.algorithmVersion = algorithmVersion;
public void setAlgorithm(final MPAlgorithm algorithm) {
this.algorithm = algorithm;
}
@Override

View File

@@ -19,6 +19,7 @@
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;
@@ -43,8 +44,8 @@ public class IncognitoUser extends MPUser<IncognitoSite> {
}
@Override
public MPMasterKey.Version getAlgorithmVersion() {
return MPMasterKey.Version.CURRENT;
public MPAlgorithm getAlgorithm() {
return MPMasterKey.Version.CURRENT.getAlgorithm();
}
@Override

View File

@@ -31,6 +31,8 @@ import javax.swing.border.CompoundBorder;
*/
public abstract class Components {
public static final float CONTROL_TEXT_SIZE = 12f;
public static GradientPanel boxLayout(final int axis, final Component... components) {
GradientPanel container = gradientPanel( null, null );
// container.setBackground( Color.red );
@@ -73,7 +75,7 @@ public abstract class Components {
{
setBorder( BorderFactory.createCompoundBorder( BorderFactory.createLineBorder( Res.colors().controlBorder(), 1, true ),
BorderFactory.createEmptyBorder( 4, 4, 4, 4 ) ) );
setFont( Res.valueFont().deriveFont( 12f ) );
setFont( Res.valueFont().deriveFont( CONTROL_TEXT_SIZE ) );
setAlignmentX( LEFT_ALIGNMENT );
setAlignmentY( BOTTOM_ALIGNMENT );
}
@@ -104,7 +106,7 @@ public abstract class Components {
public static JButton button(final String label) {
return new JButton( label ) {
{
setFont( Res.controlFont().deriveFont( 12f ) );
setFont( Res.controlFont().deriveFont( CONTROL_TEXT_SIZE ) );
setAlignmentX( LEFT_ALIGNMENT );
setAlignmentY( BOTTOM_ALIGNMENT );
}
@@ -160,7 +162,7 @@ public abstract class Components {
public static JLabel label(@Nullable final String label, final int horizontalAlignment) {
return new JLabel( label, horizontalAlignment ) {
{
setFont( Res.controlFont().deriveFont( 12f ) );
setFont( Res.controlFont().deriveFont( CONTROL_TEXT_SIZE ) );
setAlignmentX( LEFT_ALIGNMENT );
setAlignmentY( BOTTOM_ALIGNMENT );
}
@@ -175,7 +177,7 @@ public abstract class Components {
public static JCheckBox checkBox(final String label) {
return new JCheckBox( label ) {
{
setFont( Res.controlFont().deriveFont( 12f ) );
setFont( Res.controlFont().deriveFont( CONTROL_TEXT_SIZE ) );
setBackground( null );
setAlignmentX( LEFT_ALIGNMENT );
setAlignmentY( BOTTOM_ALIGNMENT );
@@ -195,7 +197,7 @@ public abstract class Components {
// BorderFactory.createLineBorder( Res.colors().controlBorder(), 1, true ),
// BorderFactory.createEmptyBorder( 4, 4, 4, 4 ) );
// ((JComponent) ((BasicComboBoxEditor) getEditor()).getEditorComponent()).setBorder(editorBorder);
setFont( Res.controlFont().deriveFont( 12f ) );
setFont( Res.controlFont().deriveFont( CONTROL_TEXT_SIZE ) );
setAlignmentX( LEFT_ALIGNMENT );
setAlignmentY( BOTTOM_ALIGNMENT );
// setBorder(null);

View File

@@ -27,6 +27,8 @@ import javax.swing.*;
*/
public class UnsignedIntegerModel extends SpinnerNumberModel {
private static final long serialVersionUID = 1L;
public UnsignedIntegerModel() {
this( UnsignedInteger.ZERO, UnsignedInteger.ZERO, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE );
}
@@ -43,6 +45,7 @@ public class UnsignedIntegerModel extends SpinnerNumberModel {
this( value, minimum, maximum, UnsignedInteger.ONE );
}
@SuppressWarnings("TypeMayBeWeakened")
public UnsignedIntegerModel(final UnsignedInteger value, final UnsignedInteger minimum, final UnsignedInteger maximum,
final UnsignedInteger stepSize) {
super( value, minimum, maximum, stepSize );

View File

@@ -18,9 +18,9 @@
package com.lyndir.masterpassword.gui.view;
import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedInteger;
import com.lyndir.masterpassword.MPMasterKey;
import com.lyndir.masterpassword.MPResultType;
import com.lyndir.masterpassword.*;
import com.lyndir.masterpassword.gui.Res;
import com.lyndir.masterpassword.gui.model.IncognitoSite;
import com.lyndir.masterpassword.gui.model.IncognitoUser;
@@ -29,6 +29,7 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@@ -81,16 +82,17 @@ public class IncognitoAuthenticationPanel extends AuthenticationPanel<IncognitoU
@Override
public PasswordFrame<IncognitoUser, ?> newPasswordFrame() {
return new PasswordFrame<IncognitoUser, IncognitoSite>( getSelectedUser() ) {
return new PasswordFrame<IncognitoUser, IncognitoSite>( Preconditions.checkNotNull( getSelectedUser() ) ) {
@Override
protected IncognitoSite createSite(final IncognitoUser user, final String siteName, final UnsignedInteger siteCounter,
final MPResultType resultType,
final MPMasterKey.Version algorithmVersion) {
return new IncognitoSite( siteName, siteCounter, resultType, algorithmVersion );
final MPAlgorithm algorithm) {
return new IncognitoSite( siteName, siteCounter, resultType, algorithm );
}
};
}
@Nullable
@Override
protected IncognitoUser getSelectedUser() {
return new IncognitoUser( fullNameField.getText() );

View File

@@ -23,14 +23,14 @@ import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.UnsignedInteger;
import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.masterpassword.MPMasterKey;
import com.lyndir.masterpassword.MPResultType;
import com.lyndir.masterpassword.*;
import com.lyndir.masterpassword.gui.Res;
import com.lyndir.masterpassword.gui.util.Components;
import com.lyndir.masterpassword.model.*;
import java.awt.*;
import java.awt.event.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@@ -71,7 +71,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel<MPFileUser> im
add( userLabel );
userField = Components.comboBox( readConfigUsers() );
userField.setFont( Res.valueFont().deriveFont( 12f ) );
userField.setFont( Res.valueFont().deriveFont( userField.getFont().getSize2D() ) );
userField.addItemListener( this );
userField.addActionListener( this );
userField.setRenderer( new DefaultListCellRenderer() {
@@ -128,6 +128,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel<MPFileUser> im
super.updateUser( repack );
}
@Nullable
@Override
protected MPFileUser getSelectedUser() {
int selectedIndex = userField.getSelectedIndex();
@@ -210,8 +211,8 @@ public class ModelAuthenticationPanel extends AuthenticationPanel<MPFileUser> im
@Override
protected MPFileSite createSite(final MPFileUser user, final String siteName, final UnsignedInteger siteCounter,
final MPResultType resultType,
final MPMasterKey.Version algorithmVersion) {
return new MPFileSite( user, siteName, siteCounter, resultType, algorithmVersion );
final MPAlgorithm algorithm) {
return new MPFileSite( user, siteName, siteCounter, resultType, algorithm );
}
};
}

View File

@@ -33,6 +33,7 @@ import com.lyndir.masterpassword.gui.util.UnsignedIntegerModel;
import com.lyndir.masterpassword.model.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import java.util.concurrent.Callable;
import javax.annotation.Nonnull;
@@ -65,7 +66,8 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
private S currentSite;
private boolean updatingUI;
public PasswordFrame(final U user) {
@SuppressWarnings({ "MagicNumber", "OverridableMethodCallDuringObjectConstruction" })
protected PasswordFrame(final U user) {
super( "Master Password" );
this.user = user;
@@ -97,7 +99,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
Futures.addCallback( updatePassword( true ), new FutureCallback<String>() {
@Override
public void onSuccess(@Nullable final String sitePassword) {
StringSelection clipboardContents = new StringSelection( sitePassword );
Transferable clipboardContents = new StringSelection( sitePassword );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents( clipboardContents, null );
SwingUtilities.invokeLater( new Runnable() {
@@ -112,7 +114,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
}
@Override
public void onFailure(final Throwable t) {
public void onFailure(@Nonnull final Throwable t) {
}
} );
}
@@ -144,8 +146,8 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
Components.stud(), //
siteCounterField = Components.spinner( siteCounterModel ) );
sitePanel.add( siteSettings );
resultTypeField.setFont( Res.valueFont().deriveFont( 12f ) );
resultTypeField.setSelectedItem( MPAlgorithm.mpw_default_type );
resultTypeField.setFont( Res.valueFont().deriveFont( resultTypeField.getFont().getSize2D() ) );
resultTypeField.setSelectedItem( user.getAlgorithm().mpw_default_type() );
resultTypeField.addItemListener( new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
@@ -153,9 +155,9 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
}
} );
siteVersionField.setFont( Res.valueFont().deriveFont( 12f ) );
siteVersionField.setFont( Res.valueFont().deriveFont( siteVersionField.getFont().getSize2D() ) );
siteVersionField.setAlignmentX( RIGHT_ALIGNMENT );
siteVersionField.setSelectedItem( user.getAlgorithmVersion() );
siteVersionField.setSelectedItem( user.getAlgorithm() );
siteVersionField.addItemListener( new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
@@ -163,7 +165,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
}
} );
siteCounterField.setFont( Res.valueFont().deriveFont( 12f ) );
siteCounterField.setFont( Res.valueFont().deriveFont( siteCounterField.getFont().getSize2D() ) );
siteCounterField.setAlignmentX( RIGHT_ALIGNMENT );
siteCounterField.addChangeListener( new ChangeListener() {
@Override
@@ -186,7 +188,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
// Password
passwordField = Components.passwordField();
passwordField.setAlignmentX( Component.CENTER_ALIGNMENT );
passwordField.setHorizontalAlignment( JTextField.CENTER );
passwordField.setHorizontalAlignment( SwingConstants.CENTER );
passwordField.putClientProperty( "JPasswordField.cutCopyAllowed", true );
passwordField.setEditable( false );
passwordField.setBackground( null );
@@ -215,6 +217,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
setLocationRelativeTo( null );
}
@SuppressWarnings("MagicNumber")
private void updateMask() {
passwordField.setEchoChar( maskPasswordField.isSelected()? passwordEchoChar: (char) 0 );
passwordField.setFont( maskPasswordField.isSelected()? passwordEchoFont: Res.bigValueFont().deriveFont( 40f ) );
@@ -233,9 +236,9 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
return Futures.immediateCancelledFuture();
}
MPResultType resultType = resultTypeField.getModel().getElementAt( resultTypeField.getSelectedIndex() );
MPMasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() );
UnsignedInteger siteCounter = siteCounterModel.getNumber();
MPResultType resultType = resultTypeField.getModel().getElementAt( resultTypeField.getSelectedIndex() );
MPAlgorithm siteAlgorithm = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() ).getAlgorithm();
UnsignedInteger siteCounter = siteCounterModel.getNumber();
Iterable<S> siteResults = user.findSites( siteNameQuery );
if (!allowNameCompletion)
@@ -246,10 +249,10 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
}
} );
final S site = ifNotNullElse( Iterables.getFirst( siteResults, null ),
createSite( user, siteNameQuery, siteCounter, resultType, siteVersion ) );
createSite( user, siteNameQuery, siteCounter, resultType, siteAlgorithm ) );
if ((currentSite != null) && currentSite.getSiteName().equals( site.getSiteName() )) {
site.setResultType( resultType );
site.setAlgorithmVersion( siteVersion );
site.setAlgorithm( siteAlgorithm );
site.setSiteCounter( siteCounter );
}
@@ -259,7 +262,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
throws Exception {
return user.getMasterKey()
.siteResult( site.getSiteName(), site.getSiteCounter(), MPKeyPurpose.Authentication, null, site.getResultType(),
null, site.getAlgorithmVersion() );
null, site.getAlgorithm() );
}
} );
Futures.addCallback( passwordFuture, new FutureCallback<String>() {
@@ -276,7 +279,7 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
else
siteActionButton.setText( "Add Site" );
resultTypeField.setSelectedItem( currentSite.getResultType() );
siteVersionField.setSelectedItem( currentSite.getAlgorithmVersion() );
siteVersionField.setSelectedItem( currentSite.getAlgorithm() );
siteCounterField.setValue( currentSite.getSiteCounter() );
siteNameField.setText( currentSite.getSiteName() );
if (siteNameField.getText().startsWith( siteNameQuery ))
@@ -290,15 +293,14 @@ public abstract class PasswordFrame<U extends MPUser<S>, S extends MPSite> exten
}
@Override
public void onFailure(final Throwable t) {
public void onFailure(@Nonnull final Throwable t) {
}
} );
return passwordFuture;
}
protected abstract S createSite(U user, String siteName, UnsignedInteger siteCounter, MPResultType resultType,
MPMasterKey.Version algorithmVersion);
protected abstract S createSite(U user, String siteName, UnsignedInteger siteCounter, MPResultType resultType, MPAlgorithm algorithm);
@Override
public void insertUpdate(final DocumentEvent e) {

View File

@@ -21,10 +21,10 @@ package com.lyndir.masterpassword.gui.view;
import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*;
import com.lyndir.masterpassword.MPIdenticon;
import com.lyndir.masterpassword.gui.Res;
import com.lyndir.masterpassword.gui.*;
import com.lyndir.masterpassword.model.MPUser;
import com.lyndir.masterpassword.gui.util.Components;
import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException;
import com.lyndir.masterpassword.model.MPUser;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.Future;
@@ -196,7 +196,7 @@ public class UnlockFrame extends JFrame {
}
void trySignIn(final JComponent... signInComponents) {
if (!checkSignIn())
if ((user == null) || !checkSignIn())
return;
for (final JComponent signInComponent : signInComponents)

View File

@@ -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 <http://www.gnu.org/licenses/>.
//==============================================================================
/**
* @author lhunath, 2018-04-26
*/
@ParametersAreNonnullByDefault
package com.lyndir.masterpassword.gui.view;
import javax.annotation.ParametersAreNonnullByDefault;