View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.casexternal.authentication;
35  
36  //import edu.yale.its.tp.cas.client.filter.CASFilter;
37  import fr.paris.lutece.plugins.mylutece.authentication.ExternalAuthentication;
38  import fr.paris.lutece.plugins.mylutece.modules.casexternal.service.CASExternalPlugin;
39  import fr.paris.lutece.portal.service.security.LoginRedirectException;
40  
41  import fr.paris.lutece.portal.service.security.LuteceUser;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import org.jasig.cas.client.authentication.AttributePrincipal;
45  
46  import java.util.Iterator;
47  import java.util.Map;
48  
49  import javax.security.auth.login.LoginException;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  
54  /**
55   * The class provides an implementation of the inherited abstract class
56   * PortalAuthentication based on CAS
57   *
58   */
59  public class CASExternalAuthentication extends ExternalAuthentication
60  {
61      // //////////////////////////////////////////////////////////////////////////////////////////////
62      // Constants
63      private static final String PROPERTY_AUTH_SERVICE_NAME = AppPropertiesService.getProperty( "mylutece-casexternal.service.name" );
64      private static final String PROPERTY_DEFAULT_ROLE_NAME = AppPropertiesService.getProperty( "mylutece-casexternal.role.name" );
65      private static final String PROPERTY_USER_DIRECTION = "mylutece-casexternal.user.direction";
66      private static final String PROPERTY_USER_ROLE = "mylutece-casexternal.user.role";
67  
68      /**
69       * Constructor
70       */
71      public CASExternalAuthentication(  )
72      {
73          super(  );
74      }
75  
76      /**
77       * Gets the Authentication service name
78       *
79       * @return The name of the authentication service
80       */
81      public String getAuthServiceName(  )
82      {
83          return PROPERTY_AUTH_SERVICE_NAME;
84      }
85  
86      /**
87       * Gets the Authentication type
88       *
89       * @param request
90       *            The HTTP request
91       * @return The type of authentication
92       */
93      public String getAuthType( HttpServletRequest request )
94      {
95          return HttpServletRequest.BASIC_AUTH;
96      }
97  
98      /**
99       * This methods logout the user
100      *
101      * @param user
102      *            The user
103      */
104     public void logout( LuteceUser user )
105     {
106     }
107 
108     /**
109      * 
110      *{@inheritDoc}
111      */
112     public String[] getRolesByUser( LuteceUser user )
113     {
114         return null;
115     }
116 
117     /**
118      * This method returns an anonymous Lutece user
119      *
120      * @return An anonymous Lutece user
121      */
122     public LuteceUser getAnonymousUser(  )
123     {
124         return new CASExternalUser( LuteceUser.ANONYMOUS_USERNAME, this );
125     }
126 
127     /**
128      * 
129      *{@inheritDoc}
130      */
131     public LuteceUser getHttpAuthenticatedUser(HttpServletRequest request) {
132 
133         String strCASExternalUserLogin = request.getRemoteUser();
134         AppLogService.debug("You are succesfully logged in as user "	+ request.getRemoteUser());
135         CASExternalUser user = null;
136         String strUserDir;
137         String strUserRole;
138 
139         if ( strCASExternalUserLogin != null )
140         {
141             user = new CASExternalUser( strCASExternalUserLogin, this );
142         }
143 
144 	AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
145 	Map attributes = principal.getAttributes();
146 
147 	if (attributes.size() > 0) {
148 
149 		AppLogService.debug("You have " + attributes.size() + " attributes : ");
150 		Iterator keyIterator = attributes.keySet().iterator();
151 
152 		while (keyIterator.hasNext()) {
153 
154 			String strKey = keyIterator.next().toString();
155 			String strValue = attributes.get(strKey).toString();
156                         user.setUserInfo(strKey, strValue);
157 			AppLogService.debug("key : '" + strKey + "' / value : '" + strValue + "'");
158 		}
159 
160                 //TODO
161                 //bouchon à supprimer lorsque la direction et la notion de management sera dans l'IAM
162                 strUserDir = AppPropertiesService.getProperty( PROPERTY_USER_DIRECTION );
163                 strUserRole = AppPropertiesService.getProperty( PROPERTY_USER_ROLE );
164 
165                 user.setUserInfo("direction", strUserDir);
166                 AppLogService.debug("direction : '" + strUserDir + "'");
167                 user.setUserInfo("role", strUserRole);
168                 AppLogService.debug("role : '" + strUserRole + "'");
169 
170 	} 
171         else
172         {
173 		AppLogService.debug("You have no attributes set");
174 	}
175 
176         return user;
177     }
178 
179     /**
180      * 
181      *{@inheritDoc}
182      */
183     public LuteceUser login(String string, String string1, HttpServletRequest hsr) throws LoginException, LoginRedirectException {
184         throw new UnsupportedOperationException("Not supported yet.");
185     }
186 
187     /**
188      * 
189      *{@inheritDoc}
190      */
191     public boolean isUserInRole(LuteceUser lu, HttpServletRequest hsr, String string) {
192         throw new UnsupportedOperationException("Not supported yet.");
193     }
194 
195     /**
196      * 
197      *{@inheritDoc}
198      */
199 	public String getName()
200 	{
201 		return CASExternalPlugin.PLUGIN_NAME;
202 	}
203 
204 	/**
205 	 * 
206 	 *{@inheritDoc}
207 	 */
208 	public String getPluginName()
209 	{
210 		return CASExternalPlugin.PLUGIN_NAME;
211 	}
212 }