1 10 package org.mmbase.security.implementation.basic; 11 12 import java.util.Map ; 13 import java.io.File ; 14 15 import org.mmbase.security.Rank; 16 17 import org.mmbase.util.ExtendedProperties; 18 import org.mmbase.util.logging.Logger; 19 import org.mmbase.util.logging.Logging; 20 21 27 public class FileLoginModule implements LoginModule { 28 private static Logger log=Logging.getLoggerInstance(FileLoginModule.class.getName()); 29 private File configFile = null; 30 31 public void load(Map properties) { 32 String passwordFile = (String )properties.get("file"); 33 34 if (passwordFile == null || passwordFile.equals("")) { 35 configFile = new File (org.mmbase.module.core.MMBaseContext.getConfigPath() + java.io.File.separator + "accounts.properties"); 36 log.warn("property file not specified, now using as config file :" + configFile); 37 } else { 38 configFile = new File (passwordFile); 39 } 40 41 if (! configFile.isAbsolute()) { 42 File parentFile = (File ) properties.get("_parentFile"); 43 log.debug("" + configFile.getPath() + " is not absolute."); 44 configFile = new File (parentFile.getParent() + File.separator + configFile.getPath()); 45 log.debug("Trying " + configFile.getAbsolutePath()); 46 } 47 48 log.debug("trying to load file login modules with password file:" + configFile.getAbsolutePath()); 49 50 if ( ! configFile.exists() ) { 51 log.error("file: '"+configFile+"' did not exist."); 52 throw new org.mmbase.security.SecurityException("file: '"+configFile+"' did not exist."); 53 } 54 if ( ! configFile.isFile() ) { 55 log.error("file: '"+configFile+"' is not a file."); 56 throw new org.mmbase.security.SecurityException("file: '"+configFile+"' is not a file."); 57 } 58 if ( ! configFile.canRead() ) { 59 log.error("file: '"+configFile+"' is not readable."); 60 throw new org.mmbase.security.SecurityException("file: '"+configFile+"' is not readable."); 61 } 62 log.debug("file login loaded"); 63 } 64 65 public boolean login(NameContext user, Map loginInfo, Object [] parameters) { 66 if(!loginInfo.containsKey("username")) throw new org.mmbase.security.SecurityException("key 'username' not found in login information"); 67 if(!loginInfo.containsKey("password")) throw new org.mmbase.security.SecurityException("key 'password' not found in login information"); 68 ExtendedProperties reader = new ExtendedProperties(); 69 70 log.debug("reading accounts from " + configFile); 71 java.util.Hashtable accounts = reader.readProperties(configFile.getAbsolutePath()); 72 73 if (accounts == null) { 74 log.error("Could not find accounts!"); 75 } 76 77 log.debug("There are "+accounts.size()+" users which can logon to our system"); 79 if ( !accounts.containsKey(loginInfo.get("username"))) { 80 log.debug("username: '"+loginInfo.get("username")+"' not found"); 81 return false; 82 } 83 String neededPass = (String ) accounts.get(loginInfo.get("username")); 84 if (!neededPass.equals(loginInfo.get("password"))) { 85 log.debug("username/password combination invalid(in HashTable values user:'"+ 86 loginInfo.get("username") + "' password:'" + 87 loginInfo.get("password") + "')"); 88 return false; 89 } 90 91 user.setIdentifier((String )loginInfo.get("username")); 93 94 if ("admin".equals(loginInfo.get("username"))) { 96 user.setRank(Rank.getRank("administrator")); 97 } 98 99 log.info("user: '" + loginInfo.get("username") + "' passed this login module"); 100 return true; 101 } 102 } 103 | Popular Tags |