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.cas.web;
35
36 import fr.paris.lutece.plugins.mylutece.modules.cas.authentication.CASAuthentication;
37 import fr.paris.lutece.portal.service.message.SiteMessage;
38 import fr.paris.lutece.portal.service.message.SiteMessageException;
39 import fr.paris.lutece.portal.service.message.SiteMessageService;
40 import fr.paris.lutece.portal.service.security.LoginRedirectException;
41 import fr.paris.lutece.portal.service.security.LuteceUser;
42 import fr.paris.lutece.portal.service.security.SecurityService;
43 import fr.paris.lutece.portal.service.spring.SpringContextService;
44 import fr.paris.lutece.portal.service.util.AppLogService;
45 import fr.paris.lutece.portal.service.util.AppPathService;
46
47 import java.io.IOException;
48 import java.util.Enumeration;
49
50 import javax.security.auth.login.LoginException;
51 import javax.servlet.Filter;
52 import javax.servlet.FilterChain;
53 import javax.servlet.FilterConfig;
54 import javax.servlet.ServletException;
55 import javax.servlet.ServletRequest;
56 import javax.servlet.ServletResponse;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59
60 import org.jasig.cas.client.authentication.DefaultGatewayResolverImpl;
61
62
63
64
65
66 public class LuteceCASFilter implements Filter
67 {
68
69
70
71
72
73 private static final String PARAM_NOCOOKIEMESSAGEKEY = "noCookieMessageKey";
74
75
76
77 private String noCookieMessageKey = null;
78
79
80
81
82
83 private static final String PARAM_REDIRECTAFTERGATEWWAY = "redirectAfterGateway";
84 private static final String PARAM_YES = "yes";
85
86
87
88
89
90 private boolean redirectAfterGateway = false;
91
92
93
94
95
96 @Override
97 public void destroy( )
98 {
99
100 }
101
102
103
104
105
106 @Override
107 public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain )
108 throws IOException, ServletException
109 {
110 HttpServletRequest request = (HttpServletRequest) servletRequest;
111 HttpServletResponse response = (HttpServletResponse) servletResponse;
112
113 Boolean attrSupportsCookies = ( Boolean ) request.getAttribute( ParameterGatewayResolver.ATTR_SUPPORTS_COOKIES );
114 if ( attrSupportsCookies != null && !attrSupportsCookies.booleanValue() && noCookieMessageKey != null )
115 {
116
117 try
118 {
119 SiteMessageService.setMessage( request, noCookieMessageKey, SiteMessage.TYPE_ERROR );
120 } catch ( SiteMessageException e )
121 {
122 request.getSession( true ).setAttribute( DefaultGatewayResolverImpl.CONST_CAS_GATEWAY, PARAM_YES );
123 response.sendRedirect(
124 response.encodeRedirectURL( AppPathService.getSiteMessageUrl( request ) ) );
125 return;
126 }
127 }
128 if ( redirectAfterGateway && request.getParameter( ParameterGatewayResolver.PARAM_GATEWAY ) != null )
129 {
130 String url = constructServiceURL(request);
131 request.getSession( true ).setAttribute( DefaultGatewayResolverImpl.CONST_CAS_GATEWAY, PARAM_YES );
132 response.sendRedirect( response.encodeRedirectURL( url ) );
133 return;
134 }
135
136 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
137
138 if ( user == null )
139 {
140 CASAuthentication./fr/paris/lutece/plugins/mylutece/modules/cas/authentication/CASAuthentication.html#CASAuthentication">CASAuthentication casAuthentication = (CASAuthentication) SpringContextService.getBean(
141 "mylutece-cas.authentication" );
142
143 try
144 {
145 user = casAuthentication.login( "", "", request );
146 }
147 catch ( LoginException | LoginRedirectException e)
148 {
149 AppLogService.error( e.getMessage( ), e );
150 }
151
152 if ( AppLogService.isDebugEnabled( ) )
153 {
154 AppLogService.debug( "User " + user + " logged" );
155 }
156
157 SecurityService.getInstance( ).registerUser( request, user );
158 }
159
160 chain.doFilter( servletRequest, response );
161 }
162
163
164
165
166
167
168 private String constructServiceURL(HttpServletRequest request) {
169 StringBuffer url = request.getRequestURL( );
170 Enumeration<String> paramNames = request.getParameterNames( );
171 boolean firstParamater = true;
172 while ( paramNames.hasMoreElements( ) ) {
173 String param = paramNames.nextElement( );
174 if ( !param.equals( ParameterGatewayResolver.PARAM_GATEWAY ) )
175 {
176 if ( firstParamater )
177 {
178 url.append( "?" );
179 firstParamater = false;
180 } else {
181 url.append( "&" );
182 }
183 url.append( param ).append( "=" ).append( request.getParameter( param ) );
184 }
185 }
186 return url.toString( );
187 }
188
189
190
191
192
193 @Override
194 public void init( FilterConfig config ) throws ServletException
195 {
196 noCookieMessageKey = config.getInitParameter( PARAM_NOCOOKIEMESSAGEKEY );
197 String paramRedirect = config.getInitParameter( PARAM_REDIRECTAFTERGATEWWAY );
198 redirectAfterGateway = paramRedirect != null && Boolean.parseBoolean( paramRedirect );
199 }
200 }