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.security;
35
36 import fr.paris.lutece.portal.service.datastore.DatastoreService;
37 import fr.paris.lutece.portal.service.init.LuteceInitException;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPropertiesService;
40 import fr.paris.lutece.util.url.UrlItem;
41
42 import java.security.Principal;
43
44 import java.util.Collection;
45 import java.util.Enumeration;
46
47 import javax.security.auth.login.LoginException;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpSession;
51
52
53
54
55
56
57 public final class SecurityService
58 {
59
60
61
62
63 private static final String ATTRIBUTE_LUTECE_USER = "lutece_user";
64 private static final String PROPERTY_AUTHENTICATION_CLASS = "mylutece.authentication.class";
65 private static final String PROPERTY_AUTHENTICATION_ENABLE = "mylutece.authentication.enable";
66 private static final String PROPERTY_PORTAL_AUTHENTICATION_REQUIRED = "mylutece.portal.authentication.required";
67 private static final String URL_INTERROGATIVE = "?";
68 private static final String URL_AMPERSAND = "&";
69 private static final String URL_EQUAL = "=";
70 private static SecurityService _singleton = new SecurityService( );
71 private static LuteceAuthentication _authenticationService;
72 private static boolean _bEnable;
73
74
75
76
77 private SecurityService( )
78 {
79 }
80
81
82
83
84
85 public static synchronized void init( ) throws LuteceInitException
86 {
87 _bEnable = false;
88
89 String strEnable = AppPropertiesService.getProperty( PROPERTY_AUTHENTICATION_ENABLE, "false" );
90
91 if ( strEnable.equalsIgnoreCase( "true" ) )
92 {
93 _authenticationService = getPortalAuthentication( );
94
95 if ( _authenticationService != null )
96 {
97 _bEnable = true;
98 }
99 } else
100 {
101
102 _authenticationService = null;
103 }
104 }
105
106
107
108
109
110 public static SecurityService getInstance( )
111 {
112 return _singleton;
113 }
114
115
116
117
118
119 public static boolean isAuthenticationEnable( )
120 {
121 return _bEnable;
122 }
123
124
125
126
127
128
129
130 public LuteceUser getRemoteUser( HttpServletRequest request )
131 throws UserNotSignedException
132 {
133 LuteceUser user = getRegisteredUser( request );
134
135 if ( user == null )
136 {
137
138 if ( _authenticationService.isExternalAuthentication( ) ||
139 _authenticationService.isMultiAuthenticationSupported( ) )
140 {
141 user = _authenticationService.getHttpAuthenticatedUser( request );
142
143 if ( ( user == null ) && isPortalAuthenticationRequired( ) )
144 {
145 throw new UserNotSignedException( );
146 }
147
148 registerUser( request, user );
149 }
150 else
151 {
152 throw new UserNotSignedException( );
153 }
154 }
155
156 return user;
157 }
158
159
160
161
162
163
164
165 public Principal getUserPrincipal( HttpServletRequest request )
166 throws UserNotSignedException
167 {
168 return getRemoteUser( request );
169 }
170
171
172
173
174
175
176
177 public boolean isUserInRole( HttpServletRequest request, String strRole )
178 {
179 LuteceUser user;
180
181 try
182 {
183 user = getRemoteUser( request );
184 }
185 catch ( UserNotSignedException e )
186 {
187 return false;
188 }
189
190 return _authenticationService.isUserInRole( user, request, strRole );
191 }
192
193
194
195
196
197
198
199 public String[] getRolesByUser( LuteceUser user )
200 {
201 return _authenticationService.getRolesByUser( user );
202 }
203
204
205
206
207
208
209
210
211
212 public void loginUser( HttpServletRequest request, final String strUserName, final String strPassword )
213 throws LoginException, LoginRedirectException
214 {
215 LuteceUser user = _authenticationService.login( strUserName, strPassword, request );
216 _authenticationService.updateDateLastLogin( user, request );
217
218 if ( _authenticationService.findResetPassword( request, strUserName ) )
219 {
220 String redirect = _authenticationService.getResetPasswordPageUrl( request );
221 registerUser( request, user );
222 throw new LoginRedirectException( redirect );
223 }
224
225 registerUser( request, user );
226 }
227
228
229
230
231
232 public void logoutUser( HttpServletRequest request )
233 {
234 LuteceUser user;
235
236 try
237 {
238 user = getRemoteUser( request );
239 }
240 catch ( UserNotSignedException e )
241 {
242 return;
243 }
244
245 _authenticationService.logout( user );
246 unregisterUser( request );
247 }
248
249
250
251
252
253
254
255 private static LuteceAuthentication getPortalAuthentication( )
256 throws LuteceInitException
257 {
258 String strAuthenticationClass = AppPropertiesService.getProperty( PROPERTY_AUTHENTICATION_CLASS );
259 LuteceAuthentication authentication = null;
260
261 if ( ( strAuthenticationClass != null ) && !strAuthenticationClass.equals( "" ) )
262 {
263 try
264 {
265 authentication = (LuteceAuthentication) Class.forName( strAuthenticationClass ).newInstance( );
266 AppLogService.info( "Authentication service loaded : " + authentication.getAuthServiceName( ) );
267 }
268 catch ( InstantiationException e )
269 {
270 throw new LuteceInitException( "Error instantiating Authentication Class", e );
271 }
272 catch ( IllegalAccessException e )
273 {
274 throw new LuteceInitException( "Error instantiating Authentication Class", e );
275 }
276 catch ( ClassNotFoundException e )
277 {
278 throw new LuteceInitException( "Error instantiating Authentication Class", e );
279 }
280 }
281
282 return authentication;
283 }
284
285
286
287
288
289
290 public void registerUser( HttpServletRequest request, LuteceUser user )
291 {
292 HttpSession session = request.getSession( true );
293 session.setAttribute( ATTRIBUTE_LUTECE_USER, user );
294 }
295
296
297
298
299
300 public void unregisterUser( HttpServletRequest request )
301 {
302 HttpSession session = request.getSession( true );
303 session.removeAttribute( ATTRIBUTE_LUTECE_USER );
304 }
305
306
307
308
309
310
311 public LuteceUser getRegisteredUser( HttpServletRequest request )
312 {
313 HttpSession session = ( request != null ) ? request.getSession( false ) : null;
314
315 if ( session != null )
316 {
317 return (LuteceUser) session.getAttribute( ATTRIBUTE_LUTECE_USER );
318 }
319
320 return null;
321 }
322
323
324
325
326
327
328 public boolean isExternalAuthentication( )
329 {
330 return _authenticationService.isExternalAuthentication( );
331 }
332
333
334
335
336
337 public String getLoginPageUrl( )
338 {
339 return _authenticationService.getLoginPageUrl( );
340 }
341
342
343
344
345
346 public String getDoLoginUrl( )
347 {
348 return _authenticationService.getDoLoginUrl( );
349 }
350
351
352
353
354
355 public String getDoLogoutUrl( )
356 {
357 return _authenticationService.getDoLogoutUrl( );
358 }
359
360
361
362
363
364 public String getNewAccountPageUrl( )
365 {
366 return _authenticationService.getNewAccountPageUrl( );
367 }
368
369
370
371
372
373 public String getViewAccountPageUrl( )
374 {
375 return _authenticationService.getViewAccountPageUrl( );
376 }
377
378
379
380
381
382 public String getLostPasswordPageUrl( )
383 {
384 return _authenticationService.getLostPasswordPageUrl( );
385 }
386
387
388
389
390
391
392
393 public String getAccessDeniedTemplate( )
394 {
395 return _authenticationService.getAccessDeniedTemplate( );
396 }
397
398
399
400
401
402 public String getAccessControledTemplate( )
403 {
404 return _authenticationService.getAccessControledTemplate( );
405 }
406
407
408
409
410
411
412 public boolean isPortalAuthenticationRequired( )
413 {
414 String strAuthenticationRequired = DatastoreService.getDataValue( PROPERTY_PORTAL_AUTHENTICATION_REQUIRED,
415 "false" );
416
417 return strAuthenticationRequired.equals( "true" );
418 }
419
420
421
422
423
424
425
426
427
428
429
430
431
432 public LuteceUser remoteLoginUser( final HttpServletRequest request, final String strUserName,
433 final String strPassword ) throws LoginException, LoginRedirectException
434 {
435 LuteceUser user = _authenticationService.login( strUserName, strPassword, request );
436
437 return user;
438 }
439
440
441
442
443
444
445 public boolean isLoginUrl( HttpServletRequest request )
446 {
447 if ( ( getLoginPageUrl( ) == null ) || ( request == null ) )
448 {
449 return false;
450 }
451
452 String strRequestUrl = request.getRequestURI( );
453 UrlItem url = new UrlItem( strRequestUrl );
454
455 for ( String strParamValueLoginPageUrl : getLoginPageUrl( )
456 .substring( getLoginPageUrl( ).indexOf( URL_INTERROGATIVE ) + 1 )
457 .split( URL_AMPERSAND ) )
458 {
459 String[] arrayParamValueLoginPageUrl = strParamValueLoginPageUrl.split( URL_EQUAL );
460 Enumeration<String> enumParams = request.getParameterNames( );
461
462 while ( enumParams.hasMoreElements( ) )
463 {
464 String strRequestParameter = (String) enumParams.nextElement( );
465
466 if ( arrayParamValueLoginPageUrl[0].equals( strRequestParameter ) &&
467 arrayParamValueLoginPageUrl[1].equals( request.getParameter( strRequestParameter ) ) )
468 {
469 url.addParameter( strRequestParameter, request.getParameter( strRequestParameter ) );
470 }
471 }
472 }
473
474 if ( url.getUrl( ).endsWith( getLoginPageUrl( ) ) && !getLoginPageUrl( ).equals( "" ) )
475 {
476 return true;
477 }
478
479 return false;
480 }
481
482
483
484
485
486
487 boolean isUsersListAvailable( )
488 {
489 return _authenticationService.isUsersListAvailable( );
490 }
491
492
493
494
495
496
497
498 public Collection<LuteceUser> getUsers( )
499 {
500 return _authenticationService.getUsers( );
501 }
502
503
504
505
506
507
508
509 public LuteceUser getUser( String strUserLogin )
510 {
511 return _authenticationService.getUser( strUserLogin );
512 }
513
514
515
516
517
518
519 public boolean isMultiAuthenticationSupported( )
520 {
521 return _authenticationService.isMultiAuthenticationSupported( );
522 }
523
524
525
526
527
528 public LuteceAuthentication getAuthenticationService( )
529 {
530 return _authenticationService;
531 }
532 }