Site settings & add sites.
This commit is contained in:
@@ -33,7 +33,7 @@ public interface MPSite<Q extends MPQuestion> extends Comparable<MPSite<?>> {
|
||||
// - Meta
|
||||
|
||||
@Nonnull
|
||||
String getName();
|
||||
String getSiteName();
|
||||
|
||||
// - Algorithm
|
||||
|
||||
@@ -57,10 +57,34 @@ public interface MPSite<Q extends MPQuestion> extends Comparable<MPSite<?>> {
|
||||
|
||||
void setLoginType(@Nullable MPResultType loginType);
|
||||
|
||||
default String getResult()
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
return getResult( MPKeyPurpose.Authentication );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
default String getResult(final MPKeyPurpose keyPurpose)
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
return getResult( keyPurpose, null );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
default String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext)
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
return getResult( keyPurpose, keyContext, null );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
String getResult(MPKeyPurpose keyPurpose, @Nullable String keyContext, @Nullable String state)
|
||||
throws MPKeyUnavailableException, MPAlgorithmException;
|
||||
|
||||
@Nonnull
|
||||
default String getLogin()
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
return getLogin( null );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
String getLogin(@Nullable String state)
|
||||
throws MPKeyUnavailableException, MPAlgorithmException;
|
||||
|
@@ -20,8 +20,6 @@ package com.lyndir.masterpassword.model;
|
||||
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.lyndir.masterpassword.*;
|
||||
import com.lyndir.masterpassword.model.impl.MPBasicSite;
|
||||
import com.lyndir.masterpassword.model.impl.MPBasicUser;
|
||||
import java.util.Collection;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@@ -87,7 +85,10 @@ public interface MPUser<S extends MPSite<?>> extends Comparable<MPUser<?>> {
|
||||
|
||||
// - Relations
|
||||
|
||||
void addSite(S site);
|
||||
S addSite(String siteName);
|
||||
|
||||
@Nonnull
|
||||
S addSite(S site);
|
||||
|
||||
void deleteSite(S site);
|
||||
|
||||
@@ -95,7 +96,7 @@ public interface MPUser<S extends MPSite<?>> extends Comparable<MPUser<?>> {
|
||||
Collection<S> getSites();
|
||||
|
||||
@Nonnull
|
||||
ImmutableCollection<S> findSites(String query);
|
||||
ImmutableCollection<S> findSites(@Nullable String query);
|
||||
|
||||
boolean addListener(Listener listener);
|
||||
|
||||
|
@@ -70,7 +70,7 @@ public abstract class MPBasicQuestion extends Changeable implements MPQuestion {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public abstract MPBasicSite<?> getSite();
|
||||
public abstract MPBasicSite<?, ?> getSite();
|
||||
|
||||
@Override
|
||||
protected void onChanged() {
|
||||
|
@@ -23,34 +23,36 @@ 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 com.lyndir.masterpassword.model.*;
|
||||
import java.util.*;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
/**
|
||||
* @author lhunath, 14-12-16
|
||||
*/
|
||||
public abstract class MPBasicSite<Q extends MPQuestion> extends Changeable implements MPSite<Q> {
|
||||
public abstract class MPBasicSite<U extends MPUser<?>, Q extends MPQuestion> extends Changeable
|
||||
implements MPSite<Q> {
|
||||
|
||||
private final Collection<Q> questions = new LinkedHashSet<>();
|
||||
private final U user;
|
||||
private final String siteName;
|
||||
|
||||
private String name;
|
||||
private MPAlgorithm algorithm;
|
||||
private UnsignedInteger counter;
|
||||
private MPResultType resultType;
|
||||
private MPResultType loginType;
|
||||
|
||||
private final Collection<Q> questions = new LinkedHashSet<>();
|
||||
|
||||
protected MPBasicSite(final String name, final MPAlgorithm algorithm) {
|
||||
this( name, algorithm, null, null, null );
|
||||
protected MPBasicSite(final U user, final String siteName, final MPAlgorithm algorithm) {
|
||||
this( user, siteName, algorithm, null, null, null );
|
||||
}
|
||||
|
||||
protected MPBasicSite(final String name, final MPAlgorithm algorithm, @Nullable final UnsignedInteger counter,
|
||||
protected MPBasicSite(final U user, final String siteName, final MPAlgorithm algorithm,
|
||||
@Nullable final UnsignedInteger counter,
|
||||
@Nullable final MPResultType resultType, @Nullable final MPResultType loginType) {
|
||||
this.name = name;
|
||||
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;
|
||||
@@ -59,8 +61,8 @@ public abstract class MPBasicSite<Q extends MPQuestion> extends Changeable imple
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getSiteName() {
|
||||
return siteName;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -128,7 +130,7 @@ public abstract class MPBasicSite<Q extends MPQuestion> extends Changeable imple
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
return getUser().getMasterKey().siteResult(
|
||||
getName(), getAlgorithm(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ),
|
||||
getSiteName(), getAlgorithm(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ),
|
||||
keyPurpose, keyContext, type, state );
|
||||
}
|
||||
|
||||
@@ -137,7 +139,7 @@ public abstract class MPBasicSite<Q extends MPQuestion> extends Changeable imple
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
return getUser().getMasterKey().siteState(
|
||||
getName(), getAlgorithm(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ),
|
||||
getSiteName(), getAlgorithm(), ifNotNullElse( counter, getAlgorithm().mpw_default_counter() ),
|
||||
keyPurpose, keyContext, type, state );
|
||||
}
|
||||
|
||||
@@ -171,32 +173,35 @@ public abstract class MPBasicSite<Q extends MPQuestion> extends Changeable imple
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public abstract MPBasicUser<?> getUser();
|
||||
public U getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onChanged() {
|
||||
super.onChanged();
|
||||
|
||||
getUser().setChanged();
|
||||
if (user instanceof Changeable)
|
||||
((Changeable) user).setChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode( getName() );
|
||||
return Objects.hashCode( getSiteName() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
return (this == obj) || ((obj instanceof MPSite) && Objects.equals( getName(), ((MPSite<?>) obj).getName() ));
|
||||
return (this == obj) || ((obj instanceof MPSite) && Objects.equals( getSiteName(), ((MPSite<?>) obj).getSiteName() ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(@NotNull final MPSite<?> o) {
|
||||
return getName().compareTo( o.getName() );
|
||||
public int compareTo(@Nonnull final MPSite<?> o) {
|
||||
return getSiteName().compareTo( o.getSiteName() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return strf( "{%s: %s}", getClass().getSimpleName(), getName() );
|
||||
return strf( "{%s: %s}", getClass().getSimpleName(), getSiteName() );
|
||||
}
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* @author lhunath, 2014-06-08
|
||||
*/
|
||||
public abstract class MPBasicUser<S extends MPBasicSite<?>> extends Changeable implements MPUser<S> {
|
||||
public abstract class MPBasicUser<S extends MPBasicSite<?, ?>> extends Changeable implements MPUser<S> {
|
||||
|
||||
protected final Logger logger = Logger.get( getClass() );
|
||||
private final Set<Listener> listeners = new CopyOnWriteArraySet<>();
|
||||
@@ -152,10 +152,11 @@ public abstract class MPBasicUser<S extends MPBasicSite<?>> extends Changeable i
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSite(final S site) {
|
||||
sites.put( site.getName(), site );
|
||||
public S addSite(final S site) {
|
||||
sites.put( site.getSiteName(), site );
|
||||
|
||||
setChanged();
|
||||
return site;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,11 +174,12 @@ public abstract class MPBasicUser<S extends MPBasicSite<?>> extends Changeable i
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public ImmutableCollection<S> findSites(final String query) {
|
||||
public ImmutableCollection<S> findSites(@Nullable final String query) {
|
||||
ImmutableSortedSet.Builder<S> results = ImmutableSortedSet.naturalOrder();
|
||||
for (final S site : getSites())
|
||||
if (site.getName().startsWith( query ))
|
||||
results.add( site );
|
||||
if (query != null)
|
||||
for (final S site : getSites())
|
||||
if (site.getSiteName().startsWith( query ))
|
||||
results.add( site );
|
||||
|
||||
return results.build();
|
||||
}
|
||||
@@ -211,7 +213,7 @@ public abstract class MPBasicUser<S extends MPBasicSite<?>> extends Changeable i
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final MPUser<?> o) {
|
||||
public int compareTo(@Nonnull final MPUser<?> o) {
|
||||
return getFullName().compareTo( o.getFullName() );
|
||||
}
|
||||
|
||||
|
@@ -31,9 +31,7 @@ import org.joda.time.ReadableInstant;
|
||||
* @author lhunath, 14-12-05
|
||||
*/
|
||||
@SuppressWarnings("ComparableImplementedButEqualsNotOverridden")
|
||||
public class MPFileSite extends MPBasicSite<MPFileQuestion> {
|
||||
|
||||
private final MPFileUser user;
|
||||
public class MPFileSite extends MPBasicSite<MPFileUser, MPFileQuestion> {
|
||||
|
||||
@Nullable
|
||||
private String url;
|
||||
@@ -61,10 +59,9 @@ public class MPFileSite extends MPBasicSite<MPFileQuestion> {
|
||||
@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,
|
||||
super( user, name, (algorithm == null)? user.getAlgorithm(): algorithm, counter,
|
||||
(resultType == null)? user.getDefaultType(): resultType, loginType );
|
||||
|
||||
this.user = user;
|
||||
this.resultState = resultState;
|
||||
this.loginState = loginState;
|
||||
this.url = url;
|
||||
@@ -94,23 +91,21 @@ public class MPFileSite extends MPBasicSite<MPFileQuestion> {
|
||||
public void use() {
|
||||
uses++;
|
||||
lastUsed = new Instant();
|
||||
user.use();
|
||||
getUser().use();
|
||||
|
||||
setChanged();
|
||||
}
|
||||
|
||||
public String getResult()
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
return getResult( MPKeyPurpose.Authentication, null );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getResult(final MPKeyPurpose keyPurpose, @Nullable final String keyContext)
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
return getResult( keyPurpose, keyContext, getResultState() );
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getLogin()
|
||||
throws MPKeyUnavailableException, MPAlgorithmException {
|
||||
|
||||
@@ -153,14 +148,8 @@ public class MPFileSite extends MPBasicSite<MPFileQuestion> {
|
||||
setChanged();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public MPFileUser getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final MPSite<?> o) {
|
||||
public int compareTo(@Nonnull final MPSite<?> o) {
|
||||
int comparison = (o instanceof MPFileSite)? ((MPFileSite) o).getLastUsed().compareTo( getLastUsed() ): 0;
|
||||
if (comparison != 0)
|
||||
return comparison;
|
||||
|
@@ -23,6 +23,7 @@ import com.lyndir.masterpassword.model.MPIncorrectMasterPasswordException;
|
||||
import com.lyndir.masterpassword.model.MPUser;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.Instant;
|
||||
import org.joda.time.ReadableInstant;
|
||||
@@ -163,6 +164,11 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPFileSite addSite(final String siteName) {
|
||||
return addSite( new MPFileSite( this, siteName ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onChanged() {
|
||||
try {
|
||||
@@ -180,7 +186,7 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(final MPUser<?> o) {
|
||||
public int compareTo(@Nonnull final MPUser<?> o) {
|
||||
int comparison = (o instanceof MPFileUser)? ((MPFileUser) o).getLastUsed().compareTo( getLastUsed() ): 0;
|
||||
if (comparison != 0)
|
||||
return comparison;
|
||||
|
@@ -79,7 +79,7 @@ public class MPFlatMarshaller implements MPMarshaller {
|
||||
site.getAlgorithm().version().toInt(), // algorithm
|
||||
site.getCounter().intValue() ), // counter
|
||||
ifNotNullElse( loginName, "" ), // loginName
|
||||
site.getName(), // siteName
|
||||
site.getSiteName(), // siteName
|
||||
ifNotNullElse( password, "" ) // password
|
||||
) );
|
||||
}
|
||||
|
@@ -90,7 +90,7 @@ public class MPJSONFile extends MPJSONAnyObject {
|
||||
// Clear Text
|
||||
content = modelSite.getResult();
|
||||
loginContent = modelUser.getMasterKey().siteResult(
|
||||
modelSite.getName(), modelSite.getAlgorithm(), modelSite.getAlgorithm().mpw_default_counter(),
|
||||
modelSite.getSiteName(), modelSite.getAlgorithm(), modelSite.getAlgorithm().mpw_default_counter(),
|
||||
MPKeyPurpose.Identification, null, modelSite.getLoginType(), modelSite.getLoginState() );
|
||||
} else {
|
||||
// Redacted
|
||||
@@ -100,9 +100,9 @@ public class MPJSONFile extends MPJSONAnyObject {
|
||||
loginContent = modelSite.getLoginState();
|
||||
}
|
||||
|
||||
Site site = sites.get( modelSite.getName() );
|
||||
Site site = sites.get( modelSite.getSiteName() );
|
||||
if (site == null)
|
||||
sites.put( modelSite.getName(), site = new Site() );
|
||||
sites.put( modelSite.getSiteName(), site = new Site() );
|
||||
site.type = modelSite.getResultType();
|
||||
site.counter = modelSite.getCounter().longValue();
|
||||
site.algorithm = modelSite.getAlgorithm().version();
|
||||
|
Reference in New Issue
Block a user