01: package com.technoetic.xplanner.security.module.ntlm;
02:
03: import java.util.Map;
04: import javax.security.auth.Subject;
05: import javax.servlet.http.HttpServletRequest;
06:
07: import jcifs.smb.SmbAuthException;
08: import jcifs.smb.SmbException;
09: import org.apache.log4j.Logger;
10:
11: import com.technoetic.xplanner.security.AuthenticationException;
12: import com.technoetic.xplanner.security.LoginModule;
13: import com.technoetic.xplanner.security.module.LoginSupport;
14: import com.technoetic.xplanner.security.module.LoginSupportImpl;
15:
16: public class NtlmLoginModule implements LoginModule {
17: private String domainController;
18: private String domain;
19: private String name;
20: private transient Logger log = Logger.getLogger(getClass());
21: transient LoginSupport support = new LoginSupportImpl();
22: transient NtlmLoginHelper helper = new NtlmLoginHelperImpl();
23: public static final String DOMAIN_KEY = "domain";
24: public static final String CONTROLLER_KEY = "controller";
25:
26: public NtlmLoginModule(LoginSupport loginSupport,
27: NtlmLoginHelper helper) {
28: this .support = loginSupport;
29: this .helper = helper;
30: }
31:
32: public void setOptions(Map options) {
33: domain = options.get(DOMAIN_KEY) != null ? (String) options
34: .get(DOMAIN_KEY) : "YANDEX";
35: domainController = options.get(CONTROLLER_KEY) != null ? (String) options
36: .get(CONTROLLER_KEY)
37: : domain;
38: log.debug("initialized");
39: }
40:
41: public Subject authenticate(String userId, String password)
42: throws AuthenticationException {
43: log.debug(ATTEMPTING_TO_AUTHENTICATE + this .getName() + " ("
44: + userId + ")");
45: try {
46: helper.authenticate(userId, password, domainController,
47: domain);
48:
49: } catch (SmbAuthException sae) {
50: log.error("NT domain did not authenticate user " + userId);
51: throw new AuthenticationException(
52: MESSAGE_AUTHENTICATION_FAILED_KEY);
53: } catch (SmbException se) {
54: log
55: .error("SmbException while authenticating "
56: + userId, se);
57: throw new AuthenticationException(MESSAGE_SERVER_ERROR_KEY);
58: } catch (java.net.UnknownHostException e) {
59: log.error("UnknownHostException while authenticating "
60: + userId, e);
61: throw new AuthenticationException(
62: MESSAGE_SERVER_NOT_FOUND_KEY);
63: }
64:
65: log.info("NT domain authenticated user " + userId);
66:
67: Subject subject = support.createSubject();
68: log.debug("looking for user: " + userId);
69: support.populateSubjectPrincipalFromDatabase(subject, userId);
70: log.debug(AUTHENTICATION_SUCCESFULL + this .getName());
71: return subject;
72: }
73:
74: public boolean isCapableOfChangingPasswords() {
75: return false;
76: }
77:
78: public void changePassword(String userId, String password)
79: throws AuthenticationException {
80: throw new UnsupportedOperationException(
81: "change Password not implemented");
82: }
83:
84: public void logout(HttpServletRequest request)
85: throws AuthenticationException {
86: request.getSession().invalidate();
87: }
88:
89: public String getName() {
90: return name;
91: }
92:
93: public void setName(String name) {
94: this.name = name;
95: }
96:
97: }
|