KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > spi > UsersLoginModule


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.auth.spi;
8
9 // $Id: UsersLoginModule.java,v 1.1 2004/03/30 21:21:11 tdiesler Exp $
10

11 import org.jboss.security.SimpleGroup;
12
13 import javax.security.auth.Subject JavaDoc;
14 import javax.security.auth.callback.CallbackHandler JavaDoc;
15 import javax.security.auth.login.LoginException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.security.acl.Group JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * A simple properties file based login module that consults a Java Properties
25  * formatted text files for username to password("users.properties") mapping.
26  * The name of the properties file may be overriden by the usersProperties option.
27  * The properties file are loaded during initialization using the thread context
28  * class loader. This means that these files can be placed into the J2EE
29  * deployment jar or the JBoss config directory.
30  *
31  * The users.properties file uses a format:
32  * username1=password1
33  * username2=password2
34  * ...
35  *
36  * to define all valid usernames and their corresponding passwords.
37  *
38  * @author Thomas.Diesler@jboss.org
39  * @version $Revision: 1.1 $
40  */

41 public class UsersLoginModule extends UsernamePasswordLoginModule
42 {
43    /** The name of the properties resource containing user/passwords */
44    private String JavaDoc usersRsrcName = "users.properties";
45    /** The users.properties values */
46    private Properties JavaDoc users;
47
48    /**
49     * Initialize this LoginModule.
50     * @param options the login module option map. Supported options include:
51     * usersProperties: The name of the properties resource containing
52     * user/passwords. The default is "users.properties"
53     */

54    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options)
55    {
56       super.initialize(subject, callbackHandler, sharedState, options);
57       try
58       {
59          // Check for usersProperties & rolesProperties
60
String JavaDoc option = (String JavaDoc) options.get("usersProperties");
61          if (option != null)
62             usersRsrcName = option;
63
64          // Load the properties file that contains the list of users and passwords
65
loadUsers();
66       }
67       catch (Exception JavaDoc e)
68       {
69          // Note that although this exception isn't passed on, users or roles will be null
70
// so that any call to login will throw a LoginException.
71
super.log.error("Failed to load users/passwords/role files", e);
72       }
73    }
74
75    /**
76     * Method to authenticate a Subject (phase 1). This validates that the
77     * users properties file were loaded and then calls
78     * super.login to perform the validation of the password.
79     *
80     * @exception javax.security.auth.login.LoginException thrown if the users or roles properties files
81     * were not found or the super.login method fails.
82     */

83    public boolean login() throws LoginException JavaDoc
84    {
85       if (users == null)
86          throw new LoginException JavaDoc("Missing users.properties file.");
87
88       return super.login();
89    }
90
91    /**
92     * Return a group Roles with no members
93     *
94     * @return Group[] containing the sets of roles
95     */

96    protected Group[] getRoleSets() throws LoginException JavaDoc
97    {
98       return new Group[0];
99    }
100
101    protected String JavaDoc getUsersPassword()
102    {
103       String JavaDoc username = getUsername();
104       String JavaDoc password = null;
105       if (username != null)
106          password = users.getProperty(username, null);
107       return password;
108    }
109
110    private void loadUsers() throws IOException JavaDoc
111    {
112       users = loadProperties(usersRsrcName);
113    }
114
115    /**
116     * Loads the given properties file and returns a Properties object containing the
117     * key,value pairs in that file.
118     * The properties files should be in the class path.
119     */

120    private Properties JavaDoc loadProperties(String JavaDoc propertiesName) throws IOException JavaDoc
121    {
122       Properties JavaDoc bundle = null;
123       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
124       URL JavaDoc url = loader.getResource(propertiesName);
125       if (url == null)
126          throw new IOException JavaDoc("Properties file " + propertiesName + " not found");
127
128       super.log.trace("Properties file=" + url);
129
130       InputStream JavaDoc is = url.openStream();
131       if (is != null)
132       {
133          bundle = new Properties JavaDoc();
134          bundle.load(is);
135       }
136       else
137       {
138          throw new IOException JavaDoc("Properties file " + propertiesName + " not avilable");
139       }
140       return bundle;
141    }
142 }
143
Popular Tags