KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > auth > ClientcertAuthenticationHandler


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.auth;
8
9 import java.io.IOException JavaDoc;
10 import java.security.cert.X509Certificate JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18 import org.w3c.dom.Node JavaDoc;
19
20 import winstone.AuthenticationPrincipal;
21 import winstone.AuthenticationRealm;
22 import winstone.Logger;
23 import winstone.WinstoneRequest;
24
25 /**
26  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
27  * @version $Id: ClientcertAuthenticationHandler.java,v 1.3 2006/02/28 07:32:47 rickknowles Exp $
28  */

29 public class ClientcertAuthenticationHandler extends BaseAuthenticationHandler {
30     public ClientcertAuthenticationHandler(Node JavaDoc loginConfigNode,
31             List JavaDoc constraintNodes, Set JavaDoc rolesAllowed,
32             AuthenticationRealm realm) {
33         super(loginConfigNode, constraintNodes, rolesAllowed, realm);
34         Logger.log(Logger.DEBUG, AUTH_RESOURCES,
35                 "ClientcertAuthenticationHandler.Initialised", realmName);
36     }
37
38     /**
39      * Call this once we know that we need to authenticate
40      */

41     protected void requestAuthentication(HttpServletRequest JavaDoc request,
42             HttpServletResponse JavaDoc response, String JavaDoc pathRequested)
43             throws IOException JavaDoc {
44         // Return unauthorized, and set the realm name
45
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
46                 AUTH_RESOURCES.getString("ClientcertAuthenticationHandler.UnauthorizedMessage"));
47     }
48
49     /**
50      * Handling the (possible) response
51      */

52     protected boolean validatePossibleAuthenticationResponse(
53             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
54             String JavaDoc pathRequested) throws IOException JavaDoc {
55         // Check for certificates in the request attributes
56
X509Certificate JavaDoc certificateArray[] = (X509Certificate JavaDoc[]) request
57                 .getAttribute("javax.servlet.request.X509Certificate");
58         if ((certificateArray != null) && (certificateArray.length > 0)) {
59             boolean failed = false;
60             for (int n = 0; n < certificateArray.length; n++)
61                 try {
62                     certificateArray[n].checkValidity();
63                 } catch (Throwable JavaDoc err) {
64                     failed = true;
65                 }
66             if (!failed) {
67                 AuthenticationPrincipal principal = this.realm
68                         .retrieveUser(certificateArray[0].getSubjectDN()
69                                 .getName());
70                 if (principal != null) {
71                     principal.setAuthType(HttpServletRequest.CLIENT_CERT_AUTH);
72                     if (request instanceof WinstoneRequest)
73                         ((WinstoneRequest) request).setRemoteUser(principal);
74                     else if (request instanceof HttpServletRequestWrapper JavaDoc) {
75                         HttpServletRequestWrapper JavaDoc wrapper = (HttpServletRequestWrapper JavaDoc) request;
76                         if (wrapper.getRequest() instanceof WinstoneRequest)
77                             ((WinstoneRequest) wrapper.getRequest())
78                                     .setRemoteUser(principal);
79                         else
80                             Logger.log(Logger.WARNING, AUTH_RESOURCES,
81                                     "ClientCertAuthenticationHandler.CantSetUser",
82                                             wrapper.getRequest().getClass().getName());
83                     } else
84                         Logger.log(Logger.WARNING, AUTH_RESOURCES,
85                                 "ClientCertAuthenticationHandler.CantSetUser",
86                                 request.getClass().getName());
87                 }
88             }
89         }
90         return true;
91     }
92 }
93
Popular Tags