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.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
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
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
128
129
130
131
132
133
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 }