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_DISCONNECT, CONSTANT_ACTION_LOGOUT_ADMINUSER, user, null, CONSTANT_BO );
234
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248 private <T extends AdminUser> T bindUser(T user) throws AccessDeniedException, UserNotSignedException
249 {
250 if ( user == null )
251 {
252 throw new UserNotSignedException( );
253 }
254
255
256 T bindUser = AdminUserHome.findUserByLogin( user.getAccessCode( ),user );
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 user;
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291 public <T extends AdminUser> void registerUser( HttpServletRequest request, T user ) throws AccessDeniedException, UserNotSignedException
292 {
293 HttpSession session = request.getSession( true );
294 session.setAttribute( ATTRIBUTE_ADMIN_USER, bindUser( user ) );
295 }
296
297
298
299
300
301
302
303
304
305
306 public void unregisterUser( HttpServletRequest request )
307 {
308 HttpSession session = request.getSession( true );
309 session.removeAttribute( ATTRIBUTE_ADMIN_USER );
310 }
311
312
313
314
315
316
317
318
319 public AdminUser getRegisteredUser( HttpServletRequest request )
320 {
321 HttpSession session = request.getSession( );
322
323 if ( session != null )
324 {
325 return (AdminUser) session.getAttribute( ATTRIBUTE_ADMIN_USER );
326 }
327
328 return null;
329 }
330
331
332
333
334
335
336 public boolean isExternalAuthentication( )
337 {
338 return _authentication.isExternalAuthentication( );
339 }
340
341
342
343
344
345
346 public String getLoginPageUrl( )
347 {
348 return _authentication.getLoginPageUrl( );
349 }
350
351
352
353
354
355
356 public String getChangePasswordPageUrl( )
357 {
358 return _authentication.getChangePasswordPageUrl( );
359 }
360
361
362
363
364
365
366 public String getDoLoginUrl( )
367 {
368 return _authentication.getDoLoginUrl( );
369 }
370
371
372
373
374
375
376 public String getDoLogoutUrl( )
377 {
378 return _authentication.getDoLogoutUrl( );
379 }
380
381
382
383
384
385
386 public String getNewAccountPageUrl( )
387 {
388 return _authentication.getNewAccountPageUrl( );
389 }
390
391
392
393
394
395
396 public String getViewAccountPageUrl( )
397 {
398 return _authentication.getViewAccountPageUrl( );
399 }
400
401
402
403
404
405
406 public String getLostPasswordPageUrl( )
407 {
408 return _authentication.getLostPasswordPageUrl( );
409 }
410
411
412
413
414
415
416 public String getLostLoginPageUrl( )
417 {
418 return _authentication.getLostLoginPageUrl( );
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432 public Collection<AdminUser> getUserListFromModule( String strLastName, String strFirstName, String strEmail )
433 {
434 return _authentication.getUserList( strLastName, strFirstName, strEmail );
435 }
436
437
438
439
440
441
442
443 public AdminUser getUserPublicDataFromModule( String strAccessCode )
444 {
445 return _authentication.getUserPublicData( strAccessCode );
446 }
447
448
449
450
451
452
453
454 public void setLoginNextUrl( HttpServletRequest request )
455 {
456 String strNextUrl = request.getRequestURI( );
457 UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( strNextUrl );
458 Enumeration enumParams = request.getParameterNames( );
459
460 while ( enumParams.hasMoreElements( ) )
461 {
462 String strParamName = (String) enumParams.nextElement( );
463 url.addParameter( strParamName, request.getParameter( strParamName ) );
464 }
465
466 HttpSession session = request.getSession( true );
467 session.setAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL, url.getUrl( ) );
468 }
469
470
471
472
473
474
475
476
477 public String getLoginNextUrl( HttpServletRequest request )
478 {
479 String strNextUrl = StringUtils.EMPTY;
480 HttpSession session = request.getSession( false );
481
482 if ( session != null )
483 {
484 strNextUrl = (String) session.getAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL );
485 session.removeAttribute( ATTRIBUTE_ADMIN_LOGIN_NEXT_URL );
486 }
487
488 return strNextUrl;
489 }
490
491
492
493
494
495 public String getAuthServiceName()
496 {
497 return _authentication.getAuthServiceName();
498 }
499
500 }