View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.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   * OAuthService
60   */
61  public final class OAuthService
62  {
63      private Map<String, OAuthAuthentication> _mapAuthentications;
64  
65      /**
66       * Private constructor
67       */
68      private OAuthService(  )
69      {
70          // nothing
71          _mapAuthentications = new HashMap<String, OAuthAuthentication>(  );
72      }
73  
74      /**
75       * Inits the authentications.
76       * <strong>Plugin needs to be configured (i.e. pool set)</strong>
77       */
78      public void init(  )
79      {
80          Plugin plugin = PluginService.getPlugin( OAuthPlugin.PLUGIN_NAME );
81  
82          if ( plugin != null )
83          {
84              // call home to register available auth.
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      * Gets the authentication
104      * @param strAuthName the auth name
105      * @return the authentication found, <code>null</code> otherwise.
106      */
107     public OAuthAuthentication getAuthentication( String strAuthName )
108     {
109         return _mapAuthentications.get( strAuthName );
110     }
111 
112     /**
113      * Finds the registered authentication list.
114      * @return the registered authentications
115      */
116     public List<OAuthAuthentication> getListAuthentication(  )
117     {
118         return new ArrayList<OAuthAuthentication>( _mapAuthentications.values(  ) );
119     }
120 
121     /**
122      * Call {@link MultiLuteceAuthentication} activation.
123      * @param authentication the authentication
124      */
125     private void registerAuthentication( OAuthAuthentication authentication )
126     {
127         MultiLuteceAuthentication.registerAuthentication( authentication );
128         _mapAuthentications.put( authentication.getName(  ), authentication );
129     }
130 
131     /**
132      * Call {@link MultiLuteceAuthentication} activation.
133      * @param strAuthenticationName the authentication
134      */
135     private void removeAuthentication( String strAuthenticationName )
136     {
137         MultiLuteceAuthentication.removeAuthentication( strAuthenticationName );
138         _mapAuthentications.remove( strAuthenticationName );
139     }
140 
141     /**
142      * Creates a new authentication and registers it.
143      * @param authentication the authentication to create
144      * @param plugin the plugin
145      */
146     public void createNewAuthentication( OAuthAuthentication authentication, Plugin plugin )
147     {
148         OAuthAuthenticationHome.create( authentication, plugin );
149         registerAuthentication( authentication );
150     }
151 
152     /**
153      * Updates the authentication : registers it and saves it.
154      * @param authentication the authentication
155      * @param plugin the plugin
156      */
157     public void updateAuthentication( OAuthAuthentication authentication, Plugin plugin )
158     {
159         OAuthAuthenticationHome.update( authentication, plugin );
160         // update current authentication since name is not mutable.
161         registerAuthentication( authentication );
162     }
163 
164     /**
165      * Removes the authentication
166      * @param strAuthenticationName the authentication name.
167      * @param plugin the plugin
168      */
169     public void removeAuthentication( String strAuthenticationName, Plugin plugin )
170     {
171         OAuthAuthenticationHome.remove( strAuthenticationName, plugin );
172         removeAuthentication( strAuthenticationName );
173     }
174 
175     /**
176      * Do the actual authentication
177      * @param request the request with auth_provider parameter
178      * @return portal url if no error
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 }