001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * François Maturel 016 */ 017 018package org.nuxeo.ecm.platform.ui.web.keycloak; 019 020import org.apache.catalina.connector.Request; 021import org.apache.catalina.connector.RequestFacade; 022import org.keycloak.adapters.AdapterDeploymentContext; 023import org.keycloak.adapters.KeycloakDeployment; 024import org.keycloak.adapters.tomcat.CatalinaHttpFacade; 025 026import javax.servlet.http.HttpServletRequest; 027import javax.servlet.http.HttpServletResponse; 028import java.lang.reflect.Field; 029 030/** 031 * 032 * @since 7.4 033 */ 034 035public class DeploymentResult { 036 private boolean isOk; 037 038 private static KeycloakDeployment keycloakDeployment; 039 040 private HttpServletRequest httpServletRequest; 041 private HttpServletResponse httpServletResponse; 042 private Request request; 043 private CatalinaHttpFacade facade; 044 045 public DeploymentResult(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { 046 this.httpServletRequest = httpServletRequest; 047 this.httpServletResponse = httpServletResponse; 048 } 049 050 boolean isOk() { 051 return isOk; 052 } 053 054 public static KeycloakDeployment getKeycloakDeployment() { 055 return keycloakDeployment; 056 } 057 058 public Request getRequest() { 059 return request; 060 } 061 062 public CatalinaHttpFacade getFacade() { 063 return facade; 064 } 065 066 public DeploymentResult invokeOn(AdapterDeploymentContext deploymentContext) { 067 068 // In Tomcat, a HttpServletRequest and a HttpServletResponse are wrapped in a Facades 069 request = unwrapRequest((RequestFacade) httpServletRequest); 070 facade = new CatalinaHttpFacade(request, httpServletResponse); 071 072 if (keycloakDeployment == null) { 073 keycloakDeployment = deploymentContext.resolveDeployment(facade); 074 } 075 if (keycloakDeployment.isConfigured()) { 076 isOk = true; 077 return this; 078 } 079 isOk = false; 080 return this; 081 } 082 083 /** 084 * Get the wrapper {@link Request} hidden in a {@link RequestFacade} object 085 * 086 * @param requestFacade, the main RequestFacade object 087 * @return the wrapper {@link Request} in {@link RequestFacade} 088 */ 089 private Request unwrapRequest(RequestFacade requestFacade) { 090 try { 091 Field f = requestFacade.getClass().getDeclaredField("request"); 092 f.setAccessible(true); // grant access to (protected) field 093 return (Request) f.get(requestFacade); 094 } catch (NoSuchFieldException | IllegalAccessException e) { 095 throw new RuntimeException(e); 096 } 097 } 098}