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 import fr.paris.lutece.util.signrequest.service.ClientKeyService;
43
44
45
46
47 public class ClientHeaderHashAuthenticator extends AbstractAuthenticator implements RequestAuthenticator
48 {
49 private static final String HEADER_SIGNATURE = "Lutece-Request-Signature";
50 private static final String HEADER_TIMESTAMP = "Lutece-Request-Timestamp";
51 private static final String HEADER_CLIENT_ID = "Lutece-Request-ClientID";
52
53 private String _strClientId;
54 private ClientKeyService _clientKeyService;
55
56
57
58
59
60
61
62 public void setClientId( String strClientId )
63 {
64 _strClientId = strClientId;
65 }
66
67
68
69
70
71
72
73
74 public void setClientKeyService( ClientKeyService clientKeyService )
75 {
76 _clientKeyService = clientKeyService;
77 }
78
79
80
81
82 @Override
83 public boolean isRequestAuthenticated( HttpServletRequest request )
84 {
85 String strHash1 = request.getHeader( HEADER_SIGNATURE );
86 String strTimestamp = request.getHeader( HEADER_TIMESTAMP );
87 String strClientId = request.getHeader( HEADER_CLIENT_ID );
88
89
90 if ( ( strHash1 == null ) || ( strTimestamp == null ) || ( strClientId == null ) )
91 {
92 LOGGER.info( "SignRequest - Invalid signature" );
93
94 return false;
95 }
96
97 if ( !isValidTimestamp( strTimestamp ) )
98 {
99 LOGGER.info( "SignRequest - Invalid timestamp : " + strTimestamp );
100
101 return false;
102 }
103
104 List<String> listElements = new ArrayList<String>( );
105
106 for ( String strParameter : getSignatureElements( ) )
107 {
108 String strValue = request.getParameter( strParameter );
109
110 if ( strValue != null )
111 {
112 listElements.add( strValue );
113 }
114 }
115
116 String strClientKey = _clientKeyService.getKey( strClientId );
117 String strHash2 = buildSignature( listElements, strTimestamp, strClientKey );
118
119 return strHash1.equals( strHash2 );
120 }
121
122
123
124
125 @Override
126 public AuthenticateRequestInformations getSecurityInformations( List<String> elements )
127 {
128 String strTimestamp = String.valueOf( new Date( ).getTime( ) );
129 String strClientKey = _clientKeyService.getKey( _strClientId );
130 String strSignature = buildSignature( elements, strTimestamp, strClientKey );
131
132 return new AuthenticateRequestInformations().addSecurityHeader(HEADER_TIMESTAMP, strTimestamp ).addSecurityHeader(HEADER_CLIENT_ID, _strClientId).addSecurityHeader(HEADER_SIGNATURE, strSignature);
133
134
135 }
136 }