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.franceconnect.oidc.dataclient;
35  
36  import fr.paris.lutece.plugins.franceconnect.oidc.Token;
37  import fr.paris.lutece.plugins.franceconnect.service.BearerTokenAuthenticator;
38  import fr.paris.lutece.plugins.franceconnect.web.Constants;
39  import fr.paris.lutece.util.httpaccess.HttpAccess;
40  import fr.paris.lutece.util.httpaccess.HttpAccessException;
41  import fr.paris.lutece.util.signrequest.RequestAuthenticator;
42  
43  import org.apache.log4j.Logger;
44  
45  import java.util.Iterator;
46  import java.util.Set;
47  
48  
49  /**
50   * DataClient
51   */
52  public abstract class AbstractDataClient implements DataClient
53  {
54      protected static Logger _logger = Logger.getLogger( Constants.LOGGER_FRANCECONNECT );
55      
56      private static final char SEPARATOR = '+';
57      
58      private String _strName;
59      private String _strRedirectUri;
60      private String _strDataServerUri;
61      private String _strTokenMethod;
62      private Set<String> _scope;
63      private Set<String> _acrValues;
64  
65      /**
66       * {@inheritDoc }
67       */
68      @Override
69      public String getName(  )
70      {
71          return _strName;
72      }
73  
74      /**
75       * {@inheritDoc }
76       */
77      @Override
78      public void setName( String strName )
79      {
80          _strName = strName;
81      }
82  
83      /**
84       * {@inheritDoc }
85       */
86      @Override
87      public Set getScope(  )
88      {
89          return _scope;
90      }
91  
92      /**
93       * {@inheritDoc }
94       */
95      @Override
96      public void setScope( Set scope )
97      {
98          _scope = scope;
99      }
100 
101     /**
102      * {@inheritDoc }
103      */
104     @Override
105     public String getScopes(  )
106     {
107         StringBuilder sbScopes = new StringBuilder(  );
108 
109         Iterator iterator = _scope.iterator(  );
110         boolean bFirst = true;
111 
112         while ( iterator.hasNext(  ) )
113         {
114             if ( !bFirst )
115             {
116                 sbScopes.append( SEPARATOR );
117             }
118 
119             bFirst = false;
120             sbScopes.append( iterator.next(  ) );
121         }
122 
123         return sbScopes.toString(  );
124     }
125 
126     /**
127      * {@inheritDoc }
128      */
129     @Override
130     public String getRedirectUri(  )
131     {
132         return _strRedirectUri;
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public void setRedirectUri( String strRedirectUri )
140     {
141         _strRedirectUri = strRedirectUri;
142     }
143 
144     /**
145      * {@inheritDoc }
146      */
147     @Override
148     public Set getAcrValuesSet(  )
149     {
150         return _acrValues;
151     }
152 
153     /**
154      * {@inheritDoc }
155      */
156     @Override
157     public void setAcrValuesSet( Set acrValues )
158     {
159         _acrValues = acrValues;
160     }
161 
162     /**
163      * {@inheritDoc }
164      */
165     @Override
166     public String getAcrValues(  )
167     {
168         if( _acrValues==null || _acrValues.isEmpty() )
169         {
170             return null;
171         }
172         
173         StringBuilder sbAcrValues = new StringBuilder(  );
174 
175         Iterator iterator = _acrValues.iterator(  );
176         boolean bFirst = true;
177 
178         while ( iterator.hasNext(  ) )
179         {
180             if ( !bFirst )
181             {
182                 sbAcrValues.append( SEPARATOR );
183             }
184 
185             bFirst = false;
186             sbAcrValues.append( iterator.next(  ) );
187         }
188 
189         return sbAcrValues.toString(  );
190     }
191 
192     /**
193      * {@inheritDoc }
194      */
195     @Override
196     public String getDataServerUri(  )
197     {
198         return _strDataServerUri;
199     }
200 
201     /**
202      * {@inheritDoc }
203      */
204     @Override
205     public void setDataServerUri( String strDataServerUri )
206     {
207         _strDataServerUri = strDataServerUri;
208     }
209 
210     /**
211      * {@inheritDoc }
212      */
213     @Override
214     public String getTokenMethod(  )
215     {
216         return _strTokenMethod;
217     }
218 
219     /**
220      * {@inheritDoc }
221      */
222     @Override
223     public void setTokenMethod( String strTokenMethod )
224     {
225         _strTokenMethod = strTokenMethod;
226     }
227 
228     /**
229      * Send an authenticated request with the access token to retreive data
230      * @param token The token
231      * @return The response
232      */
233     public String getData( Token token )
234     {
235         String strResponse = null;
236         HttpAccess httpAccess = new HttpAccess(  );
237 
238         String strUrl = _strDataServerUri;
239 
240         try
241         {
242             RequestAuthenticator authenticator = new BearerTokenAuthenticator( token.getAccessToken(  ) );
243             strResponse = httpAccess.doGet( strUrl, authenticator, null );
244             _logger.debug( "FranceConnect response : " + strResponse );
245         }
246         catch ( HttpAccessException ex )
247         {
248             _logger.error( "OAuth Login Error" + ex.getMessage(  ), ex );
249         }
250 
251         return strResponse;
252     }
253 }