KickJava   Java API By Example, From Geeks To Geeks.

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


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 jcifs.smb.SmbException;
25 import jcifs.smb.SmbAuthException;
26 import jcifs.smb.SmbSession;
27 import jcifs.smb.NtlmPasswordAuthentication;
28 import jcifs.UniAddress;
29
30 import java.net.UnknownHostException JavaDoc;
31
32 public class NtlmAuthenticationScheme implements AuthenticationScheme {
33     private final String JavaDoc name;
34     private final String JavaDoc description;
35     private final String JavaDoc domainControllerAddress;
36     private final String JavaDoc domain;
37     private final UserCreator userCreator;
38
39     public NtlmAuthenticationScheme(String JavaDoc name, String JavaDoc description, String JavaDoc domainControllerAddress, String JavaDoc domain, UserCreator userCreator) {
40         this.name = name;
41         this.description = description;
42         this.domainControllerAddress = domainControllerAddress;
43         this.domain = domain;
44         this.userCreator = userCreator;
45     }
46
47     public String JavaDoc getName() {
48         return name;
49     }
50
51     public String JavaDoc getDescription() {
52         return description;
53     }
54
55     public boolean check(Credentials credentials) throws AuthenticationException {
56         UniAddress mydomaincontroller = null;
57         try {
58             mydomaincontroller = UniAddress.getByName(domainControllerAddress);
59         } catch (UnknownHostException JavaDoc e) {
60             throw new AuthenticationException("Error authenticating using NTLM.", e);
61         }
62         NtlmPasswordAuthentication mycreds = new NtlmPasswordAuthentication(domain, credentials.getLogin(), credentials.getPassword());
63         try {
64             SmbSession.logon( mydomaincontroller, mycreds );
65             // SUCCESS
66
return true;
67         } catch( SmbAuthException sae ) {
68             // AUTHENTICATION FAILURE
69
return false;
70         } catch( SmbException se ) {
71             // NETWORK PROBLEMS?
72
throw new AuthenticationException("Error authenticating using NTLM.", se);
73         }
74     }
75
76     public void clearCaches() {
77         // do nothing
78
}
79
80     public User createUser(Credentials crendentials, UserManager userManager) throws AuthenticationException {
81         if (userCreator != null) {
82             if (check(crendentials)) {
83                 return userCreator.create(crendentials.getLogin(), userManager);
84             }
85         }
86         return null;
87     }
88 }
89
Popular Tags