| | |
| | | import org.keycloak.services.managers.ClientSessionCode; |
| | | import org.keycloak.services.managers.ResourceAdminManager; |
| | | import org.keycloak.sessions.AuthenticationSessionModel; |
| | | import org.keycloak.sessions.CommonClientSessionModel; |
| | | |
| | | import javax.ws.rs.core.HttpHeaders; |
| | | import javax.ws.rs.core.Response; |
| | | import javax.ws.rs.core.UriInfo; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Method; |
| | | import java.net.URI; |
| | | |
| | | public class CASLoginProtocol implements LoginProtocol { |
| | |
| | | public static final String LOGIN_PROTOCOL = "cas"; |
| | | |
| | | public static final String SERVICE_PARAM = "service"; |
| | | public static final String TARGET_PARAM = "TARGET"; |
| | | public static final String RENEW_PARAM = "renew"; |
| | | public static final String GATEWAY_PARAM = "gateway"; |
| | | public static final String TICKET_PARAM = "ticket"; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Response authenticated(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) { |
| | | public Response authenticated(UserSessionModel userSession, ClientSessionContext clientSessionCtx) { |
| | | AuthenticatedClientSessionModel clientSession = clientSessionCtx.getClientSession(); |
| | | ClientSessionCode<AuthenticatedClientSessionModel> accessCode = new ClientSessionCode<>(session, realm, clientSession); |
| | | |
| | | String service = clientSession.getRedirectUri(); |
| | | //TODO validate service |
| | | |
| | | String code; |
| | | try { |
| | | // Keycloak >3.4 branch: Method getCode was renamed to getOrGenerateCode, CODE_TO_TOKEN was removed |
| | | Method getOrGenerateCode = ClientSessionCode.class.getMethod("getOrGenerateCode"); |
| | | code = (String) getOrGenerateCode.invoke(accessCode); |
| | | } catch (ReflectiveOperationException e) { |
| | | // Keycloak <=3.3 branch |
| | | accessCode.setAction(CommonClientSessionModel.Action.CODE_TO_TOKEN.name()); |
| | | code = accessCode.getCode(); |
| | | } |
| | | String code = accessCode.getOrGenerateCode(); |
| | | KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(service); |
| | | uriBuilder.queryParam(TICKET_RESPONSE_PARAM, SERVICE_TICKET_PREFIX + code); |
| | | |
| | |
| | | |
| | | event.event(EventType.LOGOUT); |
| | | event.user(userSession.getUser()).session(userSession).success(); |
| | | LoginFormsProvider infoPage = session.getProvider(LoginFormsProvider.class).setSuccess("Logout successful"); |
| | | |
| | | if (redirectUri != null) { |
| | | infoPage.setAttribute("pageRedirectUri", redirectUri); |
| | | return Response.status(302).location(URI.create(redirectUri)).build(); |
| | | } else { |
| | | LoginFormsProvider infoPage = session.getProvider(LoginFormsProvider.class).setSuccess("Logout successful"); |
| | | infoPage.setAttribute("skipLink", true); |
| | | return infoPage.createInfoPage(); |
| | | } |
| | | return infoPage.createInfoPage(); |
| | | } |
| | | |
| | | @Override |