[jboss-cvs] jboss-seam/src/main/org/jboss/seam/security/management ...
Shane Bryzak
sbryzak at redhat.com
Wed Dec 5 02:16:19 EST 2007
User: sbryzak2
Date: 07/12/05 02:16:19
Added: src/main/org/jboss/seam/security/management
CreateAccountException.java IdentityManager.java
IdentityStore.java JpaIdentityStore.java
UserAccount.java package-info.java
Log:
some of the identity management stuff
Revision Changes Path
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/CreateAccountException.java
Index: CreateAccountException.java
===================================================================
package org.jboss.seam.security.management;
/**
* Thrown when an exception is encountered during account creation.
*
* @author Shane Bryzak
*/
public class CreateAccountException extends RuntimeException
{
public CreateAccountException(String message)
{
super(message);
}
public CreateAccountException(String message, Throwable cause)
{
super(message, cause);
}
}
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/IdentityManager.java
Index: IdentityManager.java
===================================================================
package org.jboss.seam.security.management;
import static org.jboss.seam.ScopeType.APPLICATION;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.log.LogProvider;
import org.jboss.seam.log.Logging;
/**
* Identity Management API, deals with user name/password-based identity management.
*
* @author Shane Bryzak
*/
@Scope(APPLICATION)
@Name("org.jboss.seam.security.identityManager")
@Install(precedence = BUILT_IN)
public class IdentityManager
{
public static final String IDENTITY_STORE_COMPONENT_NAME = "identityStore";
private static final LogProvider log = Logging.getLogProvider(IdentityManager.class);
private IdentityStore identityStore;
@Create
public void create()
{
initIdentityStore();
}
protected void initIdentityStore()
{
if (identityStore == null)
{
identityStore = (IdentityStore) Component.getInstance(IDENTITY_STORE_COMPONENT_NAME, true);
}
if (identityStore == null)
{
log.warn("no identity store available - please install an IdentityStore with the name '" +
IDENTITY_STORE_COMPONENT_NAME + "' if identity management is required.");
}
}
public static IdentityManager instance()
{
if ( !Contexts.isApplicationContextActive() )
{
throw new IllegalStateException("No active application context");
}
IdentityManager instance = (IdentityManager) Component.getInstance(
IdentityManager.class, ScopeType.APPLICATION);
if (instance == null)
{
throw new IllegalStateException("No IdentityManager could be created");
}
return instance;
}
public UserAccount createAccount(String username, String password)
{
return identityStore.createAccount(username, password);
}
public IdentityStore getIdentityStore()
{
return identityStore;
}
public void setIdentityStore(IdentityStore identityStore)
{
this.identityStore = identityStore;
}
}
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/IdentityStore.java
Index: IdentityStore.java
===================================================================
package org.jboss.seam.security.management;
import java.security.MessageDigest;
import org.jboss.seam.util.Hex;
/**
* The identity store does the actual work of persisting user accounts in a
* database, LDAP directory, etc.
*
* @author Shane Bryzak
*/
public abstract class IdentityStore
{
private String hashFunction = "MD5";
private String hashCharset = "UTF-8";
protected abstract UserAccount createAccount(String username, String password);
protected void hashAccountPassword(UserAccount account, String password)
{
try {
MessageDigest md = MessageDigest.getInstance(hashFunction);
md.update(password.getBytes(hashCharset));
byte[] raw = md.digest();
// TODO - salt the hash, possibly using the user name?
account.setPasswordHash(new String(Hex.encodeHex(raw)));
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
Index: JpaIdentityStore.java
===================================================================
package org.jboss.seam.security.management;
import static org.jboss.seam.ScopeType.APPLICATION;
import javax.persistence.EntityManager;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
/**
* The default identity store implementation, uses JPA as its persistence mechanism.
*
* @author Shane Bryzak
*/
@Scope(APPLICATION)
@BypassInterceptors
public class JpaIdentityStore extends IdentityStore
{
private Class<? extends UserAccount> accountClass;
private String entityManagerName = "entityManager";
@Override
protected UserAccount createAccount(String username, String password)
{
try
{
if (accountClass == null)
{
throw new CreateAccountException("Could not create account, accountClass not set");
}
UserAccount account = accountClass.newInstance();
account.setUsername(username);
if (password == null)
{
account.setEnabled(false);
}
else
{
hashAccountPassword(account, password);
account.setEnabled(true);
}
persistAccount(account);
return account;
}
catch (Exception ex)
{
if (ex instanceof CreateAccountException)
{
throw (CreateAccountException) ex;
}
else
{
throw new CreateAccountException("Could not create account", ex);
}
}
}
protected void persistAccount(UserAccount account)
{
getEntityManager().persist(account);
}
private EntityManager getEntityManager()
{
return (EntityManager) Component.getInstance(entityManagerName);
}
public String getEntityManagerName()
{
return entityManagerName;
}
public void setEntityManagerName(String name)
{
this.entityManagerName = name;
}
public Class<? extends UserAccount> getAccountClass()
{
return accountClass;
}
public void setAccountClass(Class<? extends UserAccount> accountClass)
{
this.accountClass = accountClass;
}
}
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/UserAccount.java
Index: UserAccount.java
===================================================================
package org.jboss.seam.security.management;
import java.io.Serializable;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class UserAccount implements Serializable
{
private String username;
private String passwordHash;
private boolean enabled;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPasswordHash()
{
return passwordHash;
}
public void setPasswordHash(String passwordHash)
{
this.passwordHash = passwordHash;
}
public boolean isEnabled()
{
return enabled;
}
public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
1.1 date: 2007/12/05 07:16:19; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/management/package-info.java
Index: package-info.java
===================================================================
/**
* Seam Identity Management
*
* @see org.jboss.seam.security.management.IdentityManager
*/
@Namespace(value="http://jboss.com/products/seam/security/management", prefix="org.jboss.seam.security.management")
@AutoCreate
package org.jboss.seam.security.management;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Namespace;
More information about the jboss-cvs-commits
mailing list