KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > authentication > impl > LdapAuthenticationScheme


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.authentication.impl;
17
18 import org.outerj.daisy.authentication.AuthenticationScheme;
19 import org.outerj.daisy.authentication.AuthenticationException;
20 import org.outerj.daisy.authentication.UserCreator;
21 import org.outerj.daisy.repository.Credentials;
22 import org.outerj.daisy.repository.user.User;
23 import org.outerj.daisy.repository.user.UserManager;
24 import org.apache.avalon.framework.logger.Logger;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.naming.directory.InitialDirContext JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class LdapAuthenticationScheme implements AuthenticationScheme {
34     private final String JavaDoc name;
35     private final String JavaDoc description;
36     private final Map JavaDoc templateEnvironment;
37     private final UserCreator userCreator;
38     private final Logger logger;
39
40     public LdapAuthenticationScheme(String JavaDoc name, String JavaDoc description, Map JavaDoc templateEnvironment, UserCreator userCreator, Logger logger) {
41         this.name = name;
42         this.description = description;
43         this.templateEnvironment = templateEnvironment;
44         this.userCreator = userCreator;
45         this.logger = logger;
46     }
47     
48     public String JavaDoc getName() {
49         return name;
50     }
51
52     public String JavaDoc getDescription() {
53         return description;
54     }
55
56     public void clearCaches() {
57         // do nothing
58
}
59
60     public boolean check(Credentials credentials) throws AuthenticationException {
61         Hashtable JavaDoc env = new Hashtable JavaDoc();
62         Iterator JavaDoc templateEnvIt = templateEnvironment.entrySet().iterator();
63         while (templateEnvIt.hasNext()) {
64             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)templateEnvIt.next();
65             String JavaDoc key = (String JavaDoc)entry.getKey();
66             String JavaDoc value = (String JavaDoc)entry.getValue();
67             if (key.equals(Context.SECURITY_PRINCIPAL)) {
68                 value = value.replaceAll("\\$daisyLogin", credentials.getLogin());
69             }
70             env.put(key, value);
71         }
72
73         // Reject empty or whitespace passwords
74
String JavaDoc password = credentials.getPassword();
75         if (password == null || password.trim().length() == 0) {
76             return false;
77         }
78         env.put(Context.SECURITY_CREDENTIALS, password);
79
80         try {
81             new InitialDirContext JavaDoc(env);
82         } catch (NamingException JavaDoc e) {
83             if (logger.isDebugEnabled()) {
84                 env.put(Context.SECURITY_CREDENTIALS, "***REMOVED ON PURPOSE***");
85                 logger.debug("Failed to authenticate user with following environment: " + env, e);
86             }
87             return false;
88         }
89         return true;
90     }
91
92     public User createUser(Credentials crendentials, UserManager userManager) throws AuthenticationException {
93         if (userCreator != null) {
94             if (check(crendentials)) {
95                 return userCreator.create(crendentials.getLogin(), userManager);
96             }
97         }
98         return null;
99     }
100 }
101
Popular Tags