1 22 package org.jboss.test.security.service; 23 24 import java.io.FileNotFoundException ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.math.BigInteger ; 28 import java.net.URL ; 29 import java.security.KeyException ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Properties ; 33 import javax.naming.InitialContext ; 34 import javax.naming.Name ; 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 50 public class PropertiesVerifierStore extends ServiceMBeanSupport 51 implements PropertiesVerifierStoreMBean, SRPVerifierStore 52 { 53 private String jndiName = "srp/DefaultVerifierSource"; 54 private HashMap storeMap = new HashMap (); 55 private Thread addUserThread; 56 57 58 public PropertiesVerifierStore() 59 { 60 } 61 62 64 public String getJndiName() 65 { 66 return jndiName; 67 } 68 70 public void setJndiName(String jndiName) 71 { 72 this.jndiName = jndiName; 73 } 74 75 protected void startService() throws Exception 76 { 77 Util.init(); 79 80 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 82 URL users = loader.getResource("users.properties"); 83 if( users == null ) 84 throw new FileNotFoundException ("Failed to find users.properties resource"); 85 InputStream is = users.openStream(); 86 final Properties userPasswords = new Properties (); 87 userPasswords.load(is); 88 is.close(); 89 addUserThread = new Thread ("AddUsers") 90 { 91 public void run() 92 { 93 Iterator keys = userPasswords.keySet().iterator(); 94 while( keys.hasNext() ) 95 { 96 String username = (String ) keys.next(); 97 char[] password = userPasswords.getProperty(username).toCharArray(); 98 String cipherAlgorithm = "Blowfish"; 99 String hashAlgorithm = "SHA_Interleave"; 100 addUser(username, password, cipherAlgorithm, hashAlgorithm); 101 log.info("Added user: "+username); 102 } 103 } 104 }; 105 addUserThread.start(); 106 107 InitialContext ctx = new InitialContext (); 109 Name 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 114 { 115 InitialContext ctx = new InitialContext (); 116 NonSerializableFactory.unbind(jndiName); 117 ctx.unbind(jndiName); 118 log.debug("Unbound SRPVerifierStore at "+jndiName); 119 } 120 121 public VerifierInfo getUserVerifier(String username) throws KeyException , IOException 122 { 123 if( addUserThread != null ) 124 { 125 try 126 { 127 addUserThread.join(); 128 addUserThread = null; 129 } 130 catch(InterruptedException e) 131 { 132 } 133 } 134 VerifierInfo info = (VerifierInfo) storeMap.get(username); 135 return info; 136 } 137 public void setUserVerifier(String username, VerifierInfo info) throws IOException 138 { 139 throw new IOException ("PropertiesVerifierStore is read only"); 140 } 141 142 public void verifyUserChallenge(String username, Object auxChallenge) 143 throws SecurityException 144 { 145 } 146 147 private void addUser(String username, char[] password, String cipherAlgorithm, 148 String hashAlgorithm) 149 { 150 VerifierInfo info = new VerifierInfo(); 151 info.username = username; 152 long r = Util.nextLong(); 154 String rs = Long.toHexString(r); 155 info.salt = rs.getBytes(); 156 BigInteger g = SRPConf.getDefaultParams().g(); 157 BigInteger 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 |