KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > auth > AuthenticationFactory


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.auth;
37
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.LinkedList JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45 import java.util.regex.Matcher JavaDoc;
46 import java.util.regex.Pattern JavaDoc;
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 /**
56  * Factory for the AuthentictionMechanism of the SASL
57  * authentication process. New AuthenticationMechanisms
58  * can be implemented and registered with this Factory.
59  * <br>
60  * Ristretto supports PLAIN, LOGIN and DIGEST-MD5.
61  * <br>
62  * <b>RFC(s)</b> 2222
63  *
64  * @author Timo Stich <tstich@users.sourceforge.net>
65  */

66 public class AuthenticationFactory {
67
68     /** JDK 1.4+ logging framework logger, used for logging. */
69     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.ristretto.auth");
70
71     
72     private static final Pattern JavaDoc authTokenizerPattern = Pattern.compile("\\b([^\\s]+)\\b");
73     
74     private Map JavaDoc authTable;
75     private static AuthenticationFactory myInstance;
76
77     private AuthenticationFactory() {
78         authTable = new Hashtable JavaDoc();
79         
80         // add Factory implemented AuthenticationMechanisms
81
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     /**
91      * Gets the singleton instance of the AuthenticationFactory.
92      *
93      * @return the singleton instance
94      */

95     public static AuthenticationFactory getInstance() {
96         if( myInstance == null ) {
97             myInstance = new AuthenticationFactory();
98         }
99         
100         return myInstance;
101     }
102
103     /**
104      * Adds a new AuthenticationMechanism to the Factory.
105      *
106      * @param name the SASL registered name of the mechanism
107      * @param auth the implementation of the SASL mechanism
108      */

109     public void addAuthentication(String JavaDoc name, Class JavaDoc auth) {
110         authTable.put(name, auth);
111     }
112
113     /**
114      * Gets a new instance of the AuthenticationMechanism
115      * which implements the specified SASL mechanism.
116      *
117      * @param name the SASL registered name of the mechanism
118      * @return a new instance of an AuthenticationMechanism
119      * @throws NoSuchAuthenticationException if no implementation of the specified
120      * mechanism can be found
121      */

122     public AuthenticationMechanism getAuthentication(String JavaDoc name) throws NoSuchAuthenticationException {
123         AuthenticationMechanism auth;
124         if( !authTable.containsKey(name) ) throw new NoSuchAuthenticationException( name );
125         
126         try {
127             auth = (AuthenticationMechanism) ((Class JavaDoc)authTable.get(name)).newInstance();
128         } catch (InstantiationException JavaDoc e) {
129            throw new NoSuchAuthenticationException(e);
130         } catch (IllegalAccessException JavaDoc e) {
131             throw new NoSuchAuthenticationException(e);
132         }
133         
134         return auth;
135     }
136
137     /**
138      * Checks if the specified mechanism is
139      * supported by the Factory.
140      *
141      * @param mechanism the SASL name of the mechanism
142      * @return true if an implementation of the mechanism
143      * is registered with this Factory.
144      */

145     public boolean isSupported(String JavaDoc mechanism) {
146         return authTable.get(mechanism) != null;
147     }
148     
149     /**
150      * Gets a List of the supported Mechanisms.
151      *
152      * @return a list of the SASL registered names
153      * of the supported mechanisms.
154      */

155     public List JavaDoc getSupportedMechanisms() {
156         List JavaDoc list = new LinkedList JavaDoc();
157         Set JavaDoc keys = authTable.keySet();
158         Iterator JavaDoc it = keys.iterator();
159
160         while(it.hasNext()) {
161             list.add(it.next());
162         }
163         
164         return list;
165     }
166
167     /**
168      * Gets a List of the supported Mechanisms from server and client.
169      *
170      * @param authCapa the CAPA response from a server.
171      * @return a list of the SASL registered names
172      * of the supported mechanisms.
173      */

174     public List JavaDoc getSupportedMechanisms(String JavaDoc authCapa) {
175         List JavaDoc list = new LinkedList JavaDoc();
176         
177         Matcher JavaDoc matcher = authTokenizerPattern.matcher( authCapa );
178         
179         // First token is AUTH
180
matcher.find();
181         
182         // Search for a supported Authentication by both
183
// client and server
184
while( matcher.find() ) {
185             if( isSupported( matcher.group(1) )) {
186                 list.add( matcher.group(1) );
187             }
188         }
189         
190         return list;
191     }
192
193     /**
194      * Gets the securest supported SASL mechanism of the
195      * specified. The assumption hereby is that the
196      * given SASL mechanims are sorted securest first.
197      * <br><br>
198      * <b>Note:</b> This method can be convieniently used with the
199      * capability reponses from a POP3, IMAP or SMTP
200      * server.
201      * <br><br>
202      * <b>Example:</b>
203      * <br><code>getSecurestMethod("DIGEST-MD5 LOGIN PLAIN")
204      * <br>returns "DIGEST-MD5".</code>
205      *
206      * @param authCapability a whitespace separated list of SASL mechanisms
207      * @return the first supported SASL mechanism
208      * @throws NoSuchAuthenticationException
209      */

210     public String JavaDoc getSecurestMethod(String JavaDoc authCapability) throws NoSuchAuthenticationException {
211         Matcher JavaDoc matcher = authTokenizerPattern.matcher( authCapability );
212         
213         // First token is AUTH
214
matcher.find();
215         
216         // Search for a supported Authentication and
217
// return the first == securest Method found
218
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