KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sso > CasService


1 package org.jahia.services.sso;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Properties JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10
11 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
19
20 import edu.yale.its.tp.cas.client.CASAuthenticationException;
21 import edu.yale.its.tp.cas.client.ServiceTicketValidator;
22
23 /**
24  * <p>Title: CAS service</p>
25  * <p>Description: a CAS service to validate CAS tickets.</p>
26  * <p>Copyright: Copyright (c) 2005 - Pascal Aubry</p>
27  * <p>Company: University of Rennes 1</p>
28  * @author Pascal Aubry
29  * @version 1.0
30  */

31
32 public class CasService extends JahiaInitializableService {
33
34     /** a logger for this class. */
35     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (CasService.class);
36
37     /** a singleton to use a single instance. */
38     private static CasService m_Instance;
39
40     /** the name of the configuration file. */
41     private static String JavaDoc CONFIGURATION_FILE = "cas.properties";
42
43     /** the property that gives the URL to use to validate tickets. */
44     private static String JavaDoc SERVER_VALIDATE_URL_PROP = "cas.server.validateUrl";
45     /** the property that gives the service URL of the application. */
46     private static String JavaDoc JAHIA_SERVICE_URL_PROP = "cas.jahia.serviceUrl";
47     /** the property that gives the login URL of the CAS server. */
48     private static String JavaDoc SERVER_LOGIN_URL_PROP = "cas.server.loginUrl";
49
50     /** CAS properties. */
51     private Properties JavaDoc casProperties = null;
52
53     /**
54      * return the singleton instance.
55      * @return a CasService instance.
56      */

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     /** The name of the configuration file. */
67     private String JavaDoc configFileName;
68     
69     /**
70      * @see org.jahia.services.JahiaInitializableService#init(org.jahia.settings.SettingsBean)
71      */

72     public void init(SettingsBean jSettings) throws JahiaInitializationException {
73         configFileName = jSettings.getJahiaCasDiskPath() + File.separator + CONFIGURATION_FILE;
74         
75         File JavaDoc configFile = new File JavaDoc (configFileName);
76         if (configFile.exists()) {
77             try {
78                 File JavaDoc casPropFile = new File JavaDoc (configFileName);
79                 FileInputStream JavaDoc casPropInputStr = new FileInputStream JavaDoc (casPropFile);
80                 casProperties = new Properties JavaDoc ();
81                 casProperties.load (casPropInputStr);
82                 casPropInputStr.close ();
83             } catch (FileNotFoundException JavaDoc fnfe) {
84                 logger.error(fnfe);
85                 throw new JahiaInitializationException(fnfe.getMessage(), fnfe);
86             } catch (IOException JavaDoc 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     /** constructor. */
96     private CasService() {
97         super();
98     }
99
100     /**
101      * Return a property (throw an exception when not set).
102      * @param propName the name of the property
103      * @return a String.
104      * @throws JahiaInitializationException
105      */

106     private String JavaDoc getCasProperty(String JavaDoc propName) throws JahiaInitializationException {
107         if (casProperties == null) {
108             throw new JahiaInitializationException("no CAS property found, please check that '" + configFileName + "' exists!");
109         }
110         String JavaDoc 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     /**
118      * @return the URL to use to validate tickets.
119      * @throws JahiaInitializationException
120      */

121     public String JavaDoc getServerValidateUrl() throws JahiaInitializationException{
122         return getCasProperty(SERVER_VALIDATE_URL_PROP);
123     }
124     
125     /**
126      * @return the service URL of the application.
127      * @throws java.net.MalformedURLException
128      * @throws org.jahia.exceptions.JahiaException
129      */

130     public String JavaDoc getJahiaServiceUrl(ParamBean paramBean) throws MalformedURLException JavaDoc, JahiaException{
131
132         int pid;
133         pid = paramBean.getPageID();
134
135         URL JavaDoc url = new URL JavaDoc(paramBean.getSiteURL(pid,false,false));
136
137         int port = url.getPort();
138         String JavaDoc 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     /**
149      * @return the login URL of the CAS server.
150      * @throws JahiaInitializationException
151      */

152     public String JavaDoc getServerLoginUrl() throws JahiaInitializationException{
153         return getCasProperty(SERVER_LOGIN_URL_PROP);
154     }
155
156     /**
157      * Validate a CAS ticket.
158      * @param ticket the ticket
159      * @throws ParserConfigurationException
160      * @throws SAXException
161      * @throws IOException
162      * @throws CASAuthenticationException
163      * @throws JahiaInitializationException
164      * @return the id of the authenticated user, or null if none.
165      */

166     public String JavaDoc validateTicket(String JavaDoc ticket, ParamBean jParams)
167     throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc, CASAuthenticationException, JahiaInitializationException, JahiaException {
168
169         String JavaDoc validateUrl = getServerValidateUrl();
170         String JavaDoc serviceUrl = getJahiaServiceUrl(jParams);
171         logger.debug(SERVER_VALIDATE_URL_PROP + " = " + validateUrl);
172         logger.debug(JAHIA_SERVICE_URL_PROP + " = " + serviceUrl);
173
174         // create a new ticket validator
175
ServiceTicketValidator sv = new ServiceTicketValidator();
176
177         // set the URL used to validate the CAS ticket
178
sv.setCasValidateUrl(validateUrl);
179         // set the service the browser will be redirected after authentication
180
sv.setService(serviceUrl);
181         // set the ticket to validate
182
sv.setServiceTicket(ticket);
183
184         // validate the ticket
185
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