50
50
import org .springframework .core .env .Environment ;
51
51
import org .springframework .web .context .WebApplicationContext ;
52
52
import org .springframework .web .context .support .WebApplicationContextUtils ;
53
+ import org .springframework .webflow .context .ExternalContextHolder ;
54
+ import org .springframework .webflow .context .servlet .ServletExternalContext ;
55
+ import org .springframework .webflow .engine .impl .FlowExecutionImpl ;
56
+ import org .springframework .webflow .execution .FlowExecutionFactory ;
57
+ import org .springframework .webflow .execution .FlowExecutionKey ;
58
+ import org .springframework .webflow .execution .repository .FlowExecutionRepository ;
59
+ import org .springframework .webflow .executor .FlowExecutionResult ;
60
+ import org .springframework .webflow .executor .FlowExecutorImpl ;
53
61
54
62
import net .shibboleth .idp .attribute .IdPAttribute ;
55
63
import net .shibboleth .idp .authn .AuthnEventIds ;
@@ -115,7 +123,12 @@ public void init(final ServletConfig config) throws ServletException {
115
123
116
124
@ Override
117
125
protected void doGet (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException {
118
- try {
126
+ if (!checkRequest (request , response )) {
127
+ return ;
128
+ }
129
+
130
+ try {
131
+ ExternalContextHolder .setExternalContext (new ServletExternalContext (request .getServletContext (), request , response ));
119
132
120
133
final String requestUrl = request .getRequestURL ().toString ();
121
134
LOG .trace ("Get request to: '{}'" , requestUrl );
@@ -157,15 +170,15 @@ public String getParameter(String name) {
157
170
}
158
171
159
172
// Get authentication key from request
160
- final String authenticationKey = ExternalAuthentication .startExternalAuthentication (externalRequest );
173
+ final String flowExecutionKey = ExternalAuthentication .startExternalAuthentication (externalRequest );
161
174
162
175
// Get external authentication properties
163
176
final boolean force = Boolean .parseBoolean (request .getAttribute (ExternalAuthentication .FORCE_AUTHN_PARAM ).toString ());
164
177
165
178
// It's an authentication
166
179
if (!authorizationResponse ) {
167
180
LOG .debug ("Initiating oxAuth login redirect" );
168
- startLoginRequest (request , response , authenticationKey , force );
181
+ startLoginRequest (request , response , flowExecutionKey , force );
169
182
return ;
170
183
}
171
184
@@ -176,22 +189,35 @@ public String getParameter(String name) {
176
189
LOG .error ("The state in session and in request are not equals" );
177
190
178
191
// Re-init login page
179
- startLoginRequest (request , response , authenticationKey , force );
192
+ startLoginRequest (request , response , flowExecutionKey , force );
180
193
return ;
181
194
}
182
195
183
- processAuthorizationResponse (request , response , authenticationKey );
196
+ processAuthorizationResponse (request , response , flowExecutionKey );
184
197
185
198
} catch (final ExternalAuthenticationException ex ) {
186
- LOG .warn ("Error processing oxAuth authentication request" , ex );
199
+ LOG .error ("Error processing oxAuth authentication request" , ex );
187
200
loadErrorPage (request , response );
188
-
189
201
} catch (final Exception ex ) {
190
202
LOG .error ("Something unexpected happened" , ex );
191
203
request .setAttribute (ExternalAuthentication .AUTHENTICATION_ERROR_KEY , AuthnEventIds .AUTHN_EXCEPTION );
204
+ } finally {
205
+ ExternalContextHolder .setExternalContext (null );
192
206
}
193
207
}
194
208
209
+ private final boolean checkRequest (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException {
210
+ // Check whether a session is required.
211
+ if (request .getSession (false ) == null ) {
212
+ LOG .error ("Pre-existing session required but none found" );
213
+ loadErrorPage (request , response );
214
+
215
+ return false ;
216
+ }
217
+
218
+ return true ;
219
+ }
220
+
195
221
private void processAuthorizationResponse (final HttpServletRequest request , final HttpServletResponse response , final String authenticationKey )
196
222
throws ExternalAuthenticationException , IOException {
197
223
try {
@@ -242,14 +268,14 @@ private void processAuthorizationResponse(final HttpServletRequest request, fina
242
268
if (!idpAttributes .isEmpty ()) {
243
269
LOG .debug ("Storing generated idp attributes" );
244
270
ProfileRequestContext prContext = ExternalAuthentication .getProfileRequestContext (authenticationKey , request );
245
- GluuScratchContext gluuScratchContext = prContext .getSubcontext (GluuScratchContext .class ,true );
271
+ GluuScratchContext gluuScratchContext = prContext .getSubcontext (GluuScratchContext .class , true );
246
272
gluuScratchContext .setIdpAttributes (idpAttributes );
247
273
}
248
274
249
275
LOG .debug ("Created an IdP subject instance with principals for {} " , userProfile .getId ());
250
276
final Set <Principal > userPrincipals = new HashSet <Principal >();
251
277
userPrincipals .add (new UsernamePrincipal (userProfile .getId ()));
252
- request .setAttribute (ExternalAuthentication .SUBJECT_KEY , new Subject (false , userPrincipals ,Collections .emptySet (),Collections .emptySet ()));
278
+ request .setAttribute (ExternalAuthentication .SUBJECT_KEY , new Subject (false , userPrincipals , Collections .emptySet (),Collections .emptySet ()));
253
279
254
280
if (authenticationContext != null ) {
255
281
String usedAcr = userProfile .getUsedAcr ();
0 commit comments