Index: topia/src/java/org/codelutin/topia/security/TopiaSecurityHelper.java diff -u topia/src/java/org/codelutin/topia/security/TopiaSecurityHelper.java:1.4 topia/src/java/org/codelutin/topia/security/TopiaSecurityHelper.java:1.5 --- topia/src/java/org/codelutin/topia/security/TopiaSecurityHelper.java:1.4 Thu Jul 21 17:10:38 2005 +++ topia/src/java/org/codelutin/topia/security/TopiaSecurityHelper.java Fri Jul 22 16:57:57 2005 @@ -23,9 +23,9 @@ * Created: 15 juillet 2005 17:39:35 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * - * Last update: $Date: 2005/07/21 17:10:38 $ + * Last update: $Date: 2005/07/22 16:57:57 $ * by : $Author: thimel $ */ @@ -37,14 +37,14 @@ import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.Properties; import java.util.StringTokenizer; -import java.util.Vector; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.Configuration; @@ -52,7 +52,6 @@ import javax.security.auth.login.LoginException; import org.codelutin.topia.TopiaContext; -import org.codelutin.topia.TopiaNotFoundException; import org.codelutin.util.HashMapMultiKey; import org.codelutin.util.ListenerSet; @@ -76,9 +75,13 @@ this.properties = properties; initSecurity(); } - + + /** + * Initialise les paramètres de sécurité nécessaires à l'authentification + * et à l'autorisation + */ private void initSecurity() { - String appEntryName = properties.getProperty("topia.auth.loginEntry"); + String appEntryName = context.getContextName(); Configuration conf = new TopiaConfiguration(appEntryName, this); Configuration.setConfiguration(conf); TopiaPolicy policy = new TopiaPolicy(); @@ -94,7 +97,7 @@ * @return la liste des TopiaPrincipal de l'utilisateur * @throws TopiaSecurityException */ - public List authenticate(String login, String password) + public List authenticate(String login, String password) throws TopiaSecurityException { String authType = properties.getProperty("topia.auth.type"); if ("simple".equalsIgnoreCase(authType)) { @@ -114,15 +117,16 @@ * @return la liste des TopiaPrincipal de l'utilisateur * @throws TopiaSecurityException */ - protected List simpleAuthentication(String login, String password) + protected List simpleAuthentication(String login, String password) throws TopiaSecurityException { - Properties props = new Properties(); + //Récupère le fichier contenants les utilisateurs et les mots de passe + Properties users = new Properties(); String fileName = properties.getProperty("topia.auth.simple.file.login"); if (fileName == null) throw new TopiaSecurityException( - "Authentication filename must be specified"); + "Authentication filename must be specified"); try { - props.load(new FileInputStream(fileName)); + users.load(new FileInputStream(fileName)); } catch (FileNotFoundException e) { throw new TopiaSecurityException("Invalid authentication file : " + fileName); @@ -130,6 +134,8 @@ throw new TopiaSecurityException( "Unable to read authentication file : " + fileName); } + //Vérifie dans les propriétés s'il faut faire un hachage du password + //pour la vérification et si c'est le cas, hache le password String hashMode = properties.getProperty("topia.auth.simple.hash"); if (hashMode != null) { try { @@ -140,27 +146,36 @@ + hashMode, eee); } } - if (!password.equals(props.getProperty(login))) + // Vérifie si le couple user / password obtenu est correct + if (!password.equals(users.getProperty(login))) throw new TopiaSecurityException("Wrong Login/Password"); - Vector principals = new Vector(); - principals.addElement(new TopiaUserPrincipal(login)); - + //Créé la liste des principals de l'utilisateur loggué ... + List principals = new ArrayList(); + // Ajoute le TopiaUserPrincipal (identifiant l'utilisateur), ... + principals.add(new TopiaUserPrincipal(login)); + // Récupère le fichier contenant les groupes String groupsFileName = properties.getProperty("topia.auth.simple.file.groups"); - props = new Properties(); + Properties groups = new Properties(); try { - props.load(new FileInputStream(groupsFileName)); + groups.load(new FileInputStream(groupsFileName)); } catch (FileNotFoundException e1) { - e1.printStackTrace(); + log.log(Level.WARNING, "Le fichier contenant les groupes n'a pas" + + " été trouvé, aucun groupe ajouté pour l'utilisateur : " + + login); } catch (IOException e1) { - e1.printStackTrace(); + log.log(Level.WARNING, "Erreur pendant la lecture du fichier" + + "contenant les groupes, aucun groupe ajouté pour" + + "l'utilisateur : " + login); } - for (Enumeration e = props.keys(); e.hasMoreElements();) { + // Parcours des groupes lus dans le fichier + for (Enumeration e = groups.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); - - for (StringTokenizer sTK = new StringTokenizer(props.getProperty(key), - ","); sTK.hasMoreTokens();) { + // Récupère les utilisateurs séparés par des "," + for (StringTokenizer sTK = new StringTokenizer( + groups.getProperty(key), ","); sTK.hasMoreTokens();) { + // Si le login fait partie du groupe, on ajoute le principal associé! if (login.equals(sTK.nextToken().trim())) { - principals.addElement(new TopiaGroupPrincipal(key)); + principals.add(new TopiaGroupPrincipal(key)); break; } } @@ -175,7 +190,7 @@ * @return la liste des TopiaPrincipal de l'utilisateur * @throws TopiaSecurityException */ - protected List ldapAuthentication(String login, String password) + protected List ldapAuthentication(String login, String password) throws TopiaSecurityException { throw new TopiaSecurityException("ldapAuthentication not supported"); //TODO Arno ;) @@ -188,7 +203,7 @@ * @return la liste des TopiaPrincipal de l'utilisateur * @throws TopiaSecurityException */ - // protected abstract List xmiAuthentication(String login, String password) + // protected abstract List xmiAuthentication(String login, String password) // throws TopiaSecurityException; /** @@ -222,16 +237,16 @@ HashMapMultiKey.Key multiKey = new HashMapMultiKey.Key().add(action); for (Enumeration en = props.keys(); en.hasMoreElements();) { String key = (String) en.nextElement(); - Vector propValues = new Vector(); + List propValues = new ArrayList(); String principals = props.getProperty(key).replaceAll(" +", " "); StringTokenizer sTK = new StringTokenizer(principals, ";"); while (sTK.hasMoreTokens()) try { principals = sTK.nextToken(); TopiaPermission perm = - new TopiaPermission(key + " " + principals, action); + new TopiaPermission(key + " " + principals, action); addNoSecurityPermission(perm, true); - propValues.addElement(perm); + propValues.add(perm); } catch (TopiaSecurityException e1) { e1.printStackTrace(); } @@ -264,16 +279,15 @@ */ private void storePermissions(String fileName, String action) { Properties props = new Properties(); - List list = permissions.getKeys(action); - for (Iterator it = list.iterator(); it.hasNext(); ) { + List keys = permissions.getKeys(action); + for (Iterator it = keys.iterator(); it.hasNext(); ) { HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); String propKey = (String)key.get(1); String value = ""; - Vector values = (Vector)permissions.get(key); + List values = (List)permissions.get(key); if (values == null || values.size()==0) continue; - for (Enumeration en = values.elements(); en.hasMoreElements(); ) { - TopiaPermission perm = (TopiaPermission)en.nextElement(); + for (TopiaPermission perm : values) { value += perm.principalsToString() + ";"; } props.put(propKey, value.substring(0, value.lastIndexOf(";"))); @@ -309,9 +323,9 @@ throws TopiaSecurityException { HashMapMultiKey.Key key; key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); - Vector perms = (Vector)permissions.get(key); + List perms = (List)permissions.get(key); if (perms == null) - perms = new Vector(); + perms = new ArrayList(); if (!perms.contains(perm)) perms.add(perm); permissions.put(key, perms); @@ -335,13 +349,13 @@ public void modifyPermission(TopiaPermission perm) throws TopiaSecurityException { TopiaAccessController.checkPermission("perm", "admin"); - List list = permissions.getKeys(perm.getId()); + List keys = permissions.getKeys(perm.getId()); boolean found = false; - for (Iterator it = list.iterator(); !found && it.hasNext(); ) { - HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); - Vector perms = (Vector)permissions.get(key); - for (Enumeration en = perms.elements(); !found && en.hasMoreElements(); ) { - TopiaPermission topiaPerm = (TopiaPermission)en.nextElement(); + for (Iterator itKeys = keys.iterator(); !found && itKeys.hasNext(); ) { + HashMapMultiKey.Key key = (HashMapMultiKey.Key)itKeys.next(); + List perms = (List)permissions.get(key); + for (Iterator itPerms = perms.iterator(); !found && itPerms.hasNext(); ) { + TopiaPermission topiaPerm = itPerms.next(); if (topiaPerm.getPrincipals().equals(perm.getPrincipals())) { found = true; if (!topiaPerm.getActions().equals(perm.getActions())) { @@ -353,7 +367,6 @@ } } } - permFileModified = true; try { permissionListeners.fire("permissionModified", @@ -374,7 +387,7 @@ TopiaAccessController.checkPermission("perm", "admin"); HashMapMultiKey.Key key; key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); - Vector perms = (Vector)permissions.get(key); + List perms = (List)permissions.get(key); if ((perms != null) && (perms.contains(perm))) { permFileModified = true; perms.remove(perm); @@ -390,14 +403,29 @@ } } + /** + * Authentifie l'utilisateur avec les login et mot de passe spécifiés puis + * renvoie le LoginContext utilisé + * @param login + * @param password + * @return Le LoginContext utilisé pour la procédure de login + * @throws LoginException Si une erreur survient pendant le login + */ public LoginContext login(String login, String password) throws LoginException { - LoginContext ctx = new LoginContext(properties.getProperty("topia.auth.loginEntry"), new TopiaSimpleCallbackHandler(login, password)); + LoginContext ctx = new LoginContext(context.getContextName(), new TopiaSimpleCallbackHandler(login, password)); ctx.login(); return ctx; } + /** + * Authentifie l'utilisateur grâce au CallbackHandler spécifié puis + * renvoie le LoginContext utilisé + * @param callbackHandler pour récupérer les paramètres d'authentification + * @return Le LoginContext utilisé pour la procédure de login + * @throws LoginException Si une erreur survient pendant le login + */ public LoginContext login(CallbackHandler callbackHandler) throws LoginException { - LoginContext ctx = new LoginContext(properties.getProperty("topia.auth.loginEntry"), callbackHandler); + LoginContext ctx = new LoginContext(context.getContextName(), callbackHandler); ctx.login(); return ctx; }