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.portal.service.admin;
35
36 import fr.paris.lutece.portal.service.security.AccessLogService;
37 import java.util.Collection;
38 import java.util.Enumeration;
39 import java.util.stream.Collectors;
40
41 import javax.security.auth.login.LoginException;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpSession;
44
45 import org.apache.commons.lang3.StringUtils;
46
47 import fr.paris.lutece.portal.business.user.AdminUser;
48 import fr.paris.lutece.portal.business.user.AdminUserHome;
49 import fr.paris.lutece.portal.business.user.authentication.AdminAuthentication;
50 import fr.paris.lutece.portal.business.user.authentication.LuteceDefaultAdminAuthentication;
51 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
52 import fr.paris.lutece.portal.service.security.AccessLoggerConstants;
53 import fr.paris.lutece.portal.service.security.UserNotSignedException;
54 import fr.paris.lutece.portal.service.spring.SpringContextService;
55 import fr.paris.lutece.portal.service.util.AppLogService;
56 import fr.paris.lutece.util.url.UrlItem;
57
58
59
60
61 public final class AdminAuthenticationService
62 {
63
64
65
66 private static final String ATTRIBUTE_ADMIN_USER = "lutece_admin_user";
67 private static final String ATTRIBUTE_ADMIN_LOGIN_NEXT_URL = "luteceAdminLoginNextUrl";
68
69 private static final String BEAN_ADMIN_AUTHENTICATION_MODULE = "adminAuthenticationModule";
70
71 private static final String CONSTANT_ACTION_LOGIN_ADMINUSER = "user.loginAdminUser";
72 private static final String CONSTANT_ACTION_LOGOUT_ADMINUSER = "user.logoutAdminUser";
73 private static final String CONSTANT_BO = "BO";
74
75 private static AdminAuthenticationServiceenticationService.html#AdminAuthenticationService">AdminAuthenticationService _singleton = new AdminAuthenticationService( );
76 private static AdminAuthentication _authentication;
77 private static boolean _bUseDefaultModule;
78
79
80
81
82 private AdminAuthenticationService( )
83 {
84 }
85
86
87
88
89 public static synchronized void init( )
90 {
91 _authentication = SpringContextService.getBean( BEAN_ADMIN_AUTHENTICATION_MODULE );
92 AppLogService.info( "Authentication module loaded : {}", _authentication.getAuthServiceName( ) );
93
94 if ( _authentication.getClass( ).equals( LuteceDefaultAdminAuthentication.class ) )
95 {
96 _bUseDefaultModule = true;
97 }
98 }
99
100
101
102
103
104
105 public static AdminAuthenticationService getInstance( )
106 {
107 return _singleton;
108 }
109
110
111
112
113
114
115 public boolean isDefaultModuleUsed( )
116 {
117 return _bUseDefaultModule;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 public AdminUser getRemoteUser( HttpServletRequest request ) throws UserNotSignedException, AccessDeniedException
132 {
133 AdminUser user = getRegisteredUser( request );
134
135 if ( _authentication.isExternalAuthentication( ) )
136 {
137 if ( user == null )
138 {
139
140 user = _authentication.getHttpAuthenticatedUser( request );
141 registerUser( request, user );
142 AdminUserService.updateDateLastLogin( user.getUserId( ) );
143
144
145 throw new UserNotSignedException( );
146 }
147 else
148 {
149
150 AdminUser newUser = _authentication.getHttpAuthenticatedUser( request );
151
152 if ( newUser == null )
153 {
154 throw new AccessDeniedException( "User not found while retrieving from external authentication" );
155 }
156 else
157 if ( !newUser.getAccessCode( ).equals( user.getAccessCode( ) ) )
158 {
159 unregisterUser( request );
160 registerUser( request, newUser );
161 AdminUserService.updateDateLastLogin( user.getUserId( ) );
162
163
164 throw new UserNotSignedException( );
165 }
166 }
167 }
168 else
169
170 {
171 if ( user == null )
172 {
173
174 throw new UserNotSignedException( );
175 }
176 }
177
178 return user;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public void loginUser( HttpServletRequest request, final String strAccessCode, final String strPassword ) throws LoginException
194 {
195 AdminUser user = _authentication.login( strAccessCode, strPassword, request );
196
197 AccessLogService.getInstance( ).info( AccessLoggerConstants.EVENT_TYPE_CONNECT, CONSTANT_ACTION_LOGIN_ADMINUSER, user, null, CONSTANT_BO );
198
199 try
200 {
201 registerUser( request, user );
202 }
203 catch( UserNotSignedException | AccessDeniedException e )
204 {
205 throw new LoginException( );
206 }
207
208 AdminUserService.updateDateLastLogin( user.getUserId( ) );
209 }
210
211
212
213
214
215
216
217 public void logoutUser( HttpServletRequest request )
218 {
219 AdminUser user;
220
221 try
222 {
223 user = getRemoteUser( request );
224 }
225 catch( AccessDeniedException | UserNotSignedException e )
226 {
227 return;
228 }
229
230 _authentication.logout( user );
231 unregisterUser( request );
232
233 AccessLogService.getInstance( ).info( AccessLoggerConstants.EVENT_TYPE_CONNECT, CONSTANT_ACTION_LOGOUT_ADMINUSER, user, null, CONSTANT_BO );
234
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 private AdminUser../../../../../fr/paris/lutece/portal/business/user/AdminUser.html#AdminUser">AdminUser bindUser( AdminUser user ) throws AccessDeniedException, UserNotSignedException
249 {
250 if ( user == null )
251 {
252 throw new UserNotSignedException( );
253 }
254
255
256 AdminUser bindUser = AdminUserHome.findUserByLogin( user.getAccessCode( ) );
257
258
259 if ( ( bindUser == null ) || ( !bindUser.isStatusActive( ) ) )
260 {
261 throw new AccessDeniedException( "User " + bindUser + " is null or not active" );
262 }
263
264
265 bindUser.setRights( AdminUserHome.getRightsListForUser( bindUser.getUserId( ) ) );
266
267
268 bindUser.setRoles( AdminUserHome.getRolesListForUser( bindUser.getUserId( ) ) );
269
270
271 bindUser.setUserWorkgroups( AdminWorkgroupHome.getUserWorkgroups( bindUser ).stream( ).map( x -> x.getCode( ) ).collect( Collectors.toList( ) ) );
272
273 return bindUser;
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288 public void registerUser( HttpServletRequest request, AdminUser user ) throws AccessDeniedException, UserNotSignedException
289 {
290 HttpSession session = request.getSession( true );
291 session.setAttribute( ATTRIBUTE_ADMIN_USER, bindUser( user ) );
292 }
293
294
295
296
297
298
299
300 public void unregisterUser( HttpServletRequest request )
301 {
302 HttpSession session = request.getSession( true );
303 session.removeAttribute( ATTRIBUTE_ADMIN_USER );
304 }
305
306
307
308
309
310
311
312
313 public AdminUser getRegisteredUser( HttpServletRequest request )
314 {
315 HttpSession session = request.getSession( );
316
317 if ( session != null )
318 {
319 return (AdminUser) session.getAttribute( ATTRIBUTE_ADMIN_USER );
320 }
321
322 return null;
323 }
324
325
326
327
328
329
330 public boolean isExternalAuthentication( )
331 {
332 return _authentication.isExternalAuthentication( );
333 }
334
335
336
337
338
339
340 public String getLoginPageUrl( )
341 {
342 return _authentication.getLoginPageUrl( );
343 }
344
345
346
347
348
349
350 public String getChangePasswordPageUrl( )
351 {
352 return _authentication.getChangePasswordPageUrl( );
353 }
354
355
356
357
358
359
360 public String getDoLoginUrl( )
361 {
362 return _authentication.getDoLoginUrl( );
363 }
364
365
366
367
368
369
370 public String getDoLogoutUrl( )
371 {
372 return _authentication.getDoLogoutUrl( );
373 }
374
375
376
377
378
379
380 public String getNewAccountPageUrl( )
381 {
382 return _authentication.getNewAccountPageUrl( );
383 }
384
385
386
387
388
389
390 public String getViewAccountPageUrl( )
391 {
392 return _authentication.getViewAccountPageUrl( );
393 }
394
395
396
397
398
399
400 public String getLostPasswordPageUrl( )
401 {
402 return _authentication.getLostPasswordPageUrl( );
403 }
404
405
406
407
408
409
410 public String getLostLoginPageUrl( )
411 {
412 return _authentication.getLostLoginPageUrl( );
413 }
414
415
416
417
418
419
420
421
422
423
424
425
426 public Collection<AdminUser> getUserListFromModule( String strLastName, String strFirstName, String strEmail )
427 {
428 return _authentication.getUserList( strLastName, strFirstName, strEmail );
429 }
430
431
432
433
434
435
436
437 public AdminUser getUserPublicDataFromModule( String strAccessCode )
438 {
439 return _authentication.getUserPublicData( strAccessCode );
440 }
441
442
443
444
445
446
447
448 public void setLoginNextUrl( HttpServletRequest request )
449 {
450 String strNextUrl = request.getRequestURI( );
451 UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strNextUrl );
452 Enumeration enumParams = request.getParameterNames( );
453
454 while ( enumParams.hasMoreElements( ) )
455 {
456 String strParamName = (String) enumParams.nextElement( );
457 url.addParameter( strParamName, request.getParameter( strParamName ) );
458 }
459
460 HttpSession session = request.getSession( true );
461 session.setAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL, url.getUrl( ) );
462 }
463
464
465
466
467
468
469
470
471 public String getLoginNextUrl( HttpServletRequest request )
472 {
473 String strNextUrl = StringUtils.EMPTY;
474 HttpSession session = request.getSession( false );
475
476 if ( session != null )
477 {
478 strNextUrl = (String) session.getAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL );
479 session.removeAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL );
480 }
481
482 return strNextUrl;
483 }
484 }