KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > params > valves > HttpBasicAuthValveImpl


1 /*
2  * Copyright 2002-2006 Jahia Ltd
3  *
4  * Licensed under the JAHIA COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (JCDDL),
5  * Version 1.0 (the "License"), or (at your option) any later version; you may
6  * not use this file except in compliance with the License. You should have
7  * received a copy of the License along with this program; if not, you may obtain
8  * a copy of the License at
9  *
10  * http://www.jahia.org/license/
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
package org.jahia.params.valves;
18
19 import org.jahia.pipelines.valves.Valve;
20 import org.jahia.pipelines.valves.ValveContext;
21 import org.jahia.pipelines.PipelineException;
22 import org.jahia.services.sites.JahiaSite;
23 import org.jahia.services.usermanager.JahiaUser;
24 import org.jahia.services.webdav.JahiaUserWrapper;
25 import org.jahia.params.ParamBean;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.exceptions.JahiaException;
28 import sun.misc.BASE64Decoder;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /**
34  * Created by IntelliJ IDEA.
35  * User: toto
36  * Date: 15 dŽc. 2004
37  * Time: 13:03:08
38  * To change this template use File | Settings | File Templates.
39  */

40 public class HttpBasicAuthValveImpl implements Valve {
41     public HttpBasicAuthValveImpl() {
42     }
43
44     public void invoke(Object JavaDoc context, ValveContext valveContext) throws PipelineException {
45         ParamBean processingContext = (ParamBean) context;
46         HttpServletRequest JavaDoc request = ((ParamBean)processingContext).getRequest();
47         String JavaDoc auth = request.getHeader("Authorization");
48         if (auth != null) {
49             try {
50                 auth = auth.substring(6).trim();
51                 BASE64Decoder decoder = new BASE64Decoder();
52                 String JavaDoc cred = new String JavaDoc(decoder.decodeBuffer(auth));
53                 int colonInd = cred.indexOf(':');
54                 String JavaDoc user = cred.substring(0,colonInd);
55                 String JavaDoc pass = cred.substring(colonInd+1);
56
57                 JahiaSite site = (JahiaSite) request.getSession().getAttribute(ParamBean.SESSION_SITE);
58                 JahiaUser jahiaUser = null;
59                     jahiaUser = ServicesRegistry.getInstance().getJahiaSiteUserManagerService().getMember(site.getID(), user);
60                     if (jahiaUser != null) {
61                         if (jahiaUser.verifyPassword(pass)) {
62                             processingContext.setTheUser(jahiaUser);
63                             return;
64                         }
65                     }
66             } catch (Exception JavaDoc e) {
67             }
68         }
69         valveContext.invokeNext(context);
70     }
71
72     public void initialize() {
73     }
74 }
75
Popular Tags