KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > basic > FileLoginModule


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security.implementation.basic;
11
12 import java.util.Map JavaDoc;
13 import java.io.File JavaDoc;
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 /**
22  * Simple implemetation, to provide authentication from files...
23  * @javadoc
24  * @author Eduard Witteveen
25  * @version $Id: FileLoginModule.java,v 1.4.2.1 2006/09/22 14:40:59 andre Exp $
26  */

27 public class FileLoginModule implements LoginModule {
28     private static Logger log=Logging.getLoggerInstance(FileLoginModule.class.getName());
29     private File JavaDoc configFile = null;
30
31     public void load(Map JavaDoc properties) {
32         String JavaDoc passwordFile = (String JavaDoc)properties.get("file");
33
34         if (passwordFile == null || passwordFile.equals("")) {
35             configFile = new File JavaDoc(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 JavaDoc(passwordFile);
39         }
40
41         if (! configFile.isAbsolute()) {
42             File JavaDoc parentFile = (File JavaDoc) properties.get("_parentFile");
43             log.debug("" + configFile.getPath() + " is not absolute.");
44             configFile = new File JavaDoc(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 JavaDoc loginInfo, Object JavaDoc[] 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 JavaDoc accounts = reader.readProperties(configFile.getAbsolutePath());
72
73         if (accounts == null) {
74             log.error("Could not find accounts!");
75         }
76     
77         // do a list with usernames and passwords...
78
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 JavaDoc neededPass = (String JavaDoc) 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         // set the identifier
92
user.setIdentifier((String JavaDoc)loginInfo.get("username"));
93         
94         // Admins are admins
95
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