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 import java.util.Map;
40
41 import javax.servlet.http.HttpServletRequest;
42
43
44
45
46 public class RequestHashAuthenticator extends AbstractPrivateKeyAuthenticator implements RequestAuthenticator
47 {
48 private static final String PARAMETER_SIGNATURE = "signature";
49 private static final String PARAMETER_TIMESTAMP = "timestamp";
50
51
52
53
54 @Override
55 public boolean isRequestAuthenticated( HttpServletRequest request )
56 {
57 String strHash1 = request.getParameter( PARAMETER_SIGNATURE );
58 String strTimestamp = request.getParameter( PARAMETER_TIMESTAMP );
59
60
61 if ( ( strHash1 == null ) || ( strTimestamp == null ) )
62 {
63 LOGGER.info( "SignRequest - Invalid signature" );
64
65 return false;
66 }
67
68 if ( !isValidTimestamp( strTimestamp ) )
69 {
70 LOGGER.info( "SignRequest - Invalid timestamp : " + strTimestamp );
71
72 return false;
73 }
74
75 List<String> listElements = new ArrayList<String>( );
76
77 for ( String strParameter : getSignatureElements( ) )
78 {
79 String strValue = request.getParameter( strParameter );
80
81 if ( strValue != null )
82 {
83 listElements.add( strValue );
84 }
85 }
86
87 String strHash2 = buildSignature( listElements, strTimestamp, getPrivateKey( ) );
88
89 return strHash1.equals( strHash2 );
90 }
91
92
93
94
95 @Override
96 public AuthenticateRequestInformations getSecurityInformations( List<String> elements )
97 {
98
99 String strTimestamp = String.valueOf( new Date( ).getTime( ) );
100 String strSignature = buildSignature( elements, strTimestamp, getPrivateKey( ) );
101
102 return new AuthenticateRequestInformations().addSecurityParameter(PARAMETER_TIMESTAMP,strTimestamp).addSecurityParameter(PARAMETER_SIGNATURE, strSignature);
103
104
105
106 }
107
108
109
110
111
112
113
114
115
116
117 public String addExtrasUrlParameters( String strUrl, List<String> listElements )
118 {
119 StringBuilder sbExtrasParameters = new StringBuilder( );
120
121 if ( strUrl.contains( "=" ) )
122 {
123 sbExtrasParameters.append( strUrl ).append( '&' );
124 }
125 else
126 {
127 sbExtrasParameters.append( strUrl ).append( '?' );
128 }
129
130 String strTimestamp = String.valueOf( new Date( ).getTime( ) );
131 sbExtrasParameters.append( PARAMETER_TIMESTAMP ).append( '=' ).append( strTimestamp );
132
133 String strSignature = buildSignature( listElements, strTimestamp, getPrivateKey( ) );
134 sbExtrasParameters.append( '&' ).append( PARAMETER_SIGNATURE ).append( '=' ).append( strSignature );
135
136 return sbExtrasParameters.toString( );
137 }
138
139
140 }