View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.mydashboard.modules.myaccount.web;
35  
36  import java.io.IOException;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import com.fasterxml.jackson.core.type.TypeReference;
44  import com.fasterxml.jackson.databind.DeserializationFeature;
45  import com.fasterxml.jackson.databind.ObjectMapper;
46  
47  import fr.paris.lutece.plugins.oauth2.business.Token;
48  import fr.paris.lutece.plugins.oauth2.dataclient.AbstractDataClient;
49  import fr.paris.lutece.plugins.openamidentityclient.business.FederationLink;
50  import fr.paris.lutece.plugins.openamidentityclient.service.OpenamIdentityException;
51  import fr.paris.lutece.plugins.openamidentityclient.service.OpenamIdentityService;
52  import fr.paris.lutece.portal.service.security.LuteceUser;
53  import fr.paris.lutece.portal.service.security.SecurityService;
54  import fr.paris.lutece.portal.service.util.AppLogService;
55  import fr.paris.lutece.portal.service.util.AppPathService;
56  import fr.paris.lutece.portal.service.util.AppPropertiesService;
57  
58  /**
59   * UserInfoDataClient
60   */
61  public class FederationLinkDataClient extends AbstractDataClient
62  {
63  
64      public static final String PROPERTY_FEDERATION_LINK_IDENTITY_PROVIDER="mydashboard-myaccount.federationLinkIdentityProvider";
65      public static final String PROPERTY_FEDERATION_LINK_IDENTITY_FIELD_USER_NAME="mydashboard-myaccount.federationLinkIdentityFieldUserName";
66      public static final String PROPERTY_FEDERATION_LINK_IDENTITY_FIELD_USER_ID="mydashboard-myaccount.federationLinkIdentityFieldUserId";
67      
68      
69  
70      private static ObjectMapper _mapper;
71  
72      static
73      {
74          _mapper = new ObjectMapper( );
75          _mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
76      }
77  
78      /**
79       * {@inheritDoc }
80       */
81      @Override
82      public void handleToken( Token token, HttpServletRequest request, HttpServletResponse response )
83      {
84      
85      	String  strFederationLinkStatus=MyDashboardFederationLinkComponent.FEDERATION_LINK_CREATTION_LINK_STATUS_KO  ;
86      	LuteceUser user=null;
87          try
88          {
89          	 user = SecurityService.getInstance(  ).getRegisteredUser( request );
90               
91               if ( user != null )
92               {
93              	 Map<String, Object> mapInfo = parse( getData( token ) );
94              	 String strFederationLinkProvider=AppPropertiesService.getProperty(PROPERTY_FEDERATION_LINK_IDENTITY_PROVIDER);
95              	 String strFederationLinkFieldUserId=AppPropertiesService.getProperty(PROPERTY_FEDERATION_LINK_IDENTITY_FIELD_USER_ID);
96              	 String strFederationLinkFieldUserName=AppPropertiesService.getProperty(PROPERTY_FEDERATION_LINK_IDENTITY_FIELD_USER_NAME);
97              	 
98              	 FederationLink federationLink=new FederationLink(user.getName(), strFederationLinkProvider, (String)mapInfo.get(strFederationLinkFieldUserId),(String) mapInfo.get(strFederationLinkFieldUserName));		
99              	 OpenamIdentityService.getService().createFederationLink(federationLink);
100             	 strFederationLinkStatus=MyDashboardFederationLinkComponent.FEDERATION_LINK_CREATTION_LINK_STATUS_OK  ;
101             	 
102              }		
103          
104         }
105         catch( IOException ex )
106         {
107             _logger.error( "Error parsing UserInfo ", ex );
108  
109         } catch (OpenamIdentityException e) {
110         	   _logger.error( "Error creating federation link  for user", e );
111         }
112         finally {
113         	  try {
114         		  
115         		   String strNextUrl = AppPropertiesService.getProperty(MyDashboardFederationLinkComponent.PROPERTY_FEDERAION_LINK_REDIRECT_URL );
116                    strNextUrl += "&"+MyDashboardFederationLinkComponent.PARAMETER_FEDERATION_LINK_CREATTION_LINK_STATUS   + "=" + strFederationLinkStatus;
117 
118                    response.sendRedirect(AppPathService.getAbsoluteUrl( request, strNextUrl ));
119 			} catch (IOException e) {
120 				   _logger.error( "Error for redirect user after creation fedaration link ", e );			}
121 		}
122       
123 
124     }
125 
126     /**
127      * parse the JSON for a token
128      * 
129      * @param strJson
130      *            The JSON
131      * @return The UserInfo
132      * @throws java.io.IOException
133      *             if an error occurs
134      */
135     Map<String, Object> parse( String strJson ) throws IOException
136     {
137         TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>( )
138         {
139         };
140 
141         return _mapper.readValue( strJson, typeRef );
142     }
143 
144     @Override
145     public void handleError( HttpServletRequest request, HttpServletResponse response, String strError )
146     {
147     	String  strFederationLinkStatus=MyDashboardFederationLinkComponent.FEDERATION_LINK_CREATTION_LINK_STATUS_KO  ;
148         try
149         {
150 
151         	
152      	   String strNextUrl = AppPropertiesService.getProperty( MyDashboardFederationLinkComponent.PROPERTY_FEDERAION_LINK_REDIRECT_URL );
153            AppPathService.getAbsoluteUrl( request, strNextUrl );
154      	   
155      	   strNextUrl += "&"+MyDashboardFederationLinkComponent.PARAMETER_FEDERATION_LINK_CREATTION_LINK_STATUS   + "=" + strFederationLinkStatus;
156 
157 		response.sendRedirect( strNextUrl );
158         }
159         catch( IOException e )
160         {
161             AppLogService.error( "Error during federation linked", e );
162         }
163 
164     }
165 }