KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > service > PropertiesVerifierStore


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.service;
23
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.math.BigInteger JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.security.KeyException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Properties JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.Name JavaDoc;
35
36 import org.jboss.naming.NonSerializableFactory;
37 import org.jboss.security.Util;
38 import org.jboss.security.srp.SRPConf;
39 import org.jboss.security.srp.SRPVerifierStore;
40 import org.jboss.security.srp.SRPVerifierStore.VerifierInfo;
41 import org.jboss.system.ServiceMBeanSupport;
42
43 /** The PropertiesVerifierStore service is a SRPVerifierStore implementation
44  that obtains the username and password info from a properties file and then
45  creates an in memory SRPVerifierStore.
46
47 @author Scott.Stark@jboss.org
48 @version $Revision: 37406 $
49 */

50 public class PropertiesVerifierStore extends ServiceMBeanSupport
51    implements PropertiesVerifierStoreMBean, SRPVerifierStore
52 {
53    private String JavaDoc jndiName = "srp/DefaultVerifierSource";
54    private HashMap JavaDoc storeMap = new HashMap JavaDoc();
55    private Thread JavaDoc addUserThread;
56
57    /** Creates a new instance of PropertiesVerifierStore */
58    public PropertiesVerifierStore()
59    {
60    }
61
62    /** Get the jndi name for the SRPVerifierSource implementation binding.
63    */

64    public String JavaDoc getJndiName()
65    {
66      return jndiName;
67    }
68    /** set the jndi name for the SRPVerifierSource implementation binding.
69    */

70    public void setJndiName(String JavaDoc jndiName)
71    {
72       this.jndiName = jndiName;
73    }
74
75    protected void startService() throws Exception JavaDoc
76    {
77       // Make sure the security utility class is initialized
78
Util.init();
79
80       // Find the users.properties file
81
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
82       URL JavaDoc users = loader.getResource("users.properties");
83       if( users == null )
84          throw new FileNotFoundException JavaDoc("Failed to find users.properties resource");
85       InputStream JavaDoc is = users.openStream();
86       final Properties JavaDoc userPasswords = new Properties JavaDoc();
87       userPasswords.load(is);
88       is.close();
89       addUserThread = new Thread JavaDoc("AddUsers")
90       {
91          public void run()
92          {
93             Iterator JavaDoc keys = userPasswords.keySet().iterator();
94             while( keys.hasNext() )
95             {
96                String JavaDoc username = (String JavaDoc) keys.next();
97                char[] password = userPasswords.getProperty(username).toCharArray();
98                String JavaDoc cipherAlgorithm = "Blowfish";
99                String JavaDoc hashAlgorithm = "SHA_Interleave";
100                addUser(username, password, cipherAlgorithm, hashAlgorithm);
101                log.info("Added user: "+username);
102             }
103          }
104       };
105       addUserThread.start();
106
107       // Bind a reference to the SRPVerifierStore using NonSerializableFactory
108
InitialContext JavaDoc ctx = new InitialContext JavaDoc();
109       Name JavaDoc name = ctx.getNameParser("").parse(jndiName);
110       NonSerializableFactory.rebind(name, this, true);
111       log.debug("Bound SRPVerifierStore at "+jndiName);
112    }
113    protected void stopService() throws Exception JavaDoc
114    {
115       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
116       NonSerializableFactory.unbind(jndiName);
117       ctx.unbind(jndiName);
118       log.debug("Unbound SRPVerifierStore at "+jndiName);
119    }
120
121    public VerifierInfo getUserVerifier(String JavaDoc username) throws KeyException JavaDoc, IOException JavaDoc
122    {
123       if( addUserThread != null )
124       {
125          try
126          {
127             addUserThread.join();
128             addUserThread = null;
129          }
130          catch(InterruptedException JavaDoc e)
131          {
132          }
133       }
134       VerifierInfo info = (VerifierInfo) storeMap.get(username);
135       return info;
136    }
137    public void setUserVerifier(String JavaDoc username, VerifierInfo info) throws IOException JavaDoc
138    {
139       throw new IOException JavaDoc("PropertiesVerifierStore is read only");
140    }
141
142    public void verifyUserChallenge(String JavaDoc username, Object JavaDoc auxChallenge)
143          throws SecurityException JavaDoc
144    {
145    }
146
147    private void addUser(String JavaDoc username, char[] password, String JavaDoc cipherAlgorithm,
148       String JavaDoc hashAlgorithm)
149    {
150       VerifierInfo info = new VerifierInfo();
151       info.username = username;
152       // Create a random salt
153
long r = Util.nextLong();
154       String JavaDoc rs = Long.toHexString(r);
155       info.salt = rs.getBytes();
156       BigInteger JavaDoc g = SRPConf.getDefaultParams().g();
157       BigInteger JavaDoc N = SRPConf.getDefaultParams().N();
158       info.cipherAlgorithm = cipherAlgorithm;
159       info.hashAlgorithm = hashAlgorithm;
160
161       info.verifier = Util.calculateVerifier(username, password, info.salt, N, g);
162       info.g = g.toByteArray();
163       info.N = N.toByteArray();
164       storeMap.put(username, info);
165    }
166 }
167
Popular Tags