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.oauth.service;
35
36 import fr.paris.lutece.plugins.mylutece.authentication.MultiLuteceAuthentication;
37 import fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication;
38 import fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthUser;
39 import fr.paris.lutece.plugins.mylutece.modules.oauth.business.OAuthAuthenticationHome;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.plugin.PluginService;
42 import fr.paris.lutece.portal.service.security.LuteceAuthentication;
43 import fr.paris.lutece.portal.service.security.SecurityService;
44 import fr.paris.lutece.portal.service.util.AppException;
45 import fr.paris.lutece.portal.service.util.AppLogService;
46 import fr.paris.lutece.portal.service.util.AppPathService;
47 import fr.paris.lutece.portal.web.PortalJspBean;
48
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59
60
61 public final class OAuthService
62 {
63 private Map<String, OAuthAuthentication> _mapAuthentications;
64
65
66
67
68 private OAuthService( )
69 {
70
71 _mapAuthentications = new HashMap<String, OAuthAuthentication>( );
72 }
73
74
75
76
77
78 public void init( )
79 {
80 Plugin plugin = PluginService.getPlugin( OAuthPlugin.PLUGIN_NAME );
81
82 if ( plugin != null )
83 {
84
85 try
86 {
87 List<OAuthAuthentication> listAuthentication = OAuthAuthenticationHome.findAll( plugin );
88
89 for ( OAuthAuthentication authentication : listAuthentication )
90 {
91 registerAuthentication( authentication );
92 }
93 }
94 catch ( AppException e )
95 {
96 AppLogService.error( "Unable to find registered OAuth authentications in module-mylutece-oauth : " +
97 e.getMessage( ), e );
98 }
99 }
100 }
101
102
103
104
105
106
107 public OAuthAuthentication getAuthentication( String strAuthName )
108 {
109 return _mapAuthentications.get( strAuthName );
110 }
111
112
113
114
115
116 public List<OAuthAuthentication> getListAuthentication( )
117 {
118 return new ArrayList<OAuthAuthentication>( _mapAuthentications.values( ) );
119 }
120
121
122
123
124
125 private void registerAuthentication( OAuthAuthentication authentication )
126 {
127 MultiLuteceAuthentication.registerAuthentication( authentication );
128 _mapAuthentications.put( authentication.getName( ), authentication );
129 }
130
131
132
133
134
135 private void removeAuthentication( String strAuthenticationName )
136 {
137 MultiLuteceAuthentication.removeAuthentication( strAuthenticationName );
138 _mapAuthentications.remove( strAuthenticationName );
139 }
140
141
142
143
144
145
146 public void createNewAuthentication( OAuthAuthentication authentication, Plugin plugin )
147 {
148 OAuthAuthenticationHome.create( authentication, plugin );
149 registerAuthentication( authentication );
150 }
151
152
153
154
155
156
157 public void updateAuthentication( OAuthAuthentication authentication, Plugin plugin )
158 {
159 OAuthAuthenticationHome.update( authentication, plugin );
160
161 registerAuthentication( authentication );
162 }
163
164
165
166
167
168
169 public void removeAuthentication( String strAuthenticationName, Plugin plugin )
170 {
171 OAuthAuthenticationHome.remove( strAuthenticationName, plugin );
172 removeAuthentication( strAuthenticationName );
173 }
174
175
176
177
178
179
180 public String doAuthentication( HttpServletRequest request )
181 {
182 String strAuthProvider = request.getParameter( "auth_provider" );
183
184 if ( AppLogService.isDebugEnabled( ) )
185 {
186 AppLogService.debug( "OAuth provider : " + strAuthProvider );
187 }
188
189 if ( SecurityService.getInstance( ).isMultiAuthenticationSupported( ) )
190 {
191 MultiLuteceAuthentication mainAuthentication = (MultiLuteceAuthentication) SecurityService.getInstance( )
192 .getAuthenticationService( );
193
194 LuteceAuthentication authentication = mainAuthentication.getLuteceAuthentication( strAuthProvider );
195
196 if ( ( authentication == null ) || !( authentication instanceof OAuthAuthentication ) )
197 {
198 throw new AppException( "Can't use provided authentication paramater : " + strAuthProvider +
199 ". Found : " + authentication );
200 }
201
202 OAuthAuthentication oAuthAuthentication = (OAuthAuthentication) authentication;
203 OAuthUser user = oAuthAuthentication.getUser( request );
204 SecurityService.getInstance( ).registerUser( request, user );
205 }
206
207 String strReturnUrl = PortalJspBean.getLoginNextUrl( request );
208
209 if ( strReturnUrl != null )
210 {
211 return strReturnUrl;
212 }
213
214 return AppPathService.getBaseUrl( request ) + AppPathService.getPortalUrl( );
215 }
216 }