1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jetspeed.portlet.sso;
21
22 import java.io.IOException;
23 import java.security.AccessControlContext;
24 import java.security.AccessController;
25 import javax.portlet.ActionRequest;
26 import javax.portlet.ActionResponse;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletContext;
29 import javax.portlet.PortletException;
30 import javax.portlet.PortletURL;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33 import javax.security.auth.Subject;
34
35 import org.apache.jetspeed.security.JSSubject;
36 import org.apache.jetspeed.sso.SSOException;
37 import org.apache.jetspeed.sso.SSOProvider;
38 import org.apache.portals.bridges.common.ScriptPostProcess;
39 import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
40
41 /***
42 * SSOProxyPortlet
43 * This portlet can be used as a bridge to any URL.
44 * It acts as a http client and therefore it can store
45 * cookies.
46 * The main purpose is that the SSOProxy portlet authenticates
47 * any SSO credential for the principal user not knowing in advance
48 * what URL the user might select. No login prompt will appear for any url
49 * in the portlet for that an SSO entry exists and the principal user has permissions.
50 *
51 * @author Roger Ruttimann <rogerrut@apache.org>
52 *
53 */
54 public class SSOProxyPortlet extends GenericVelocityPortlet {
55 private PortletContext context;
56 private SSOProvider sso;
57
58
59 private boolean isAuthenticated = false;
60
61 /*** Default encoding UTF-8*/
62 public String defaultEncoding = "UTF-8";
63
64 /*** Block Size */
65 static final int BLOCK_SIZE = 4096;
66
67 /*** ACTION_PARAMETER_SSOPROXY*/
68 static final String ACTION_PARAMETER_SSOPROXY = "SSOProxy";
69
70 /*** Preference values */
71 /*** DestinationURL */
72 static final String DESTINATION_URL = "DestinationURL";
73
74 /*** SSOSite */
75 static final String SSO_SITE = "SSOSite";
76
77 /*** ForceSSORefresh*/
78 static final String FORCE_SSO_REFRESH = "ForceSSORefresh";
79
80 /*** Encoding*/
81 static final String ENCODING = "Encoding";
82
83 private String destinationURL;
84 private String ssoSite;
85 private String encoding;
86
87 public void init(PortletConfig config) throws PortletException
88 {
89 super.init(config);
90 context = getPortletContext();
91 sso = (SSOProvider)context.getAttribute("cps:SSO");
92 if (null == sso)
93 {
94 throw new PortletException("Failed to find SSO Provider on portlet initialization");
95 }
96
97 }
98
99 public void processAction(ActionRequest request, ActionResponse actionResponse)
100 throws PortletException, IOException
101 {
102 String ssoProxyAction = request.getParameter(ACTION_PARAMETER_SSOPROXY);
103
104
105 if ( ssoProxyAction != null && ssoProxyAction.length() > 0)
106 this.destinationURL = ssoProxyAction;
107 else
108 this.destinationURL = request.getParameter(DESTINATION_URL);
109
110
111 this.ssoSite = request.getParameter(SSO_SITE);
112 this.encoding = request.getParameter(ENCODING);
113 if (this.encoding == null)
114 this.encoding = this.defaultEncoding;
115
116
117 super.processAction(request, actionResponse);
118 }
119
120 public void doView(RenderRequest request, RenderResponse response)
121 throws PortletException, IOException
122 {
123 String forceRefresh = request.getPreferences().getValue(FORCE_SSO_REFRESH, "false");
124
125 if (destinationURL == null || destinationURL.length() == 0)
126 {
127
128 request.setAttribute(PARAM_VIEW_PAGE, this.getPortletConfig().getInitParameter(PARAM_EDIT_PAGE));
129 setupPreferencesEdit(request, response);
130 super.doView(request, response);
131 return;
132 }
133
134
135 response.setContentType("text/html");
136
137
138
139
140 boolean doRefresh = false;
141 if ( (forceRefresh.compareToIgnoreCase("TRUE") == 0) || this.isAuthenticated == false)
142 doRefresh = true;
143
144 try
145 {
146 StringBuffer page= new StringBuffer();
147 Subject subject = getSubject();
148 if (ssoSite == null || ssoSite.length() ==0)
149 page.append(sso.useSSO(subject, destinationURL,doRefresh));
150 else
151 page.append(sso.useSSO(subject, destinationURL,ssoSite, doRefresh));
152
153
154 this.isAuthenticated = true;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 PortletURL actionURL = response.createActionURL();
181 ScriptPostProcess processor = new ScriptPostProcess();
182 processor.setInitalPage(page);
183 processor.postProcessPage(actionURL, ACTION_PARAMETER_SSOPROXY);
184 String finalPage = processor.getFinalizedPage();
185
186
187 response.getWriter().println(finalPage);
188
189 }
190 catch (SSOException e)
191 {
192 response.getWriter().println("<P>Error rendering page. Error message<BR>" + e.getMessage() + "</P>");
193
194 this.destinationURL ="";
195 }
196 }
197
198
199 public void doEdit(RenderRequest request, RenderResponse response)
200 throws PortletException, IOException
201 {
202 super.doEdit(request, response);
203 }
204
205
206
207
208 private Subject getSubject()
209 {
210 AccessControlContext context = AccessController.getContext();
211 return JSSubject.getSubject(context);
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 }