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.mylutece.modules.oauth2.authentication;
35
36 import java.io.IOException;
37 import java.io.Serializable;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Map;
42 import java.util.stream.Collectors;
43
44 import javax.security.auth.login.LoginException;
45 import javax.servlet.http.HttpServletRequest;
46
47 import fr.paris.lutece.plugins.mylutece.authentication.PortalAuthentication;
48 import fr.paris.lutece.plugins.mylutece.business.LuteceUserAttributeDescription;
49 import fr.paris.lutece.plugins.mylutece.business.LuteceUserRoleDescription;
50 import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeHome;
51 import fr.paris.lutece.plugins.mylutece.modules.oauth2.service.Oauth2Service;
52 import fr.paris.lutece.plugins.mylutece.service.MyLutecePlugin;
53 import fr.paris.lutece.plugins.oauth2.business.Token;
54 import fr.paris.lutece.plugins.oauth2.service.DataClientService;
55 import fr.paris.lutece.plugins.oauth2.service.TokenService;
56 import fr.paris.lutece.portal.business.role.RoleHome;
57 import fr.paris.lutece.portal.service.plugin.Plugin;
58 import fr.paris.lutece.portal.service.plugin.PluginService;
59 import fr.paris.lutece.portal.service.security.LoginRedirectException;
60 import fr.paris.lutece.portal.service.security.LuteceUser;
61 import fr.paris.lutece.portal.service.security.SecurityService;
62 import fr.paris.lutece.portal.service.util.AppLogService;
63 import fr.paris.lutece.portal.service.util.AppPropertiesService;
64
65
66
67
68 public class Oauth2Authentication extends PortalAuthentication implements Serializable
69 {
70
71
72 private static final String PROPERTY_AUTH_SERVICE_NAME = "mylutece-oauth2.service.name";
73 private static final String CONSTANT_PATH_ICON = "images/local/skin/plugins/mylutece/modules/openid/mylutece-openid.png";
74 private static final String PLUGIN_NAME = "mylutece-oauth2";
75 private static final long serialVersionUID = 1L;
76 private static final String authDataClientName = "authData";
77
78
79
80
81
82
83 @Override
84 public String getAuthServiceName( )
85 {
86 return AppPropertiesService.getProperty( PROPERTY_AUTH_SERVICE_NAME );
87 }
88
89
90
91
92
93
94
95
96 @Override
97 public String getAuthType( HttpServletRequest request )
98 {
99 return HttpServletRequest.BASIC_AUTH;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 @Override
119 public LuteceUser processLogin( String strUserName, String strUserPassword, HttpServletRequest request ) throws LoginException, LoginRedirectException
120 {
121 return getHttpAuthenticatedUser( request );
122 }
123
124
125
126
127
128
129
130 @Override
131 public void logout( LuteceUser user )
132 {
133 }
134
135
136
137
138
139
140 @Override
141 public LuteceUser getAnonymousUser( )
142 {
143 return new Oauth2User( LuteceUser.ANONYMOUS_USERNAME, null, this );
144 }
145
146
147
148
149
150 @Override
151 public String getIconUrl( )
152 {
153 return CONSTANT_PATH_ICON;
154 }
155
156
157
158
159
160 @Override
161 public String getName( )
162 {
163 return PLUGIN_NAME;
164 }
165
166
167
168
169
170 @Override
171 public String getPluginName( )
172 {
173 return PLUGIN_NAME;
174 }
175
176
177
178
179
180 @Override
181 public boolean isMultiAuthenticationSupported( )
182 {
183 return false;
184 }
185
186
187
188
189
190
191
192
193 @Override
194 public LuteceUser getHttpAuthenticatedUser( HttpServletRequest request )
195 {
196 LuteceUser user = null;
197 user = SecurityService.getInstance( ).getRegisteredUser( request );
198
199 if ( user != null && user instanceof Oauth2User )
200 {
201 Oauth2User./../../../../../fr/paris/lutece/plugins/mylutece/modules/oauth2/authentication/Oauth2User.html#Oauth2User">Oauth2User userOauth = (Oauth2User) user;
202 if ( userOauth.getToken( ).getRefreshToken( ) != null )
203 {
204
205 AuthDataClient./../../fr/paris/lutece/plugins/mylutece/modules/oauth2/authentication/AuthDataClient.html#AuthDataClient">AuthDataClient authDataClient = (AuthDataClient) DataClientService.instance( ).getClient( authDataClientName );
206 Token token = TokenService.getService( ).getTokenByRefreshToken( userOauth.getToken( ).getRefreshToken( ) );
207 try
208 {
209 Map<String, Object> mapUserInfo = authDataClient.parse( authDataClient.getData( token ) );
210 return Oauth2Service.getInstance( ).processAuthentication( request, mapUserInfo, token );
211
212 }
213 catch( IOException e )
214 {
215
216 AppLogService.error( "error during retrieving user info with refresh token ", e );
217 }
218
219 }
220
221
222
223
224 }
225
226 return user;
227 }
228
229
230
231
232
233
234
235 @Override
236 public List<LuteceUserAttributeDescription> getLuteceUserAttributesProvided(Locale locale)
237 {
238
239 return Oauth2Service.getInstance().getLuteceUserAttributesProvided(locale);
240 }
241 }