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.plugins.stationnement.dataclient;
35
36 import java.io.IOException;
37 import java.util.List;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
42
43 import org.apache.commons.httpclient.HttpMethodBase;
44
45 import fr.paris.lutece.plugins.franceconnect.oidc.Token;
46 import fr.paris.lutece.plugins.franceconnect.oidc.dataclient.AbstractDataClient;
47 import fr.paris.lutece.plugins.franceconnect.service.MapperService;
48 import fr.paris.lutece.plugins.stationnement.service.RedirectUtils;
49 import fr.paris.lutece.plugins.stationnement.web.FranceConnectSampleApp;
50 import fr.paris.lutece.portal.service.util.AppLogService;
51 import fr.paris.lutece.util.httpaccess.HttpAccess;
52 import fr.paris.lutece.util.httpaccess.HttpAccessException;
53 import fr.paris.lutece.util.signrequest.RequestAuthenticator;
54
55
56
57
58
59 public class AdresseDataClient extends AbstractDataClient
60 {
61
62 public static final String ATTRIBUTE_USERCARTESGRISES = "stationnement-dc-usercartesgrises";
63
64 @Override
65 public void handleToken( Token token, HttpServletRequest request, HttpServletResponse response )
66 {
67 CarteGrise carteGrise;
68 try
69 {
70 String strCarteGriseJSON = getData( token );
71 carteGrise = MapperService.parse( strCarteGriseJSON, CarteGrise.class );
72 carteGrise.setSource(strCarteGriseJSON);
73 } catch ( Exception ex ) {
74 AppLogService.error( "Error when fetching carte grise" + ex.getMessage( ), ex );
75 carteGrise = new CarteGrise();
76 carteGrise.setAdresse("BOUCHON: 4 rue bouchon");
77 carteGrise.setTPpNom("BOUCHON: DELL");
78 carteGrise.setVNumeroImmatriculation("BOUCHON: AD-711-AF");
79 carteGrise.setSource("BOUCHON: source");
80 }
81
82 HttpSession session = request.getSession( true );
83 session.setAttribute( ATTRIBUTE_USERCARTESGRISES, carteGrise );
84
85 try {
86 String strRedirectUrl = RedirectUtils.getViewUrl( request, FranceConnectSampleApp.VIEW_DEMARCHE_ETAPE2 );
87 response.sendRedirect( strRedirectUrl );
88 }
89 catch ( IOException ex )
90 {
91 AppLogService.error( "Error DataClient Adresse redirect : " + ex.getMessage( ), ex );
92 }
93 }
94
95 public String getData( Token token )
96 {
97 String strResponse = null;
98 HttpAccess httpAccess = new HttpAccess( );
99
100 String strUrl = getDataServerUri();
101
102 try
103 {
104 RequestAuthenticator authenticator = new SIVTokenAuthenticator( token.getAccessToken( ) );
105 strResponse = httpAccess.doGet( strUrl, authenticator, null );
106 _logger.debug( "FranceConnect response : " + strResponse );
107 }
108 catch ( HttpAccessException ex )
109 {
110 _logger.error( "OAuth Login Error" + ex.getMessage( ), ex );
111 }
112
113 return strResponse;
114 }
115
116
117
118
119 class SIVTokenAuthenticator implements RequestAuthenticator
120 {
121 private String _strAccessToken;
122
123
124
125
126
127 public SIVTokenAuthenticator( String strAccessToken )
128 {
129 _strAccessToken = strAccessToken;
130 }
131
132
133
134
135 @Override
136 public boolean isRequestAuthenticated( HttpServletRequest request )
137 {
138 return false;
139 }
140
141
142
143
144 @Override
145 public void authenticateRequest( HttpMethodBase hmb, List<String> list )
146 {
147 hmb.addRequestHeader( "Authorization", String.format( "AccessToken %s", "895fae591ccae777094931e269e46447" ) );
148 hmb.addRequestHeader( "Authorization", String.format( "Bearer %s", "895fae591ccae777094931e269e46447" ) );
149 }
150 }
151 }