1 36 package org.columba.ristretto.auth; 37 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.LinkedList ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.Set ; 44 import java.util.logging.Logger ; 45 import java.util.regex.Matcher ; 46 import java.util.regex.Pattern ; 47 48 import org.columba.ristretto.auth.mechanism.CramMD5Mechanism; 49 import org.columba.ristretto.auth.mechanism.DigestMD5Mechanism; 50 import org.columba.ristretto.auth.mechanism.LoginMechanism; 51 import org.columba.ristretto.auth.mechanism.PlainMechanism; 52 import org.columba.ristretto.auth.mechanism.SaslWrapper; 53 54 55 66 public class AuthenticationFactory { 67 68 69 private static final Logger LOG = Logger.getLogger("org.columba.ristretto.auth"); 70 71 72 private static final Pattern authTokenizerPattern = Pattern.compile("\\b([^\\s]+)\\b"); 73 74 private Map authTable; 75 private static AuthenticationFactory myInstance; 76 77 private AuthenticationFactory() { 78 authTable = new Hashtable (); 79 80 addAuthentication("PLAIN", PlainMechanism.class); 82 addAuthentication("LOGIN", LoginMechanism.class); 83 84 if( SaslWrapper.available() ) { 85 addAuthentication("DIGEST-MD5", DigestMD5Mechanism.class); 86 addAuthentication("CRAM-MD5", CramMD5Mechanism.class); 87 } 88 } 89 90 95 public static AuthenticationFactory getInstance() { 96 if( myInstance == null ) { 97 myInstance = new AuthenticationFactory(); 98 } 99 100 return myInstance; 101 } 102 103 109 public void addAuthentication(String name, Class auth) { 110 authTable.put(name, auth); 111 } 112 113 122 public AuthenticationMechanism getAuthentication(String name) throws NoSuchAuthenticationException { 123 AuthenticationMechanism auth; 124 if( !authTable.containsKey(name) ) throw new NoSuchAuthenticationException( name ); 125 126 try { 127 auth = (AuthenticationMechanism) ((Class )authTable.get(name)).newInstance(); 128 } catch (InstantiationException e) { 129 throw new NoSuchAuthenticationException(e); 130 } catch (IllegalAccessException e) { 131 throw new NoSuchAuthenticationException(e); 132 } 133 134 return auth; 135 } 136 137 145 public boolean isSupported(String mechanism) { 146 return authTable.get(mechanism) != null; 147 } 148 149 155 public List getSupportedMechanisms() { 156 List list = new LinkedList (); 157 Set keys = authTable.keySet(); 158 Iterator it = keys.iterator(); 159 160 while(it.hasNext()) { 161 list.add(it.next()); 162 } 163 164 return list; 165 } 166 167 174 public List getSupportedMechanisms(String authCapa) { 175 List list = new LinkedList (); 176 177 Matcher matcher = authTokenizerPattern.matcher( authCapa ); 178 179 matcher.find(); 181 182 while( matcher.find() ) { 185 if( isSupported( matcher.group(1) )) { 186 list.add( matcher.group(1) ); 187 } 188 } 189 190 return list; 191 } 192 193 210 public String getSecurestMethod(String authCapability) throws NoSuchAuthenticationException { 211 Matcher matcher = authTokenizerPattern.matcher( authCapability ); 212 213 matcher.find(); 215 216 while( matcher.find() ) { 219 if( isSupported( matcher.group(1) )) { 220 return matcher.group(1); 221 } 222 } 223 224 throw new NoSuchAuthenticationException( authCapability ); 225 } 226 227 } 228 | Popular Tags |