Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1014 → Rev 1015

/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStore.java
0,0 → 1,33
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
 
/**
* A password store must implement java.lang.Cloneable interface.
*/
public interface PasswordStore
{
/**
* returns digest name, e.g. "MD5"
*/
public String getDigest();
 
/**
* to store to persistent store
*/
public String getPassword();
 
/**
* to store from persistent store
*/
public void setPassword(String password);
 
/**
* to set by user
*/
public void setNewPassword(String password)
throws ModelException;
 
public boolean checkPassword(String password)
throws ModelException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStoreMd5.java
0,0 → 1,23
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.DigestMd5;
 
/**
*
* @hibernate.subclass discriminator-value="MD5"
*/
public class PasswordStoreMd5
extends PasswordStoreAbstract
{
public PasswordStoreMd5()
{
digest = "MD5";
}
 
protected String encode(String password)
throws ModelException
{
return DigestMd5.encode(password);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/Mailbox.java
1,6 → 1,9
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.Digest;
import java.util.Iterator;
import java.util.Collection;
import java.util.HashSet;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
12,7 → 15,7
extends GeneralModelObject
{
private String login;
private String password;
private Collection passwords; // Collection(PasswordStore)
private InetDomain domain;
private User owner;
private Boolean virusCheck;
28,7 → 31,12
{
super(origin);
this.login = origin.login;
this.password = origin.password;
 
if(origin.passwords == null)
this.passwords = null;
else
this.passwords = new HashSet(origin.passwords);
 
this.domain = origin.domain;
this.owner = origin.owner;
this.virusCheck = origin.virusCheck;
71,30 → 79,47
this.login = login;
}
 
/**
*
* @hibernate.property
*/
protected String getPassword()
protected void addPasswordStore(PasswordStore ps)
{
return password;
if(passwords == null) passwords = new HashSet();
passwords.add(ps);
}
 
protected void setPassword(String password)
{
this.password = password;
}
 
public void setPassword(User editor, String password)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
if(password == null)
throw new NullPointerException("Null password");
 
backupMe();
this.password = Digest.encode(password);
 
for(Iterator i = passwords.iterator(); i.hasNext(); ) {
Object o = i.next();
if(!(o instanceof PasswordStore))
throw new ModelException("It's not a password store");
((PasswordStore)o).setNewPassword(password);
}
}
 
/**
*
* @hibernate.set cascade="all"
* @hibernate.collection-key column="obj"
* @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.PasswordStoreAbstract"
*/
protected Collection getPasswords()
{
return passwords;
}
 
protected void setPasswords(Collection passwords)
{
this.passwords = passwords;
}
 
/**
*
* @hibernate.many-to-one
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStoreAbstract.java
0,0 → 1,115
package ak.hostadmiral.core.model;
 
import java.util.Date;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.DigestMd5;
 
/**
*
* @hibernate.class table="passwords"
* @hibernate.discriminator column="digest" type="string"
*/
public abstract class PasswordStoreAbstract
implements PasswordStore, Cloneable
{
private Long id;
private Date modStamp;
private User modUser;
protected String digest;
protected String password;
 
/**
*
* @hibernate.id generator-class="native"
*/
public Long getId()
{
return id;
}
 
protected void setId(Long id)
{
this.id = id;
}
 
/**
*
* @hibernate.timestamp column="mod_stamp"
*/
public Date getModStamp()
{
return modStamp;
}
 
protected void setModStamp(Date modStamp)
{
this.modStamp = modStamp;
}
 
/**
*
* @hibernate.many-to-one column="mod_user"
*/
public User getModUser()
{
return modUser;
}
 
protected void setModUser(User modUser)
{
this.modUser = modUser;
}
 
/**
* returns digest name, e.g. "MD5"
*/
public String getDigest()
{
return digest;
}
 
/**
* to store to persistent store
*
* @hibernate.property
*/
public String getPassword()
{
return password;
}
 
/**
* to store from persistent store
*/
public void setPassword(String password)
{
this.password = password;
}
 
/**
* to set by user
*/
public void setNewPassword(String password)
throws ModelException
{
this.password = encode(password);
}
 
public boolean checkPassword(String password)
throws ModelException
{
return this.password.equals(encode(password));
}
 
protected abstract String encode(String password)
throws ModelException;
 
public Object clone()
throws CloneNotSupportedException
{
PasswordStoreAbstract theClone = (PasswordStoreAbstract)super.clone();
theClone.password = password;
return theClone;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/UserManager.java
70,6 → 70,11
user.setBoss(editor);
}
 
// FIXME: make this configurable
user.addPasswordStore(new PasswordStoreMd5());
user.addPasswordStore(new PasswordStoreCrypt());
user.addPasswordStore(new PasswordStorePlain());
 
return user;
}
 
/hostadmiral/trunk/src/ak/hostadmiral/core/model/User.java
1,5 → 1,6
package ak.hostadmiral.core.model;
 
import java.util.Iterator;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
6,7 → 7,6
import java.util.Locale;
import java.util.StringTokenizer;
 
import ak.hostadmiral.util.Digest;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
 
17,8 → 17,10
public class User
extends GeneralModelObject
{
public static final String DEFAULT_PASSWORD_STORE = "MD5";
 
private String login;
private String password;
private Collection passwords; // Collection(PasswordStore)
private User boss;
private Boolean superuser;
private Locale locale = Locale.getDefault();
32,11 → 34,17
protected User(User origin)
{
super(origin);
this.login = origin.login;
this.password = origin.password;
this.boss = origin.boss;
this.login = origin.login;
 
if(origin.passwords == null)
this.passwords = null;
else
this.passwords = new HashSet(origin.passwords);
 
this.boss = origin.boss;
this.superuser = origin.superuser;
this.locale = origin.locale;
this.locale = origin.locale;
 
if(origin.loginHistory == null)
this.loginHistory = null;
else
81,20 → 89,39
this.login = login;
}
 
/**
*
* @hibernate.property
*/
protected String getPassword()
protected void addPasswordStore(PasswordStore ps)
{
return password;
if(passwords == null) passwords = new HashSet();
passwords.add(ps);
}
 
protected void setPassword(String password)
protected PasswordStore getDefaultPasswordStore()
throws ModelException
{
this.password = password;
if(passwords == null)
throw new ModelException("No password store");
 
for(Iterator i = passwords.iterator(); i.hasNext(); ) {
Object o = i.next();
if(!(o instanceof PasswordStore))
throw new ModelException("It's not a password store");
PasswordStore ps = (PasswordStore)o;
if(DEFAULT_PASSWORD_STORE.equals(ps.getDigest()))
return ps;
}
 
throw new ModelException("No password store for digest " + DEFAULT_PASSWORD_STORE);
}
 
public String getPassword(User editor)
throws ModelException
{
if(!partEditableBy(editor))
throw new ModelSecurityException();
 
return getDefaultPasswordStore().getPassword();
}
 
public void setPassword(User editor, String password)
throws ModelException
{
105,20 → 132,38
throw new NullPointerException("Null password");
 
backupMe();
this.password = Digest.encode(password);
 
for(Iterator i = passwords.iterator(); i.hasNext(); ) {
Object o = i.next();
if(!(o instanceof PasswordStore))
throw new ModelException("It's not a password store");
((PasswordStore)o).setNewPassword(password);
}
}
 
public boolean checkPassword(String password)
throws ModelException
{
if(password == null)
throw new NullPointerException("Null password");
 
return checkMd5Password(Digest.encode(password));
return getDefaultPasswordStore().checkPassword(password);
}
 
public boolean checkMd5Password(String password)
/**
*
* @hibernate.set cascade="all"
* @hibernate.collection-key column="obj"
* @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.PasswordStoreAbstract"
*/
protected Collection getPasswords()
{
return passwords;
}
 
protected void setPasswords(Collection passwords)
{
return this.password.equals(password);
this.passwords = passwords;
}
 
/**
/hostadmiral/trunk/src/ak/hostadmiral/core/model/MailboxManager.java
65,7 → 65,14
{
if(!allowedToCreate(editor)) throw new ModelSecurityException();
 
return new Mailbox();
Mailbox mailbox = new Mailbox();
 
// FIXME: make this configurable
mailbox.addPasswordStore(new PasswordStoreMd5());
mailbox.addPasswordStore(new PasswordStoreCrypt());
mailbox.addPasswordStore(new PasswordStorePlain());
 
return mailbox;
}
 
public boolean allowedToCreate(User editor)
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStoreCrypt.java
0,0 → 1,33
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.DigestCrypt;
 
/**
*
* @hibernate.subclass discriminator-value="crypt"
*/
public class PasswordStoreCrypt
extends PasswordStoreAbstract
{
public PasswordStoreCrypt()
{
digest = "crypt";
}
 
protected String encode(String password)
throws ModelException
{
return DigestCrypt.crypt(password);
}
 
public boolean checkPassword(String password)
throws ModelException
{
if(this.password == null || this.password.length() < 3) return false;
 
String salt = this.password.substring(0, 2);
String crypted = DigestCrypt.crypt(salt, password);
return crypted.equals(this.password);
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStorePlain.java
0,0 → 1,23
package ak.hostadmiral.core.model;
 
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.DigestMd5;
 
/**
*
* @hibernate.subclass discriminator-value="Plain"
*/
public class PasswordStorePlain
extends PasswordStoreAbstract
{
public PasswordStorePlain()
{
digest = "Plain";
}
 
protected String encode(String password)
throws ModelException
{
return password;
}
}