1 package org.jahia.services.sso; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 import java.util.Properties ; 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 11 import javax.xml.parsers.ParserConfigurationException ; 12 13 import org.jahia.exceptions.JahiaInitializationException; 14 import org.jahia.exceptions.JahiaException; 15 import org.jahia.services.JahiaInitializableService; 16 import org.jahia.settings.SettingsBean; 17 import org.jahia.params.ParamBean; 18 import org.xml.sax.SAXException ; 19 20 import edu.yale.its.tp.cas.client.CASAuthenticationException; 21 import edu.yale.its.tp.cas.client.ServiceTicketValidator; 22 23 31 32 public class CasService extends JahiaInitializableService { 33 34 35 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (CasService.class); 36 37 38 private static CasService m_Instance; 39 40 41 private static String CONFIGURATION_FILE = "cas.properties"; 42 43 44 private static String SERVER_VALIDATE_URL_PROP = "cas.server.validateUrl"; 45 46 private static String JAHIA_SERVICE_URL_PROP = "cas.jahia.serviceUrl"; 47 48 private static String SERVER_LOGIN_URL_PROP = "cas.server.loginUrl"; 49 50 51 private Properties casProperties = null; 52 53 57 public static synchronized CasService getInstance () { 58 59 if (m_Instance == null) { 60 m_Instance = new CasService (); 61 } 62 63 return m_Instance; 64 } 65 66 67 private String configFileName; 68 69 72 public void init(SettingsBean jSettings) throws JahiaInitializationException { 73 configFileName = jSettings.getJahiaCasDiskPath() + File.separator + CONFIGURATION_FILE; 74 75 File configFile = new File (configFileName); 76 if (configFile.exists()) { 77 try { 78 File casPropFile = new File (configFileName); 79 FileInputStream casPropInputStr = new FileInputStream (casPropFile); 80 casProperties = new Properties (); 81 casProperties.load (casPropInputStr); 82 casPropInputStr.close (); 83 } catch (FileNotFoundException fnfe) { 84 logger.error(fnfe); 85 throw new JahiaInitializationException(fnfe.getMessage(), fnfe); 86 } catch (IOException ioe) { 87 logger.error(ioe); 88 throw new JahiaInitializationException(ioe.getMessage(), ioe); 89 } 90 } else { 91 logger.error("Config file '" + configFileName + "' not found!"); 92 } 93 } 94 95 96 private CasService() { 97 super(); 98 } 99 100 106 private String getCasProperty(String propName) throws JahiaInitializationException { 107 if (casProperties == null) { 108 throw new JahiaInitializationException("no CAS property found, please check that '" + configFileName + "' exists!"); 109 } 110 String prop = casProperties.getProperty(propName); 111 if (prop == null || "".equals(prop)) { 112 throw new JahiaInitializationException("Property '" + propName + "' is not set!"); 113 } 114 return prop; 115 } 116 117 121 public String getServerValidateUrl() throws JahiaInitializationException{ 122 return getCasProperty(SERVER_VALIDATE_URL_PROP); 123 } 124 125 130 public String getJahiaServiceUrl(ParamBean paramBean) throws MalformedURLException , JahiaException{ 131 132 int pid; 133 pid = paramBean.getPageID(); 134 135 URL url = new URL (paramBean.getSiteURL(pid,false,false)); 136 137 int port = url.getPort(); 138 String serviceUrl; 139 if (port == -1) { 140 serviceUrl = "http://" + url.getHost() ; 141 } 142 else { 143 serviceUrl = "http://" + url.getHost()+ ":" + port; 144 } 145 return(serviceUrl + paramBean.composePageUrl(pid,null)); 146 } 147 148 152 public String getServerLoginUrl() throws JahiaInitializationException{ 153 return getCasProperty(SERVER_LOGIN_URL_PROP); 154 } 155 156 166 public String validateTicket(String ticket, ParamBean jParams) 167 throws IOException , SAXException , ParserConfigurationException , CASAuthenticationException, JahiaInitializationException, JahiaException { 168 169 String validateUrl = getServerValidateUrl(); 170 String serviceUrl = getJahiaServiceUrl(jParams); 171 logger.debug(SERVER_VALIDATE_URL_PROP + " = " + validateUrl); 172 logger.debug(JAHIA_SERVICE_URL_PROP + " = " + serviceUrl); 173 174 ServiceTicketValidator sv = new ServiceTicketValidator(); 176 177 sv.setCasValidateUrl(validateUrl); 179 sv.setService(serviceUrl); 181 sv.setServiceTicket(ticket); 183 184 sv.validate(); 186 187 if (!sv.isAuthenticationSuccesful()) { 188 throw new CASAuthenticationException("error #" + sv.getErrorCode() + " while validating ticket '" + ticket + "': " + sv.getErrorMessage()); 189 } 190 191 jParams.getRequest().getSession(true).setAttribute("cas.pgtiou",sv.getPgtIou()); 192 193 return sv.getUser(); 194 195 } 196 197 } 198 | Popular Tags |