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.util.signrequest;
35
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39
40 import javax.servlet.http.HttpServletRequest;
41
42
43
44
45 public class HeaderHashAuthenticator extends AbstractPrivateKeyAuthenticator implements RequestAuthenticator
46 {
47 private static final String HEADER_SIGNATURE = "Lutece-Request-Signature";
48 private static final String HEADER_TIMESTAMP = "Lutece-Request-Timestamp";
49
50
51
52
53 @Override
54 public boolean isRequestAuthenticated( HttpServletRequest request )
55 {
56 String strHash1 = request.getHeader( HEADER_SIGNATURE );
57 String strTimestamp = request.getHeader( HEADER_TIMESTAMP );
58
59
60 if ( ( strHash1 == null ) || ( strTimestamp == null ) )
61 {
62 LOGGER.info( "SignRequest - Invalid signature" );
63
64 return false;
65 }
66
67 if ( !isValidTimestamp( strTimestamp ) )
68 {
69 LOGGER.info( "SignRequest - Invalid timestamp : " + strTimestamp );
70
71 return false;
72 }
73
74 List<String> listElements = new ArrayList<String>( );
75
76 for ( String strParameter : getSignatureElements( ) )
77 {
78 String strValue = request.getParameter( strParameter );
79
80 if ( strValue != null )
81 {
82 listElements.add( strValue );
83 }
84 }
85
86 String strHash2 = buildSignature( listElements, strTimestamp, getPrivateKey( ) );
87
88 return strHash1.equals( strHash2 );
89 }
90
91
92
93
94 @Override
95 public AuthenticateRequestInformations getSecurityInformations( List<String> elements )
96 {
97 String strTimestamp = String.valueOf( new Date( ).getTime( ) );
98 String strSignature = buildSignature( elements, strTimestamp, getPrivateKey( ) );
99
100 return new AuthenticateRequestInformations().addSecurityHeader(HEADER_TIMESTAMP,strTimestamp).addSecurityHeader(HEADER_SIGNATURE, strSignature);
101
102 }
103 }