1616
1717package com .quest .keycloak .broker .wsfed ;
1818
19- import static org .keycloak .models .ClientSessionModel .Action .AUTHENTICATE ;
2019
2120import java .io .ByteArrayInputStream ;
2221import java .io .IOException ;
6867import org .keycloak .services .managers .AuthenticationManager ;
6968import org .keycloak .services .managers .ClientSessionCode ;
7069import org .keycloak .services .messages .Messages ;
70+ import org .keycloak .sessions .AuthenticationSessionModel ;
71+ import org .keycloak .sessions .CommonClientSessionModel ;
7172import org .picketlink .identity .federation .core .wstrust .wrappers .Lifetime ;
7273import org .picketlink .identity .federation .core .wstrust .wrappers .RequestSecurityTokenResponse ;
7374import org .picketlink .identity .federation .core .wstrust .wrappers .RequestSecurityTokenResponseCollection ;
@@ -161,7 +162,7 @@ protected Response execute(String wsfedAction, String wsfedResult, String contex
161162 if (wsfedAction .compareTo (WSFedConstants .WSFED_SIGNOUT_CLEANUP_ACTION ) == 0 )
162163 return handleSignoutResponse (context );
163164
164- return ErrorPage .error (session , Messages .INVALID_REQUEST );
165+ return ErrorPage .error (session , null , Messages .INVALID_REQUEST );
165166 }
166167
167168 protected Response handleSignoutRequest (String context ) {
@@ -171,7 +172,7 @@ protected Response handleSignoutRequest(String context) {
171172 logger .error ("no valid user session" );
172173 event .event (EventType .LOGOUT );
173174 event .error (Errors .USER_SESSION_NOT_FOUND );
174- return ErrorPage .error (session , Messages .IDENTITY_PROVIDER_UNEXPECTED_ERROR );
175+ return ErrorPage .error (session , null , Messages .IDENTITY_PROVIDER_UNEXPECTED_ERROR );
175176 }
176177
177178 List <UserSessionModel > userSessions = session .sessions ().getUserSessionByBrokerUserId (realm , result .getSession ().getBrokerUserId ());
@@ -205,7 +206,7 @@ protected Response handleSignoutResponse(String context) {
205206 logger .error ("no valid user session" );
206207 event .event (EventType .LOGOUT );
207208 event .error (Errors .USER_SESSION_NOT_FOUND );
208- return ErrorPage .error (session , Messages .IDENTITY_PROVIDER_UNEXPECTED_ERROR );
209+ return ErrorPage .error (session , null , Messages .IDENTITY_PROVIDER_UNEXPECTED_ERROR );
209210 }
210211
211212 UserSessionModel userSession = result .getSession ();
@@ -214,7 +215,7 @@ protected Response handleSignoutResponse(String context) {
214215 logger .error ("usersession in different state" );
215216 event .event (EventType .LOGOUT );
216217 event .error (Errors .USER_SESSION_NOT_FOUND );
217- return ErrorPage .error (session , Messages .SESSION_NOT_ACTIVE );
218+ return ErrorPage .error (session , null , Messages .SESSION_NOT_ACTIVE );
218219 }
219220
220221 return AuthenticationManager .finishBrowserLogout (session , realm , userSession , uriInfo , clientConnection , headers );
@@ -244,11 +245,12 @@ protected Response handleLoginResponse(String wsfedResponse, RequestedToken toke
244245 Map <String , String > map = getContextParameters (decodedContext );
245246 String redirectUri = URLDecoder .decode (map .get ("redirectUri" ), StandardCharsets .UTF_8 .name ());
246247 if (decodedContext .contains ("&code=" )) {
247- ClientSessionCode clientCode = ClientSessionCode .parse (map .get ("code" ), this .session , this .session .getContext ().getRealm ());
248- if (clientCode != null && clientCode .isValid (AUTHENTICATE .name (), ClientSessionCode .ActionType .LOGIN )) {
248+ //TODO not sure that we indeed have a AuthenticationSessionModel here. It could potentially be a AuthenticatedClientSessionModel
249+ ClientSessionCode .ParseResult <AuthenticationSessionModel > clientCode = ClientSessionCode .parseResult (map .get ("code" ), this .session , this .session .getContext ().getRealm (), event , AuthenticationSessionModel .class );
250+ if (clientCode != null && clientCode .getCode ().isValid (CommonClientSessionModel .Action .AUTHENTICATE .name (), ClientSessionCode .ActionType .LOGIN )) {
249251 String ACTIVE_CODE = "active_code" ; // duplicating because ClientSessionCode.ACTIVE_CODE is private
250252 // restore ACTIVE_CODE note because it must have been removed by parse() if code==activeCode
251- clientCode .getClientSession ().setNote (ACTIVE_CODE , map .get ("code" ));
253+ clientCode .getClientSession ().setClientNote (ACTIVE_CODE , map .get ("code" ));
252254
253255 // set authorization code and redirectUri
254256 identity .setCode (map .get ("code" ));
@@ -314,7 +316,7 @@ protected Response handleWsFedResponse(String wsfedResponse, String context) {
314316 if (hasExpired (rstr )) {
315317 event .event (EventType .IDENTITY_PROVIDER_RESPONSE );
316318 event .error (Errors .EXPIRED_CODE );
317- return ErrorPage .error (session , Messages .INVALID_FEDERATED_IDENTITY_ACTION );
319+ return ErrorPage .error (session , null , Messages .INVALID_FEDERATED_IDENTITY_ACTION );
318320 }
319321
320322 //TODO: Do we need to handle if the IDP sent back more than one token?
@@ -348,7 +350,7 @@ else if (rstr.getTokenType().compareTo(URI.create("urn:ietf:params:oauth:token-t
348350 logger .error ("assertion parsing failed" , e );
349351 event .event (EventType .IDENTITY_PROVIDER_RESPONSE );
350352 event .error (Errors .INVALID_SAML_RESPONSE );
351- return ErrorPage .error (session , Messages .INVALID_FEDERATED_IDENTITY_ACTION );
353+ return ErrorPage .error (session , null , Messages .INVALID_FEDERATED_IDENTITY_ACTION );
352354 }
353355 }
354356
0 commit comments