Index: topia2/src/java/org/codelutin/topia/security/TopiaSecurityVetoableListener.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaSecurityVetoableListener.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaSecurityVetoableListener.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,175 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSecurityVetoableListener.java +* +* Created: 10 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + + +/** + * + */ +package org.codelutin.topia.security; + +import static org.codelutin.topia.security.TopiaSecurityUtil.CREATE_TEXT; +import static org.codelutin.topia.security.TopiaSecurityUtil.DELETE_TEXT; +import static org.codelutin.topia.security.TopiaSecurityUtil.LOAD_TEXT; +import static org.codelutin.topia.security.TopiaSecurityUtil.UPDATE_TEXT; + +import java.security.AccessControlException; +import java.security.AccessController; + +import javax.security.auth.Subject; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.event.TopiaVetoableEntityEvent; +import org.codelutin.topia.event.TopiaVetoableEntityListener; +import org.codelutin.topia.event.TopiaVetoableEntityLoadEvent; +import org.codelutin.topia.event.TopiaVetoableEntityLoadListener; +import org.codelutin.topia.security.entities.TopiaEntityPermission; +import org.codelutin.topia.security.entities.TopiaEntityPermissionImpl; +import org.codelutin.topia.security.entities.TopiaPermission; +import org.codelutin.topia.security.entities.TopiaUserImpl; + +/** + * Implantation pour la sécurité des Vetoable Listeners de ToPIA. Dans le cas de + * load, vrai ou faux est retourné en fonction des droits de l'utilisateur. Pour + * les autres, une exception est levée si l'utilisateur n'a pas les droits. + */ +public class TopiaSecurityVetoableListener implements + TopiaVetoableEntityListener, TopiaVetoableEntityLoadListener { + + private Log log = LogFactory.getLog(TopiaSecurityVetoableListener.class); + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#createEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void createEntity(TopiaVetoableEntityEvent event) { + if (log.isDebugEnabled()) { + log.debug("[Security] create entity : " + event.getEntityClass()); + } + try { + checkPermission(event.getEntityClass(), CREATE_TEXT); + } catch (TopiaException te) { + throw new SecurityException("Access denied to entity creation", te); + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#updateEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void updateEntity(TopiaVetoableEntityEvent event) { + if (log.isDebugEnabled()) { + log.debug("[Security] update entity : " + event.getId()); + } + try { + checkPermission((String)event.getId(), UPDATE_TEXT); + } catch (TopiaException te) { + throw new SecurityException("Access denied to entity modification", te); + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityListener#deleteEntity(org.codelutin.topia.event.TopiaVetoableEntityEvent) + */ + public void deleteEntity(TopiaVetoableEntityEvent event) { + if (log.isDebugEnabled()) { + log.debug("[Security] delete entity : " + event.getId()); + } + try { + checkPermission((String)event.getId(), DELETE_TEXT); + } catch (TopiaException te) { + throw new SecurityException("Access denied to entity deletion", te); + } + } + + /* (non-Javadoc) + * @see org.codelutin.topia.event.TopiaVetoableEntityLoadListener#loadEntity(org.codelutin.topia.event.TopiaVetoableEntityLoadEvent) + */ + public boolean loadEntity(TopiaVetoableEntityLoadEvent event) { + if (log.isDebugEnabled()) { + log.debug("[Security] load entity : " + event.getId()); + } + if (event.getEntityClass().equals(TopiaUserImpl.class) || + event.getEntityClass().equals(TopiaEntityPermissionImpl.class)){ + if (log.isInfoEnabled()) { + log.info("[Security] load granted to : " + event.getId()); + } + return true; + } + try { + checkPermission((String)event.getId(), LOAD_TEXT); + } catch (TopiaException te) { + return false; + } + return true; + } + + /** + * Vérifie si l'utilisateur actuellement loggué a le droit d'accéder à + * l'entité passée en paramètre pour les actions spécifiées. + * @param entity l'entité pour laquelle on vérifie les droits + * @param actions les actions [read, write, admin] + * @throws TopiaSecurityException + */ + private void checkPermission(Class entityClass, String actions) throws TopiaException { + if (log.isTraceEnabled()) { + log.trace("Checking permissions to entity class : " + entityClass); + } + if (entityClass == null) + throw new TopiaException("Class cannot be null"); + String topiaId = entityClass.getName() + "#*"; + checkPermission(topiaId, actions); + } + + /** + * Vérifie si l'utilisateur actuellement loggué a le droit d'accéder à + * l'entité passée en paramètre pour les actions spécifiées. + * @param topiaId le topiaId de l'entité pour laquelle on vérifie les droits + * @param actions les actions [read, write, admin] + * @throws TopiaSecurityException + */ + private void checkPermission(String topiaId, String actions) throws TopiaException { + Subject subj = Subject.getSubject(AccessController.getContext()); + if (subj == null) + throw new TopiaException("Use doAs() and login first"); + try { + TopiaEntityPermission entityPerm = new TopiaEntityPermissionImpl( + topiaId, subj.getPrincipals(), actions); + AccessController.checkPermission(new TopiaPermission(entityPerm)); + } catch (AccessControlException e) { + throw new TopiaException("access denied to object \"" + topiaId + "\" for \"" + subj + "\"", e); + } + if (log.isTraceEnabled()) { + log.trace("Permission granted for entity : " + topiaId); + } + } + +} //TopiaSecurityVetoableListener Index: topia2/src/java/org/codelutin/topia/security/TopiaPolicy.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaPolicy.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaPolicy.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,164 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, + * Cédric Pineau, Benjamin Poussin, + * + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * TopiaPolicy.java + * + * Created: 17 févr. 2006 + * + * @author Arnaud Thimel + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ + +package org.codelutin.topia.security; + +import java.security.CodeSource; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.util.Collection; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaNotFoundException; +import org.codelutin.topia.framework.TopiaContextImplementor; +import org.codelutin.topia.security.entities.TopiaPermission; +import org.codelutin.topia.security.entities.TopiaPermissionManager; + +public class TopiaPolicy extends Policy { + + private Log log = LogFactory.getLog(TopiaPolicy.class); + + private TopiaContextImplementor topiaContext; + + protected Policy parentPolicy; + + public TopiaPolicy(TopiaContext context) { + super(); + this.topiaContext = (TopiaContextImplementor) context; + } + + private TopiaPermissionManager getPermissionManager() { + try { + return ((TopiaContextImplementor)topiaContext.beginTransaction()).getPermissionManager(); + } catch (TopiaNotFoundException tnfe) { + log.error("Permission manager non accessible", tnfe); + } + return null; + } + + /** + * Renvoie la Policy parente + * @see #installPolicy() + * @return l'attribut parentPolicy + */ + public Policy getParentPolicy() { + return parentPolicy; + } + + /** + * Remplace la Policy parente + * @param parentPolicy + * la nouvelle Policy parente + */ + public void setParentPolicy(Policy parentPolicy) { + this.parentPolicy = parentPolicy; + } + + /* (non-Javadoc) + * @see java.security.Policy#getPermissions(java.security.CodeSource) + */ + @Override + public PermissionCollection getPermissions(CodeSource codesource) { + PermissionCollection pc = parentPolicy.getPermissions(codesource); + return pc; + } + + /* (non-Javadoc) + * @see java.security.Policy#getPermissions(java.security.ProtectionDomain) + */ + @Override + public PermissionCollection getPermissions(ProtectionDomain domain) { + PermissionCollection pc = parentPolicy.getPermissions(domain); + TopiaPermissionManager manager = getPermissionManager(); + if (manager != null) { + try { + Collection perms = manager.getAllPermissions(); + for (TopiaPermission topiaPerm : perms) { + pc.add(topiaPerm); + } + } catch (TopiaException te) { + log.error("Récupération des TopiaPermission impossible", te); + } + } + return pc; + } + + /* (non-Javadoc) + * @see java.security.Policy#refresh() + */ + @Override + public void refresh() { + parentPolicy.refresh(); + } + + /* (non-Javadoc) + * @see java.security.Policy#implies(java.security.ProtectionDomain, + * java.security.Permission) + */ + @Override + public boolean implies(ProtectionDomain domain, Permission permission) { + PermissionCollection pc = getPermissions(domain); + if (pc == null) { + return false; + } + return pc.implies(permission); + } + + /** + * Installe cette TopiaPolicy. Si la Policy existante est déja cette + * TopiaPolicy alors la méthode n'a pas d'effet. Si une autre Policy existe + * deja alors cette TopiaPolicy, elle conserve l'ancienne Policy dans + * parentPolicy et la remplace alors. + */ + public void installPolicy() { + Policy policy = Policy.getPolicy(); + if (policy == this) + return; + if (policy instanceof TopiaPolicy) { + if (log.isDebugEnabled()) { + log.debug("Policy deja modifie en: " + policy); + } + } else { + if (log.isDebugEnabled()) { + log.debug("l'ancienne Policy etait: " + policy); + } + setParentPolicy(policy); + Policy.setPolicy(this); + } + } + +} // TopiaPolicy Index: topia2/src/java/org/codelutin/topia/security/TopiaConfiguration.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaConfiguration.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaConfiguration.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,153 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, + * Cédric Pineau, Benjamin Poussin, + * + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * TopiaConfiguration.java + * + * Created: 20 févr. 2006 + * + * @author Arnaud Thimel + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ + +package org.codelutin.topia.security; + +import static org.codelutin.topia.security.TopiaSecurityUtil.CONTEXT_KEY; +import static org.codelutin.topia.security.TopiaSecurityUtil.TOPIA_LOGIN_MODULE; + +import java.util.HashMap; +import java.util.Map; + +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; + +import org.codelutin.topia.TopiaContext; + +public class TopiaConfiguration extends Configuration { + + private Map appConfEntries; + + /** + * Créé une instance de TopiConfiguration avec un identifiant de + * configurationEntry et le nom du fichier de propriétés associé. + * + * @param name + * le nom de la configurationEntry + * @param contextPropertiesName + * le nom du fichier de propriétés + */ + public TopiaConfiguration(String name, TopiaContext context) { + super(); + appConfEntries = new HashMap(); + addEntry(name, context); + } + + /** + * Ajoute une ConfigurationEntry avec le nom de fichier de propriétés + * associé + * + * @param name + * le nom de la configurationEntry + * @param securityHelper + * le nom du fichier de propriétés + */ + private void addEntry(String name, TopiaContext context) { + AppConfigurationEntry[] confEntries = getAppConfigurationEntry(name); + if (confEntries != null) { + int i = 0; + for (; i < confEntries.length; i++) + if (TOPIA_LOGIN_MODULE.equals(confEntries[i] + .getLoginModuleName())) + break; + if (i == confEntries.length) { + AppConfigurationEntry[] tmpConfEntries = confEntries; + confEntries = new AppConfigurationEntry[confEntries.length + 1]; + for (int j = 0; j < confEntries.length; j++) + confEntries[j] = tmpConfEntries[j]; + confEntries[confEntries.length - 1] = createEntry(context); + } else { + if ( /* Mauvais FLAG */ + !AppConfigurationEntry.LoginModuleControlFlag.REQUIRED + .equals(confEntries[i].getControlFlag()) + /* Ne contient pas la propriété */ + || !confEntries[i].getOptions().containsKey( + CONTEXT_KEY) + /* Propriété mal initialisée */ + || !confEntries[i].getOptions() + .get(CONTEXT_KEY).equals(context)) + confEntries[i] = createEntry(context, confEntries[i] + .getOptions()); + } + } else { + confEntries = new AppConfigurationEntry[1]; + confEntries[0] = createEntry(context); + } + appConfEntries.put(name, confEntries); + } + + /** + * Créé une entry avec des options vides + * + * @param securityHelper + * le SecurityHelper + * @return l'entry créée + */ + private AppConfigurationEntry createEntry(TopiaContext context) { + return createEntry(context, null); + } + + /** + * Créé une entry en rajoutant les options nécessaires à l'attribut options + * + * @param securityHelper + * le nom du fichier de propriétés + * @param options + * l'objet contenant les options précédentes + * @return l'entry créée + */ + private AppConfigurationEntry createEntry(TopiaContext context, Map options) { + if (options == null) + options = new HashMap(); + options.put(CONTEXT_KEY, context); + return new AppConfigurationEntry(TOPIA_LOGIN_MODULE, + AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); + } + + /** + * Renvoie les entries associéess à l'attribut name + * + * @param name + * l'identifiant des entries demandées + * @return un tableau cotenant les entries demandées + */ + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { + return appConfEntries.get(name); + } + + /* (non-Javadoc) + * @see javax.security.auth.login.Configuration#refresh() + */ + public void refresh() { + } + +} // TopiaConfiguration Index: topia2/src/java/org/codelutin/topia/security/TopiaLoginModule.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaLoginModule.java:1.1 --- /dev/null Fri Feb 24 00:48:20 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaLoginModule.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,173 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaLoginModule.java +* +* Created: 15 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + + +/** + * + */ +package org.codelutin.topia.security; + +import java.security.Principal; +import java.util.Map; +import java.util.Set; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.login.LoginException; +import javax.security.auth.spi.LoginModule; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.framework.TopiaContextImplementor; +import org.codelutin.topia.security.entities.TopiaUserManager; + +import static org.codelutin.topia.security.TopiaSecurityUtil.CONTEXT_KEY; + +public class TopiaLoginModule extends Object implements LoginModule { + + private Log log = LogFactory.getLog(TopiaLoginModule.class); + + private Subject subject; + private CallbackHandler callbackHandler; + private boolean loginSuccess; + private Set principals; + private TopiaContextImplementor topiaContext; + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#initialize( + * javax.security.auth.Subject, + * javax.security.auth.callback.CallbackHandler, + * java.util.Map, + * java.util.Map) + */ + public void initialize(Subject subject, CallbackHandler callbackHandler, + Map sharedState, Map options) { + this.subject = subject; + this.callbackHandler = callbackHandler; + this.principals = null; + this.topiaContext = (TopiaContextImplementor)options.get(CONTEXT_KEY); + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#login() + */ + public boolean login() throws LoginException { + if (callbackHandler == null) { + throw new LoginException("CallbackHandler cannot be null"); + } + if (topiaContext == null) { + throw new LoginException("\"" + CONTEXT_KEY + "\" property must be set"); + } + + String login, password = null; + + loginSuccess = false; + + NameCallback nc = new NameCallback("login"); + PasswordCallback pc = new PasswordCallback("password", false); + + Callback[] callbacks = new Callback[2]; + + callbacks[0] = nc; + callbacks[1] = pc; + + try { + //Récupération du login et mot de passe + callbackHandler.handle(callbacks); + } catch (Exception eee) { + if (log.isWarnEnabled()) { + log.warn("Utilisation du CallbackHandler impossible", eee); + } + LoginException le = new LoginException( + "Utilisation du CallbackHandler impossible"); + le.initCause(eee); + throw le; + } + login = nc.getName(); + password = new String(pc.getPassword()); + pc.clearPassword(); + + //Véricfication du login/pass et récupération des Principal + try { + TopiaUserManager userManager = + ((TopiaContextImplementor)topiaContext.beginTransaction()) + .getUserManager(); + principals = userManager.authenticate(login, password); + } catch (TopiaException te) { + if (log.isWarnEnabled()) { + log.warn("Erreur lors de l'authentification", te); + } + LoginException le = new LoginException( + "Erreur lors de l'authentification"); + le.initCause(te); + throw le; + } + return (loginSuccess = true); + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#commit() + */ + public boolean commit() throws LoginException { + if (loginSuccess) { + subject.getPrincipals().addAll(principals); + } + return true; + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#abort() + */ + public boolean abort() throws LoginException { + //On effectue les mêmes actions que logout + return logout(); + } + + /* (non-Javadoc) + * @see javax.security.auth.spi.LoginModule#logout() + */ + public boolean logout() throws LoginException { + //On libère les ressources + subject.getPrincipals().remove(principals); + loginSuccess = false; + subject = null; + principals.clear(); + principals = null; + callbackHandler = null; + return true; + } + +} //TopiaLoginModule Index: topia2/src/java/org/codelutin/topia/security/TopiaSecurityUtil.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaSecurityUtil.java:1.1 --- /dev/null Fri Feb 24 00:48:21 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaSecurityUtil.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,74 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program 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 2 +* of the License, or (at your option) any later version. +* +* This program 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 should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* TopiaSecurityUtil.java +* +* Created: 15 févr. 2006 +* +* @author Arnaud Thimel +* @version $Revision$ +* +* Mise a jour: $Date$ +* par : $Author$ +*/ + + +package org.codelutin.topia.security; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class TopiaSecurityUtil { + + public static final int LOAD = 0x1; + public static final String LOAD_TEXT = "LOAD"; + public static final int CREATE = 0x2; + public static final String CREATE_TEXT = "CREATE"; + public static final int UPDATE = 0x4; + public static final String UPDATE_TEXT = "UPDATE"; + public static final int DELETE = 0x8; + public static final String DELETE_TEXT = "DELETE"; + + public static final String CONTEXT_KEY = "topia.app.context"; + + public static final String TOPIA_LOGIN_MODULE = TopiaLoginModule.class.getName(); + + /** + * Applique un algorithme de hashage sur la chaine de caratère passée en + * paramètre + * @param msg la chaine de caratère sur laquelle on veut opérer le hashage + * @return La chaine de caractère une fois l'algorithme appliqué + */ + public static String hash(String msg) { + if (msg == null) { + return null; + } + try { + MessageDigest digest = MessageDigest.getInstance("SHA"); + byte[] bytes = msg.getBytes(); + bytes = digest.digest(bytes); + return new String(bytes); + } catch (NoSuchAlgorithmException nsee) { + return msg; + } + } + +} //TopiaSecurityUtil Index: topia2/src/java/org/codelutin/topia/security/TopiaCallbackHandler.java diff -u /dev/null topia2/src/java/org/codelutin/topia/security/TopiaCallbackHandler.java:1.1 --- /dev/null Fri Feb 24 00:48:21 2006 +++ topia2/src/java/org/codelutin/topia/security/TopiaCallbackHandler.java Fri Feb 24 00:48:15 2006 @@ -0,0 +1,78 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, + * Cédric Pineau, Benjamin Poussin, + * + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * TopiaCallbackHandler.java + * + * Created: 20 févr. 2006 + * + * @author Arnaud Thimel + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ + +package org.codelutin.topia.security; + +import java.io.IOException; + +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; + +public class TopiaCallbackHandler implements CallbackHandler { + + private String username; + + private String password; + + /** + * @param username + * @param password + */ + public TopiaCallbackHandler(String username, String password) { + super(); + this.username = username; + this.password = password; + } + + /* + * (non-Javadoc) + * + * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[]) + */ + public void handle(Callback[] callbacks) throws IOException, + UnsupportedCallbackException { + for (int i = 0; i < callbacks.length; i++) { + if (callbacks[i] instanceof NameCallback) { + NameCallback nc = (NameCallback) callbacks[i]; + nc.setName(username); + } else if (callbacks[i] instanceof PasswordCallback) { + PasswordCallback pc = (PasswordCallback) callbacks[i]; + pc.setPassword(password.toCharArray()); + } else + throw new UnsupportedCallbackException(callbacks[i]); + } + } + +}