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.saml.authentication;
35  
36  import fr.paris.lutece.plugins.mylutece.authentication.PortalAuthentication;
37  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.ConfigProperties;
38  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.Constants;
39  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.engine.SAMLTokenHandler;
40  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.CertificateValidationException;
41  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.InvalidAttributeException;
42  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLCheckerException;
43  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLParsingException;
44  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLTokenExtractorException;
45  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SignatureValidationException;
46  import fr.paris.lutece.plugins.mylutece.modules.saml.service.SAMLPlugin;
47  import fr.paris.lutece.portal.service.security.LoginRedirectException;
48  import fr.paris.lutece.portal.service.security.LuteceUser;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Iterator;
54  import java.util.Map;
55  import java.util.Map.Entry;
56  
57  import javax.security.auth.login.LoginException;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  
62  public class SAMLAuthentication extends PortalAuthentication
63  {
64      /**
65       * This method create an anonymous user
66       *
67       * @return A LuteceUser object corresponding to an anonymous user
68       */
69      public LuteceUser getAnonymousUser(  )
70      {
71          return new SAMLUser( LuteceUser.ANONYMOUS_USERNAME, this );
72      }
73  
74      /**
75       * Gets the Authentification service name
76       * @return The name of the authentication service
77       */
78      public String getAuthServiceName(  )
79      {
80          return this.getClass(  ).getName(  );
81      }
82  
83      /**
84       * Gets the Authentification type
85       * @param request The HTTP request
86       * @return The type of authentication
87       */
88      public String getAuthType( HttpServletRequest request )
89      {
90          return HttpServletRequest.BASIC_AUTH;
91      }
92  
93      /**
94       * Checks that the current user is associated to a given role
95       * @param user The user
96       * @param request The HTTP request
97       * @param strRole The role name
98       * @return Returns true if the user is associated to the role, otherwise false
99       */
100     public boolean isUserInRole( LuteceUser user, HttpServletRequest request, String strRole )
101     {
102         return true;
103     }
104 
105     /**
106      * Checks the login
107      *
108      * @param strUserName The username
109      * @param strUserPassword The user's passord
110      * @param request The HttpServletRequest
111      *
112      * @return A LuteceUser object corresponding to the login
113      *
114      * @throws LoginException If a Login error occured
115      * @throws LoginRedirectException If the the login process should be redirected
116      */
117     public LuteceUser login( String strUserName, String strUserPassword, HttpServletRequest request )
118         throws LoginException, LoginRedirectException
119     {
120         SAMLTokenHandler tokenHandler = new SAMLTokenHandler(  );
121         SAMLUser user = null;
122 
123         try
124         {
125             // Check Token
126             tokenHandler.checkSAMLResponse( request );
127 
128             // Create LuteceUser
129             user = createSAMLUser( tokenHandler );
130         }
131         catch ( SignatureValidationException e )
132         {
133             AppLogService.error( e.getMessage(  ), e );
134             throw new LoginException(  );
135         }
136         catch ( CertificateValidationException e )
137         {
138             AppLogService.error( e.getMessage(  ), e );
139             throw new LoginException(  );
140         }
141         catch ( InvalidAttributeException e )
142         {
143             AppLogService.error( e.getMessage(  ), e );
144             throw new LoginException(  );
145         }
146         catch ( SAMLTokenExtractorException e )
147         {
148             AppLogService.error( e.getMessage(  ), e );
149             throw new LoginException(  );
150         }
151         catch ( SAMLParsingException e )
152         {
153             AppLogService.error( e.getMessage(  ), e );
154             throw new LoginException(  );
155         }
156         catch ( SAMLCheckerException e )
157         {
158             AppLogService.error( e.getMessage(  ), e );
159             throw new LoginException(  );
160         }
161 
162         return user;
163     }
164 
165     /**
166      * logout the user
167      * @param user The user
168      */
169     public void logout( LuteceUser user )
170     {
171     }
172 
173     private SAMLUser createSAMLUser( SAMLTokenHandler tokenHandler )
174         throws SAMLParsingException
175     {
176         // Create LuteceUser
177         SAMLUser user = new SAMLUser( tokenHandler.getLuteceUserName(  ), this );
178 
179         // Set LuteceUser infos
180         Map<String, String> userInfos = tokenHandler.getLuteceUserProperties(  );
181         Iterator<Entry<String, String>> it = userInfos.entrySet(  ).iterator(  );
182 
183         while ( it.hasNext(  ) )
184         {
185             Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next(  );
186             user.setUserInfo( pairs.getKey(  ), pairs.getValue(  ) );
187         }
188 
189         // Set User Groups
190         Collection<String> groups = tokenHandler.getLuteceUserGroups(  );
191         user.setGroups( groups );
192 
193         // Set User Role
194         Collection<String> roles = new ArrayList<String>(  );
195         roles.add( ConfigProperties.getInstance(  ).getProperty( Constants.LUTECE_USER_ROLE_PROP ) );
196         user.setRoles( roles );
197 
198         AppLogService.info( "Cr�ation LuteceUser : Nom=" + user.getName(  ) );
199 
200         return user;
201     }
202 
203     public String[] getRolesByUser( LuteceUser user )
204     {
205         return null;
206     }
207 
208     /**
209      * 
210      *{@inheritDoc}
211      */
212 	public String getName()
213 	{
214 		return SAMLPlugin.PLUGIN_NAME;
215 	}
216 
217 	/**
218 	 * 
219 	 *{@inheritDoc}
220 	 */
221 	public String getPluginName()
222 	{
223 		return SAMLPlugin.PLUGIN_NAME;
224 	}
225 }