KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authentication > ldap > LDAPAuthenticationComponentImpl


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.authentication.ldap;
18
19 import javax.naming.NamingException JavaDoc;
20 import javax.naming.directory.InitialDirContext JavaDoc;
21
22 import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent;
23 import org.alfresco.repo.security.authentication.AuthenticationException;
24
25 /**
26  * Currently expects the cn name of the user which is in a fixed location.
27  *
28  * @author Andy Hind
29  */

30 public class LDAPAuthenticationComponentImpl extends AbstractAuthenticationComponent
31 {
32     
33     private String JavaDoc userNameFormat;
34     
35     private LDAPInitialDirContextFactory ldapInitialContextFactory;
36     
37     public LDAPAuthenticationComponentImpl()
38     {
39         super();
40     }
41
42     
43     public void setLDAPInitialDirContextFactory(LDAPInitialDirContextFactory ldapInitialDirContextFactory)
44     {
45         this.ldapInitialContextFactory = ldapInitialDirContextFactory;
46     }
47     
48     
49     public void setUserNameFormat(String JavaDoc userNameFormat)
50     {
51         this.userNameFormat = userNameFormat;
52     }
53     
54     /**
55      * Implement the authentication method
56      */

57     public void authenticate(String JavaDoc userName, char[] password) throws AuthenticationException
58     {
59         InitialDirContext JavaDoc ctx = null;
60         try
61         {
62             ctx = ldapInitialContextFactory.getInitialDirContext(String.format(userNameFormat, new Object JavaDoc[]{userName}), new String JavaDoc(password));
63             
64             // Authentication has been successful.
65
// Set the current user, they are now authenticated.
66
setCurrentUser(userName);
67             
68         }
69         finally
70         {
71             if(ctx != null)
72             {
73                 try
74                 {
75                     ctx.close();
76                 }
77                 catch (NamingException JavaDoc e)
78                 {
79                     clearCurrentSecurityContext();
80                     throw new AuthenticationException("Failed to close connection", e);
81                 }
82             }
83         }
84     }
85
86
87     @Override JavaDoc
88     protected boolean implementationAllowsGuestLogin()
89     {
90         InitialDirContext JavaDoc ctx = null;
91         try
92         {
93             ctx = ldapInitialContextFactory.getDefaultIntialDirContext();
94             return true;
95             
96         }
97         catch(Exception JavaDoc e)
98         {
99             return false;
100         }
101         finally
102         {
103             if(ctx != null)
104             {
105                 try
106                 {
107                     ctx.close();
108                 }
109                 catch (NamingException JavaDoc e)
110                 {
111                     throw new AuthenticationException("Failed to close connection", e);
112                 }
113             }
114         }
115     }
116     
117     
118 }
119
Popular Tags