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.web.system;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41
42 import org.apache.commons.lang3.StringUtils;
43 import org.apache.commons.text.StringEscapeUtils;
44
45 import fr.paris.lutece.portal.business.securityheader.SecurityHeader;
46 import fr.paris.lutece.portal.business.securityheader.SecurityHeaderHome;
47 import fr.paris.lutece.portal.business.securityheader.SecurityHeaderType;
48 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
49 import fr.paris.lutece.portal.service.message.AdminMessage;
50 import fr.paris.lutece.portal.service.message.AdminMessageService;
51 import fr.paris.lutece.portal.service.security.SecurityTokenService;
52 import fr.paris.lutece.portal.service.securityheader.SecurityHeaderService;
53 import fr.paris.lutece.portal.service.spring.SpringContextService;
54 import fr.paris.lutece.portal.service.template.AppTemplateService;
55 import fr.paris.lutece.portal.service.util.AppLogService;
56 import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
57 import fr.paris.lutece.util.ErrorMessage;
58 import fr.paris.lutece.util.ReferenceItem;
59 import fr.paris.lutece.util.ReferenceList;
60 import fr.paris.lutece.util.html.HtmlTemplate;
61 import fr.paris.lutece.util.http.SecurityUtil;
62
63
64
65
66 public class SecurityHeaderJspBean extends MVCAdminJspBean
67 {
68
69 public static final String RIGHT_SECURITY_HEADER_MANAGEMENT = "CORE_SECURITY_HEADER_MANAGEMENT";
70
71
72 private static final String TEMPLATE_CREATE_SECURITYHEADER = "admin/system/create_securityheader.html";
73 private static final String TEMPLATE_MODIFY_SECURITYHEADER = "admin/system/modify_securityheader.html";
74
75
76 private static final String MARK_SECURITY_HEADERS_LIST = "security_headers_list";
77 private static final String MARK_SECURITY_HEADER = "securityheader";
78 private static final String MARK_TYPES_LIST = "types_list";
79 private static final String MARK_TYPE_SELECTED = "selected_type";
80 private static final String MARK_PAGE_CATEGORY_LIST = "page_category_list";
81 private static final String MARK_PAGE_CATEGORY_SELECTED = "selected_pageCategory";
82 private static final String MARK_ERRORS = "errors";
83
84
85 private static final String PROPERTY_CREATE_SECURITYHEADER_PAGETITLE = "portal.securityheader.create_securityheader.pageTitle";
86 private static final String PROPERTY_MODIFY_SECURITYHEADER_PAGETITLE = "portal.securityheader.modify_securityheader.pageTitle";
87 private static final String MESSAGE_ACTIVE_HEADER_NOT_EDITABLE = "portal.securityheader.message.activeHeaderNotEditable";
88 private static final String MESSAGE_CONFIRM_REMOVE = "portal.securityheader.message.confirmRemoveSecurityHeader";
89 private static final String MESSAGE_HEADER_ALREADY_EXIST = "portal.securityheader.message.securityHeadersAlreadyexists";
90 private static final String MESSAGE_PAGE_CATEGORY_REQUIRED_WHEN_PAGE_IS_TYPE = "portal.securityheader.message.pageCategoryRequiredTypePage";
91 private static final String MESSAGE_TYPE_UNKNOWN = "portal.securityheader.message.typeUnknown";
92 private static final String MESSAGE_PAGE_CATEGORY_UNKNOWN = "portal.securityheader.message.pageCategoryUnknown";
93
94
95 private static final String VALIDATION_ATTRIBUTES_PREFIX = "portal.securityheader.model.entity.securityheader.attribute.";
96
97
98 private static final String TEMPLATE_MANAGE_SECURITY_HEADERS = "admin/system/manage_security_headers.html";
99
100
101 private static final String PARAMETER_SECURITY_HEADER_ID = "id_securityheader";
102 private static final String PARAMETER_NAME = "name";
103 private static final String PARAMETER_VALUE = "value";
104 private static final String PARAMETER_DESCRIPTION = "description";
105 private static final String PARAMETER_TYPE = "type";
106 private static final String PARAMETER_PAGE_CATEGORY = "pageCategory";
107 private static final String PARAMETER_ACTION = "action";
108
109
110 public static final String JSP_MANAGE_SECURITY_HEADERS = "ManageSecurityHeaders.jsp";
111 public static final String JSP_REMOVE_SECURITY_HEADERS = "jsp/admin/system/DoRemoveSecurityHeader.jsp";
112
113
114 private static final String ACTION_ENABLE = "ENABLE";
115 private static final String ACTION_DISABLE = "DISABLE";
116
117 private static final long serialVersionUID = 7010476999488231065L;
118
119
120
121
122
123
124
125
126 public String getManageSecurityHeaders( HttpServletRequest request )
127 {
128 HashMap<String, Object> model = createModelForHeadersList( request );
129 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_SECURITY_HEADERS, getLocale( ), model );
130
131 return getAdminPage( template.getHtml( ) );
132 }
133
134
135
136
137
138
139
140
141
142 private HashMap<String, Object> createModelForHeadersList( HttpServletRequest request )
143 {
144 HashMap<String, Object> model = new HashMap<>( );
145 model.put( MARK_SECURITY_HEADERS_LIST, getSecurityHeaderService( ).findAllSorted( getLocale( ) ) );
146 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MANAGE_SECURITY_HEADERS ) );
147
148 return model;
149 }
150
151
152
153
154
155
156
157
158 public String getCreateSecurityHeader( HttpServletRequest request )
159 {
160 setPageTitleProperty( PROPERTY_CREATE_SECURITYHEADER_PAGETITLE );
161 HashMap<String, Object> model = createModelForHeaderCreation( request );
162 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_SECURITYHEADER, getLocale( ), model );
163
164 return getAdminPage( template.getHtml( ) );
165 }
166
167
168
169
170
171
172
173
174
175 private HashMap<String, Object> createModelForHeaderCreation( HttpServletRequest request )
176 {
177 ReferenceList listTypes = getSecurityHeaderService( ).getTypeList( );
178 ReferenceList listPageCategories = getSecurityHeaderService( ).getPageCategoryList( );
179
180 HashMap<String, Object> model = new HashMap<>( );
181 model.put( MARK_TYPES_LIST, listTypes );
182 model.put( MARK_PAGE_CATEGORY_LIST, listPageCategories );
183 if ( !listTypes.isEmpty( ) )
184 {
185 model.put( MARK_TYPE_SELECTED, listTypes.get( 0 ).getCode( ) );
186 }
187
188 if ( !listPageCategories.isEmpty( ) )
189 {
190 model.put( MARK_PAGE_CATEGORY_SELECTED, listPageCategories.get( 0 ).getCode( ) );
191 }
192 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_SECURITYHEADER ) );
193
194 return model;
195 }
196
197
198
199
200
201
202
203
204
205
206 public String doCreateSecurityHeader( HttpServletRequest request ) throws AccessDeniedException
207 {
208 SecurityHeadertyheader/SecurityHeader.html#SecurityHeader">SecurityHeader securityHeader = new SecurityHeader( );
209 String strErrors = processCreationFormData( request, securityHeader );
210
211 if ( strErrors != null )
212 {
213 return AdminMessageService.getMessageUrl( request, strErrors, AdminMessage.TYPE_STOP );
214 }
215
216 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_SECURITYHEADER ) )
217 {
218 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
219 }
220
221 getSecurityHeaderService( ).create( securityHeader );
222
223 return JSP_MANAGE_SECURITY_HEADERS;
224 }
225
226
227
228
229
230
231
232
233
234
235 private String processCreationFormData( HttpServletRequest request, SecurityHeader securityHeader )
236 {
237 securityHeader.setName( request.getParameter( PARAMETER_NAME ) );
238 securityHeader.setValue( request.getParameter( PARAMETER_VALUE ) );
239 securityHeader.setDescription( request.getParameter( PARAMETER_DESCRIPTION ) );
240 securityHeader.setType( request.getParameter( PARAMETER_TYPE ) );
241 securityHeader.setPageCategory( getPageCategory( request.getParameter( PARAMETER_PAGE_CATEGORY ), securityHeader.getType( ) ) );
242
243 String strErrors = processCommonControls( securityHeader );
244 if( strErrors != null )
245 {
246 return strErrors;
247 }
248
249 if( !isSecurityHeaderToCreateUnique( securityHeader ) )
250 {
251 return MESSAGE_HEADER_ALREADY_EXIST;
252 }
253
254 return null;
255 }
256
257
258
259
260
261
262
263
264
265 private String processCommonControls( SecurityHeader securityHeader )
266 {
267
268 if( !validateBean( securityHeader, VALIDATION_ATTRIBUTES_PREFIX ) )
269 {
270 List<ErrorMessage> listErrors = ( List<ErrorMessage> ) getModel( ).get( MARK_ERRORS );
271 return listErrors.get( 0 ).getMessage( );
272 }
273
274 if( !isTypeBelongsToReferenceList ( securityHeader.getType( ) ) )
275 {
276 return MESSAGE_TYPE_UNKNOWN;
277 }
278
279 if( securityHeader.getPageCategory( ) != null && !isPageCategoryBelongsToReferenceList ( securityHeader.getPageCategory( ) ) )
280 {
281 return MESSAGE_PAGE_CATEGORY_UNKNOWN;
282 }
283
284 if( SecurityHeaderType.PAGE.getCode( ).equals( securityHeader.getType( ) ) && StringUtils.isEmpty( securityHeader.getPageCategory( ) ) )
285 {
286 return MESSAGE_PAGE_CATEGORY_REQUIRED_WHEN_PAGE_IS_TYPE;
287 }
288
289 return null;
290 }
291
292
293
294
295
296
297
298
299 private boolean isTypeBelongsToReferenceList( String strType )
300 {
301 for (ReferenceItem referenceType : getSecurityHeaderService( ).getTypeList( ) )
302 {
303 if ( strType.equals( referenceType.getCode() ) )
304 {
305 return true;
306 }
307 }
308
309 return false;
310 }
311
312
313
314
315
316
317
318
319 private boolean isPageCategoryBelongsToReferenceList( String strPageCategory )
320 {
321 for (ReferenceItem referencePageCategory : getSecurityHeaderService( ).getPageCategoryList( ) )
322 {
323 if ( strPageCategory.equals( referencePageCategory.getCode() ) )
324 {
325 return true;
326 }
327 }
328
329 return false;
330 }
331
332
333
334
335
336
337
338
339
340 private boolean isSecurityHeaderToCreateUnique( SecurityHeader securityHeaderToCreate )
341 {
342 return getSecurityHeaderService( ).find( securityHeaderToCreate.getName( ), securityHeaderToCreate.getType( ) , securityHeaderToCreate.getPageCategory( ) ).isEmpty( );
343 }
344
345
346
347
348
349
350
351
352 public String getModifySecurityHeader( HttpServletRequest request )
353 {
354 setPageTitleProperty( PROPERTY_MODIFY_SECURITYHEADER_PAGETITLE );
355
356 String strSecurityHeaderId = request.getParameter( PARAMETER_SECURITY_HEADER_ID );
357
358 if ( !StringUtils.isNumeric( strSecurityHeaderId ) )
359 {
360 AppLogService.error( " {} is not a valid security header id.", ( ) -> SecurityUtil.logForgingProtect( strSecurityHeaderId ) );
361
362 return getManageSecurityHeaders( request );
363 }
364
365 SecurityHeader securityHeader = getSecurityHeaderForModification( strSecurityHeaderId );
366 if ( securityHeader == null )
367 {
368 AppLogService.error( "{} is not a valid security header id.", ( ) -> SecurityUtil.logForgingProtect( strSecurityHeaderId ) );
369
370 return getManageSecurityHeaders( request );
371 }
372
373 HashMap<String, Object> model = createModelForHeaderModification( request, securityHeader );
374 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_SECURITYHEADER, getLocale( ), model );
375
376 return getAdminPage( template.getHtml( ) );
377 }
378
379
380
381
382
383
384
385
386
387 private SecurityHeader getSecurityHeaderForModification( String strSecurityHeaderId )
388 {
389 SecurityHeader securityHeader = SecurityHeaderHome.findByPrimaryKey( Integer.parseInt( strSecurityHeaderId ) );
390 if( securityHeader != null )
391 {
392 securityHeader.setValue( StringEscapeUtils.escapeHtml4( securityHeader.getValue( ) ) );
393 }
394
395 return securityHeader;
396 }
397
398
399
400
401
402
403
404
405 public String getMessageNotEditableSecurityHeader( HttpServletRequest request )
406 {
407 return AdminMessageService.getMessageUrl( request, MESSAGE_ACTIVE_HEADER_NOT_EDITABLE, AdminMessage.TYPE_INFO );
408 }
409
410
411
412
413
414
415
416
417
418 private HashMap<String, Object> createModelForHeaderModification(HttpServletRequest request, SecurityHeader securityHeader)
419 {
420 ReferenceList listTypes = getSecurityHeaderService().getTypeList( );
421 ReferenceList listPageCategories = getSecurityHeaderService().getPageCategoryList( );
422
423 HashMap<String, Object> model = new HashMap<>( );
424 model.put( MARK_TYPES_LIST, listTypes );
425 model.put( MARK_PAGE_CATEGORY_LIST, listPageCategories );
426 model.put( MARK_TYPE_SELECTED, securityHeader.getType() );
427 String selectedCategory = null;
428 if(securityHeader.getType().equals(SecurityHeaderType.PAGE.getCode()))
429 {
430 selectedCategory = securityHeader.getPageCategory();
431 }
432 else
433 {
434 selectedCategory = listPageCategories.get( 0 ).getCode( );
435 }
436 model.put( MARK_PAGE_CATEGORY_SELECTED, selectedCategory );
437 model.put( MARK_SECURITY_HEADER, securityHeader );
438 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_SECURITYHEADER ) );
439 return model;
440 }
441
442
443
444
445
446
447
448
449
450
451 public String doModifySecurityHeader( HttpServletRequest request ) throws AccessDeniedException
452 {
453 int nId = Integer.parseInt( request.getParameter( PARAMETER_SECURITY_HEADER_ID ) );
454 SecurityHeader securityHeader = SecurityHeaderHome.findByPrimaryKey( nId );
455
456 String strErrors = processModifyFormData( request, securityHeader );
457
458 if ( strErrors != null )
459 {
460 return AdminMessageService.getMessageUrl( request, strErrors, AdminMessage.TYPE_STOP );
461 }
462 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_SECURITYHEADER ) )
463 {
464 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
465 }
466
467 getSecurityHeaderService( ).update( securityHeader );
468
469 return getHomeUrl( request );
470 }
471
472
473
474
475
476
477
478
479
480
481 private String processModifyFormData( HttpServletRequest request, SecurityHeader securityHeader )
482 {
483 securityHeader.setName( request.getParameter( PARAMETER_NAME ) );
484 securityHeader.setValue( request.getParameter( PARAMETER_VALUE ) );
485 securityHeader.setDescription( request.getParameter( PARAMETER_DESCRIPTION ) );
486 securityHeader.setType( request.getParameter( PARAMETER_TYPE ) );
487 securityHeader.setPageCategory( getPageCategory( request.getParameter( PARAMETER_PAGE_CATEGORY ), securityHeader.getType( ) ) );
488
489 String strErrors = processCommonControls( securityHeader );
490 if( strErrors != null )
491 {
492 return strErrors;
493 }
494
495 if( !isSecurityHeaderToModifyUnique( securityHeader, securityHeader.getName( ), securityHeader.getType( ), securityHeader.getPageCategory( ) ) )
496 {
497 return MESSAGE_HEADER_ALREADY_EXIST;
498 }
499
500 return null;
501 }
502
503
504
505
506
507
508
509
510
511 private String getPageCategory( String strPageCategoryFromRequest, String strType )
512 {
513 if( SecurityHeaderType.PAGE.getCode( ).equals( strType ) )
514 {
515 return strPageCategoryFromRequest ;
516 }
517
518 return null;
519 }
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535 private boolean isSecurityHeaderToModifyUnique( SecurityHeader securityHeaderToModify, String strName, String strType, String strPageCategory )
536 {
537 for( SecurityHeader securityHeader : getSecurityHeaderService( ).find( strName, strType, strPageCategory ) )
538 {
539 if( securityHeaderToModify.getId( ) != securityHeader.getId( ) )
540 {
541 return false;
542 }
543 }
544
545 return true;
546 }
547
548
549
550
551
552
553
554
555 public String getConfirmRemoveSecurityHeader( HttpServletRequest request )
556 {
557 Map<String, String> parameters = new HashMap<>( );
558 parameters.put( PARAMETER_SECURITY_HEADER_ID, request.getParameter( PARAMETER_SECURITY_HEADER_ID ) );
559 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_REMOVE_SECURITY_HEADERS ) );
560
561 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE, JSP_REMOVE_SECURITY_HEADERS, AdminMessage.TYPE_CONFIRMATION, parameters );
562 }
563
564
565
566
567
568
569
570
571
572
573 public String doRemoveSecurityHeader( HttpServletRequest request ) throws AccessDeniedException
574 {
575 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_REMOVE_SECURITY_HEADERS ) )
576 {
577 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
578 }
579 String strId = request.getParameter( PARAMETER_SECURITY_HEADER_ID );
580 getSecurityHeaderService( ).remove( Integer.parseInt( strId ) );
581
582 return JSP_MANAGE_SECURITY_HEADERS;
583 }
584
585
586
587
588
589
590
591
592
593
594 public String doSecurityHeaderAction( HttpServletRequest request ) throws AccessDeniedException
595 {
596 String strAction = request.getParameter( PARAMETER_ACTION );
597 int nId = Integer.parseInt( request.getParameter( PARAMETER_SECURITY_HEADER_ID ) );
598
599 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MANAGE_SECURITY_HEADERS ) )
600 {
601 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
602 }
603 switch( strAction )
604 {
605 case ACTION_ENABLE:
606 getSecurityHeaderService( ).enable( nId );
607 break;
608 case ACTION_DISABLE:
609 getSecurityHeaderService( ).disable( nId );
610 break;
611 default:
612 AppLogService.error( "Unknown security header action : {}", strAction );
613 }
614
615 return getHomeUrl( request );
616 }
617
618
619
620
621
622
623 private SecurityHeaderService getSecurityHeaderService( )
624 {
625 return SpringContextService.getBean( "securityHeaderService" );
626 }
627 }