View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * AdresseDataClient.java
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     //TODO remove this when the http header is "Authorization: Bearer XXX"
117     //currently, it is "Authorization: AccessToken XXX"
118     //Also when the token is the one from franceconnect, not hardcoded
119     class SIVTokenAuthenticator implements RequestAuthenticator
120 {
121     private String _strAccessToken;
122 
123     /**
124      * Constructor
125      * @param strAccessToken The access token value
126      */
127     public SIVTokenAuthenticator( String strAccessToken )
128     {
129         _strAccessToken = strAccessToken;
130     }
131 
132     /**
133      * {@inheritDoc }
134      */
135     @Override
136     public boolean isRequestAuthenticated( HttpServletRequest request )
137     {
138         return false; // not used
139     }
140 
141     /**
142      * {@inheritDoc }
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 }