KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > security > CombinedJNDIBasicAuthenticationModule


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.security;
25
26 import java.security.Principal JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.servlet.FilterChain JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.infoglue.cms.exception.SystemException;
36
37 /**
38  * @author Mattias Bogeblad
39  *
40  * This authentication module authenticates an user against the ordinary infoglue database.
41  */

42
43 public class CombinedJNDIBasicAuthenticationModule extends JNDIBasicAuthenticationModule
44 {
45     private final static Logger logger = Logger.getLogger(CombinedJNDIBasicAuthenticationModule.class.getName());
46     
47     
48     public static AuthenticationModule getFallbackAuthenticationModule(Object JavaDoc transactionObject, String JavaDoc successLoginUrl) throws SystemException
49     {
50         AuthenticationModule authenticationModule = null;
51         
52         try
53         {
54             String JavaDoc authorizerClass = InfoGlueAuthenticationFilter.authorizerClass;
55             String JavaDoc invalidLoginUrl = InfoGlueAuthenticationFilter.invalidLoginUrl;
56             String JavaDoc loginUrl = InfoGlueAuthenticationFilter.loginUrl;
57             String JavaDoc logoutUrl = InfoGlueAuthenticationFilter.logoutUrl;
58             String JavaDoc serverName = InfoGlueAuthenticationFilter.serverName;
59             Properties JavaDoc extraProperties = InfoGlueAuthenticationFilter.extraProperties;
60             String JavaDoc casRenew = InfoGlueAuthenticationFilter.casRenew;
61             String JavaDoc casServiceUrl = InfoGlueAuthenticationFilter.casServiceUrl;
62             String JavaDoc casValidateUrl = InfoGlueAuthenticationFilter.casValidateUrl;
63             String JavaDoc casLogoutUrl = InfoGlueAuthenticationFilter.casLogoutUrl;
64
65             authenticationModule = new InfoGlueBasicAuthenticationModule();
66             authenticationModule.setAuthenticatorClass(InfoGlueBasicAuthenticationModule.class.getName());
67             authenticationModule.setAuthorizerClass(authorizerClass);
68             authenticationModule.setInvalidLoginUrl(invalidLoginUrl);
69             authenticationModule.setLoginUrl(loginUrl);
70             authenticationModule.setLogoutUrl(logoutUrl);
71             authenticationModule.setServerName(serverName);
72             authenticationModule.setExtraProperties(extraProperties);
73             authenticationModule.setCasRenew(casRenew);
74             
75             if(successLoginUrl != null && successLoginUrl.length() > 0)
76             {
77                 int index = successLoginUrl.indexOf("&ticket=");
78                 if(index > -1)
79                 {
80                     successLoginUrl = successLoginUrl.substring(0, index);
81                 }
82                 logger.info("successLoginUrl:" + successLoginUrl);
83                 authenticationModule.setCasServiceUrl(successLoginUrl);
84                 authenticationModule.setSuccessLoginUrl(successLoginUrl);
85             }
86             else
87                 authenticationModule.setCasServiceUrl(casServiceUrl);
88             
89             authenticationModule.setCasValidateUrl(casValidateUrl);
90             authenticationModule.setCasLogoutUrl(casLogoutUrl);
91             authenticationModule.setTransactionObject(transactionObject);
92         }
93         catch(Exception JavaDoc e)
94         {
95             logger.error("An error occurred when we tried to get an authenticationModule:" + e, e);
96             throw new SystemException("An error occurred when we tried to get an authenticationModule: " + e.getMessage(), e);
97         }
98         
99         return authenticationModule;
100     }
101
102     /**
103      * This method handles all of the logic for checking how to handle a login.
104      */

105     
106     public String JavaDoc authenticateUser(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc fc) throws Exception JavaDoc
107     {
108         String JavaDoc authenticatedUserName = null;
109  
110         try
111         {
112             logger.info("authenticateUser 1");
113             request.setAttribute("disableRedirect", "true");
114             authenticatedUserName = super.authenticateUser(request, response, fc);
115             logger.info("authenticatedUserName from JNDI:" + authenticatedUserName);
116             if(authenticatedUserName == null)
117             {
118                 authenticatedUserName = getFallbackAuthenticationModule(null, null).authenticateUser(request, response, fc);
119                 logger.info("authenticatedUserName from BASIC:" + authenticatedUserName);
120             }
121         }
122         catch(Exception JavaDoc e)
123         {
124             logger.info("NO authenticatedUserName from JNDI");
125             authenticatedUserName = getFallbackAuthenticationModule(null, null).authenticateUser(request, response, fc);
126             logger.info("authenticatedUserName from BASIC:" + authenticatedUserName);
127         }
128             
129         return authenticatedUserName;
130     }
131     
132     
133     /**
134      * This method handles all of the logic for checking how to handle a login.
135      */

136     
137     public String JavaDoc authenticateUser(Map JavaDoc request) throws Exception JavaDoc
138     {
139         String JavaDoc authenticatedUserName = null;
140         try
141         {
142             logger.info("authenticateUser 2");
143             request.put("disableRedirect", "true");
144             authenticatedUserName = super.authenticateUser(request);
145             logger.info("authenticatedUserName from JNDI:" + authenticatedUserName);
146             if(authenticatedUserName == null)
147             {
148                 authenticatedUserName = getFallbackAuthenticationModule(null, null).authenticateUser(request);
149                 logger.info("authenticatedUserName from BASIC:" + authenticatedUserName);
150             }
151         }
152         catch(Exception JavaDoc e)
153         {
154             logger.info("NO authenticatedUserName from JNDI");
155             authenticatedUserName = getFallbackAuthenticationModule(null, null).authenticateUser(request);
156             logger.info("authenticatedUserName from BASIC:" + authenticatedUserName);
157         }
158         
159         return authenticatedUserName;
160     }
161     
162     
163     /**
164      * This method handles all of the logic for checking how to handle a login.
165      */

166     
167     public String JavaDoc getLoginDialogUrl(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
168     {
169         return super.getLoginDialogUrl(request, response);
170     }
171     
172     public Principal JavaDoc loginUser(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Map JavaDoc status) throws Exception JavaDoc
173     {
174         return null;
175     }
176
177     public boolean logoutUser(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
178     {
179         return false;
180     }
181         
182     
183 }
Popular Tags