1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication;
35
36 import fr.paris.lutece.plugins.mylutece.authentication.ExternalAuthentication;
37 import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.IdxWSSODatabaseHome;
38 import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.service.WssoDatabasePlugin;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.plugin.PluginService;
41 import fr.paris.lutece.portal.service.security.LuteceUser;
42 import fr.paris.lutece.portal.service.util.AppPropertiesService;
43
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.List;
47
48 import javax.security.auth.login.LoginException;
49 import javax.servlet.http.Cookie;
50 import javax.servlet.http.HttpServletRequest;
51
52
53
54
55
56
57
58 public class IdxWSSODatabaseAuthentication extends ExternalAuthentication
59 {
60 private static final String PROPERTY_AUTH_SERVICE_NAME = "mylutece-wssodatabase.service.name";
61 private static final String PROPERTY_COOKIE_AUTHENTIFICATION = "mylutece-wssodatabase.cookie.authenticationMode";
62 private static final String PROPERTY_COOKIE_WSSOGUID = "mylutece-wssodatabase.cookie.wssoguid";
63 private static final String PLUGIN_NAME = "mylutece-wssodatabase";
64
65
66
67
68 public IdxWSSODatabaseAuthentication( )
69 {
70 }
71
72
73
74
75
76 public String getAuthServiceName( )
77 {
78 return AppPropertiesService.getProperty( PROPERTY_AUTH_SERVICE_NAME );
79 }
80
81
82
83
84
85
86 public String getAuthType( HttpServletRequest request )
87 {
88 Cookie[] cookies = request.getCookies( );
89 String strAuthType = request.getAuthType( );
90
91 for ( int i = 0; i < cookies.length; i++ )
92 {
93 Cookie cookie = cookies[i];
94
95 if ( cookie.getName( ).equals( PROPERTY_COOKIE_AUTHENTIFICATION ) )
96 {
97 strAuthType = cookie.getValue( );
98 }
99 }
100
101 return strAuthType;
102 }
103
104
105
106
107
108
109
110
111
112
113 public LuteceUser login( String strUserName, String strUserPassword, HttpServletRequest request )
114 throws LoginException
115 {
116
117 LuteceUser luteceUser = getHttpAuthenticatedUser( request );
118
119 return luteceUser;
120 }
121
122
123
124
125
126 public void logout( LuteceUser user )
127 {
128 }
129
130
131
132
133
134
135 public LuteceUser getAnonymousUser( )
136 {
137
138
139
140
141 throw new java.lang.UnsupportedOperationException( "The method getAnonymousUser() is not implemented yet." );
142 }
143
144
145
146
147
148
149
150
151
152 public boolean isUserInRole( LuteceUser user, HttpServletRequest request, String strRole )
153 {
154 if ( ( user == null ) || ( strRole == null ) )
155 {
156 return false;
157 }
158
159 String[] roles = user.getRoles( );
160
161 if ( roles != null )
162 {
163 for ( int i = 0; i < roles.length; i++ )
164 {
165 if ( strRole.equals( roles[i] ) )
166 {
167 return true;
168 }
169 }
170 }
171
172 return false;
173 }
174
175
176
177
178
179
180
181 public LuteceUser getHttpAuthenticatedUser( HttpServletRequest request )
182 {
183 if ( request != null )
184 {
185 Cookie[] cookies = request.getCookies( );
186 IdxWSSODatabaseUser user = null;
187 String strUserID = null;
188
189 if ( cookies != null )
190 {
191 for ( int i = 0; i < cookies.length; i++ )
192 {
193 Cookie cookie = cookies[i];
194
195 if ( cookie.getName( ).equals( AppPropertiesService.getProperty( PROPERTY_COOKIE_WSSOGUID ) ) )
196 {
197 strUserID = cookie.getValue( );
198 }
199 }
200 }
201
202 if ( strUserID != null )
203 {
204
205 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
206 user = IdxWSSODatabaseHome.findUserByGuid( strUserID, plugin, this );
207
208 if ( user != null )
209 {
210 IdxWSSODatabaseHome.updateDateLastLogin( strUserID, new java.util.Date( ), plugin );
211 List<String> arrayRoles = IdxWSSODatabaseHome.findUserRolesFromGuid( strUserID, plugin, this );
212
213 if ( !arrayRoles.isEmpty( ) )
214 {
215 user.setRoles( arrayRoles );
216 }
217 }
218 }
219
220 return user;
221 }
222 return null;
223 }
224
225
226
227
228
229
230 public boolean isUsersListAvailable( )
231 {
232 return true;
233 }
234
235
236
237
238
239
240
241 public Collection<LuteceUser> getUsers( )
242 {
243 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
244
245 Collection<IdxWSSODatabaseUser> usersList = IdxWSSODatabaseHome.findUsersList( plugin, this );
246 Collection<LuteceUser> luteceUsers = new ArrayList<LuteceUser>( );
247
248 for ( IdxWSSODatabaseUser user : usersList )
249 {
250 luteceUsers.add( user );
251 }
252
253 return luteceUsers;
254 }
255
256
257
258
259
260
261
262 public LuteceUser getUser( String userLogin )
263 {
264 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
265
266
267 IdxWSSODatabaseUser user = IdxWSSODatabaseHome.findUserByGuid( userLogin, plugin, this );
268
269 return user;
270 }
271
272
273
274
275
276
277
278 public String[] getRolesByUser( LuteceUser user )
279 {
280 return user.getRoles( );
281 }
282
283
284
285
286
287 public String getIconUrl( )
288 {
289 return null;
290 }
291
292
293
294
295
296 public String getName( )
297 {
298 return WssoDatabasePlugin.PLUGIN_NAME;
299 }
300
301
302
303
304
305 public String getPluginName( )
306 {
307 return WssoDatabasePlugin.PLUGIN_NAME;
308 }
309
310
311
312
313
314 public boolean isMultiAuthenticationSupported( )
315 {
316 return false;
317 }
318
319
320
321
322
323 @Override
324 public void updateDateLastLogin( LuteceUser user, HttpServletRequest request )
325 {
326 Plugin plugin = PluginService.getPlugin( PLUGIN_NAME );
327 IdxWSSODatabaseHome.updateDateLastLogin( user.getName( ), new java.util.Date( ), plugin );
328 }
329 }