1
2
3
4 package org.itracker.services.authentication.adsson;
5
6 import org.apache.log4j.Logger;
7 import org.itracker.model.User;
8 import org.itracker.UserException;
9 import org.itracker.model.UserPreferences;
10 import org.itracker.services.UserService;
11 import org.itracker.services.authentication.DefaultAuthenticator;
12 import org.itracker.services.exceptions.AuthenticatorException;
13 import org.itracker.model.util.UserUtilities;
14
15 import javax.servlet.http.HttpServletRequest;
16 import java.rmi.RemoteException;
17 import java.util.Date;
18
19
20
21
22
23
24
25
26
27
28 public abstract class WindowsSSONAuthenticator extends DefaultAuthenticator {
29
30 private static final Logger logger = Logger.getLogger(WindowsSSONAuthenticator.class);
31
32 private static String TEMPLATE_USER = "TemplateUser";
33
34
35
36
37
38 public User checkLogin(String login, Object authentication, int authType, int reqSource)
39 throws AuthenticatorException {
40 User userModel;
41 try {
42
43
44 if (authType != AUTH_TYPE_REQUEST || !(authentication instanceof HttpServletRequest)) {
45 logger.error("Only http request authentication supported by this single sign on class. Received "
46 + authType);
47 throw new AuthenticatorException(
48 "Only http request authentication supported by this single sign on class",
49 AuthenticatorException.INVALID_AUTHENTICATION_TYPE);
50 }
51 UserService userService = getUserService();
52
53
54
55 String theLogin = ((HttpServletRequest) authentication).getRemoteUser();
56
57 if (theLogin == null) {
58 throw new AuthenticatorException("User obtained from jcifs is null. Check that jcifs is active",
59 AuthenticatorException.CUSTOM_ERROR);
60 }
61
62
63 if (theLogin.indexOf("\\") > 0) {
64 theLogin = theLogin.substring(theLogin.indexOf("\\") + 1);
65 }
66 if (!theLogin.equals(login)) {
67
68 AuthenticatorExceptionenticatorException.html#AuthenticatorException">AuthenticatorException ex = new AuthenticatorException("User obtained from authenticator does not match, got " + theLogin + ", expected " + login + ".",
69 AuthenticatorException.CUSTOM_ERROR);
70 logger.warn("checkLogin: checking login for " + login + " but got " + theLogin + " in authentication " + authentication, ex);
71 throw ex;
72 }
73
74 userModel = updateOrCreateUser(theLogin, userService);
75 return userModel;
76 } catch (RemoteException ex) {
77 logger.error("pt_PT", ex);
78 throw new AuthenticatorException(ex.getMessage(), AuthenticatorException.SYSTEM_ERROR, ex);
79 } catch (UserException ex) {
80 logger.error("pt_PT", ex);
81 throw new AuthenticatorException(ex.getMessage(), AuthenticatorException.SYSTEM_ERROR, ex);
82 } catch (AuthenticatorException ex) {
83 logger.error("pt_PT", ex);
84 throw new AuthenticatorException(ex.getMessage(), AuthenticatorException.SYSTEM_ERROR, ex);
85 }
86 }
87
88
89
90
91 private User updateOrCreateUser(String login, UserService userService) throws RemoteException, UserException,
92 AuthenticatorException {
93 User userModel;
94
95
96
97 userModel = userService.getUserByLogin(login);
98 if (null == userModel) {
99 userModel = createUser(login, userService);
100 } else {
101
102
103 if (needsUpdate(userModel, getExternalUserInfo(login))) {
104
105
106
107 userModel = userService.getUserByLogin(login);
108 userModel = updateUser(userModel, getExternalUserInfo(login));
109 userService.updateUser(userModel);
110 }
111 }
112 return userModel;
113 }
114
115
116
117
118 private User updateUser(User oldUserModel, User newUserModel) {
119 oldUserModel.setEmail(newUserModel.getEmail());
120 oldUserModel.setFirstName(newUserModel.getFirstName());
121 oldUserModel.setLastName(newUserModel.getLastName());
122 oldUserModel.setLastModifiedDate(new Date());
123 oldUserModel.setSuperUser(newUserModel.isSuperUser());
124 return (oldUserModel);
125 }
126
127
128
129
130 private User createUser(String login, UserService userService) throws RemoteException, UserException,
131 AuthenticatorException {
132
133
134 User userModel = getExternalUserInfo(login);
135 userModel.setRegistrationType(UserUtilities.REGISTRATION_TYPE_ADMIN);
136 userModel.setStatus(UserUtilities.STATUS_ACTIVE);
137 userModel = userService.createUser(userModel);
138
139
140
141 if (!userModel.isSuperUser()) {
142 setDefaultPermissions(userModel, userService);
143 }
144
145 return userModel;
146 }
147
148
149
150
151
152
153
154 private void setDefaultPermissions(User userModel, UserService userService) throws RemoteException,
155 AuthenticatorException, UserException {
156
157 User templateUser = userService.getUserByLogin(TEMPLATE_USER);
158 if (templateUser == null) {
159 String errorMessage = "TemplateUser not found. Create a user called template user, new permissions are copied from him to new users";
160 logger.error(errorMessage);
161 throw new AuthenticatorException(errorMessage, AuthenticatorException.CUSTOM_ERROR);
162 }
163
164 userService.setUserPermissions(userModel.getId(), userService.getPermissionsByUserId(templateUser.getId()));
165
166 UserPreferences preferences = templateUser.getPreferences();
167 preferences.setUser(userModel);
168 userService.updateUserPreferences(preferences);
169 }
170
171
172
173
174
175
176
177
178
179 private boolean needsUpdate(User localUser, User remoteUser) {
180 if (!(localUser.getEmail().equals(remoteUser.getEmail())))
181 return true;
182 if (!(localUser.getFirstName().equals(remoteUser.getFirstName())))
183 return true;
184 if (!(localUser.getLastName().equals(remoteUser.getLastName())))
185 return true;
186 if (localUser.isSuperUser() != remoteUser.isSuperUser())
187 return true;
188 return (false);
189 }
190
191 protected abstract User getExternalUserInfo(String login) throws AuthenticatorException;
192
193
194
195
196
197
198
199 public boolean allowProfileUpdates(User user, Object authentication, int authType, int reqSource)
200 throws AuthenticatorException {
201 return true;
202 }
203
204
205
206
207
208
209
210 public boolean allowPasswordUpdates(User user, Object authentication, int authType, int reqSource)
211 throws AuthenticatorException {
212 return false;
213 }
214
215 }