Index: topia/src/java/org/codelutin/topia/TopiaContext.java diff -u topia/src/java/org/codelutin/topia/TopiaContext.java:1.32 topia/src/java/org/codelutin/topia/TopiaContext.java:1.33 --- topia/src/java/org/codelutin/topia/TopiaContext.java:1.32 Wed Sep 15 15:26:22 2004 +++ topia/src/java/org/codelutin/topia/TopiaContext.java Fri Apr 29 16:00:39 2005 @@ -23,25 +23,31 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.32 $ + * @version $Revision: 1.33 $ * - * Mise a jour: $Date: 2004/09/15 15:26:22 $ - * par : $Author: bpoussin $ + * Mise a jour: $Date: 2005/04/29 16:00:39 $ + * par : $Author: thimel $ */ package org.codelutin.topia; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.Properties; +import java.util.Vector; + import org.codelutin.topia.distribution.AbstractDistributionHelper; -import org.codelutin.topia.persistence.AbstractPersistenceHelper; -import org.codelutin.topia.persistence.PersistenceHelper; import org.codelutin.topia.hook.HookHelper; +import org.codelutin.topia.persistence.PersistenceHelper; +import org.codelutin.topia.security.TopiaSecurityException; +import org.codelutin.topia.security.TopiaUserPrincipal; import org.codelutin.util.CategorisedListenerSet; -import java.lang.reflect.Constructor; -import java.util.logging.Logger; -import java.util.logging.Level; -import java.util.Properties; -public class TopiaContext { // TopiaContext +public abstract class TopiaContext { // TopiaContext protected PersistenceHelper persistenceHelper = null; protected AbstractDistributionHelper distributionHelper = null; @@ -218,6 +224,63 @@ TopiaEntity createEntity(Class entityClass) throws TopiaException { return (TopiaEntity)contextHelper.getInstance(entityClass); } - + + public List authenticate(String login, String password) + throws TopiaSecurityException { + String authType = properties.getProperty("topia.auth.type"); + if ("simple".equalsIgnoreCase(authType)) { + return simpleAuthentication(login, password); + } else if ("ldap".equalsIgnoreCase(authType)) { + return ldapAuthentication(login, password); + } else if ("xmi".equalsIgnoreCase(authType)) { + return xmiAuthentication(login, password); + } + throw new TopiaSecurityException("Invalid auth type : " + authType); + } + + protected List simpleAuthentication(String login, String password) + throws TopiaSecurityException { + Properties props = new Properties(); + String fileName = properties.getProperty("topia.auth.simple.file"); + if (fileName == null) + throw new TopiaSecurityException( + "Authentication filename must be specified"); + try { + props.load(new FileInputStream(fileName)); + } catch(FileNotFoundException e) { + throw new TopiaSecurityException( + "Invalid authentication file : " + fileName); + } catch(IOException ioe) { + throw new TopiaSecurityException( + "Unable to read authentication file : " + fileName); + } + String hashMode = properties.getProperty("topia.auth.simple.hash"); + if (hashMode != null) { + try { + password = new String(MessageDigest.getInstance(hashMode) + .digest(password.getBytes())); + } catch (NoSuchAlgorithmException nsaE) { + throw new TopiaSecurityException( + "Invalid hash algorithm : " +hashMode); + } + } + if (!password.equals(props.getProperty(login))) + throw new TopiaSecurityException("Wrong Login/Password"); + Vector principals = new Vector(); + principals.addElement(new TopiaUserPrincipal(login)); +// TODO Arno : ajouter la liste des GroupPrincipals + return principals; + } + + protected List ldapAuthentication(String login, String password) + throws TopiaSecurityException { + throw new TopiaSecurityException("ldapAuthentication not supported"); + //TODO Arno ;) + } + + protected abstract List xmiAuthentication(String login, String password) + throws TopiaSecurityException; + + } // TopiaContext