KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > samples > NullAuthenticationSessionBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.samples;
15
16 import java.rmi.RemoteException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18
19 import javax.ejb.CreateException JavaDoc;
20 import javax.ejb.EJBException JavaDoc;
21 import javax.ejb.ObjectNotFoundException JavaDoc;
22
23 import org.ejbca.core.ejb.BaseSessionBean;
24 import org.ejbca.core.ejb.log.ILogSessionHome;
25 import org.ejbca.core.ejb.log.ILogSessionRemote;
26 import org.ejbca.core.model.SecConst;
27 import org.ejbca.core.model.ca.AuthLoginException;
28 import org.ejbca.core.model.ca.AuthStatusException;
29 import org.ejbca.core.model.log.Admin;
30 import org.ejbca.core.model.log.LogEntry;
31 import org.ejbca.core.model.ra.UserDataConstants;
32 import org.ejbca.core.model.ra.UserDataVO;
33 import org.ejbca.util.CertTools;
34
35
36
37
38 /**
39  * Approves all authentication requests that contain a DN as the username, password is ignored and
40  * the username is returned as DN. Could be useful for demo purposes to give out certificates to anyone.
41  *
42  * To install it must replace the current org.ejbca.core.model.authorization.LocalAuthorizationSessionBean
43  * which will require some work from your part.
44  *
45  * @ejb.bean
46  * display-name="AuthenticationSB"
47  * name="AuthenticationSession"
48  * jndi-name="AuthenticationSession"
49  * local-jndi-name="AuthenticationSessionLocal"
50  * view-type="both"
51  * type="Stateless"
52  * transaction-type="Container"
53  * generate="false"
54  *
55  * @ejb.transaction type="Supports"
56  *
57  * @ejb.ejb-external-ref
58  * description="The Log session bean"
59  * view-type="local"
60  * ejb-name="LogSessionLocal"
61  * type="Session"
62  * home="org.ejbca.core.ejb.log.ILogSessionLocalHome"
63  * business="org.ejbca.core.ejb.log.ILogSessionLocal"
64  * link="LogSession"
65  *
66  * @ejb.home
67  * extends="javax.ejb.EJBHome"
68  * local-extends="javax.ejb.EJBLocalHome"
69  * local-class="org.ejbca.samples.IAuthenticationSessionLocalHome"
70  * remote-class="org.ejbca.samples.IAuthenticationSessionHome"
71  * generate="none"
72  *
73  * @ejb.interface
74  * extends="javax.ejb.EJBObject"
75  * local-extends="javax.ejb.EJBLocalObject"
76  * local-class="org.ejbca.samples.IAuthenticationSessionLocal"
77  * remote-class="org.ejbca.samples.IAuthenticationSessionRemote"
78  * generate="none"
79  *
80  *
81  * @version $Id: NullAuthenticationSessionBean.java,v 1.4 2007/01/01 11:08:21 anatom Exp $
82  *
83  */

84 public class NullAuthenticationSessionBean extends BaseSessionBean {
85     /** The remote interface of the log session bean */
86     private ILogSessionRemote logsession;
87
88
89     /**
90      * Default create for SessionBean without any creation Arguments.
91      *
92      * @throws CreateException if bean instance can't be created
93      */

94     public void ejbCreate() throws CreateException JavaDoc {
95         debug(">ejbCreate()");
96
97         try {
98             ILogSessionHome logsessionhome = (ILogSessionHome) getLocator().getLocalHome(ILogSessionHome.COMP_NAME);
99             logsession = logsessionhome.create();
100         } catch (Exception JavaDoc e) {
101             throw new EJBException JavaDoc(e);
102         }
103
104         debug("<ejbCreate()");
105     }
106
107     /**
108      * Implements IAuthenticationSession::authenticateUser. Implements a mechanism that does no
109      * real authentication. Returns the username as DN is the username contains a DN. Only returns
110      * entities of type USER_ENDUSER. STATUS_NEW, STATUS_FAILED or STATUS_INPROCESS.
111      *
112      * @param admin administrator performing this task
113      * @param username username to be authenticated
114      * @param password password for user to be authenticated
115      *
116      * @return UserData for authenticated user
117      */

118     public UserDataVO authenticateUser(Admin admin, String JavaDoc username, String JavaDoc password)
119         throws ObjectNotFoundException JavaDoc, AuthStatusException, AuthLoginException {
120         debug(">authenticateUser(" + username + ", hiddenpwd)");
121
122         try {
123             // Does the username contain a DN?
124
String JavaDoc dn = CertTools.stringToBCDNString(username);
125
126             if ((dn != null) && (dn.length() > 0)) {
127                 String JavaDoc email = null;
128                 ArrayList JavaDoc emails = CertTools.getEmailFromDN(dn);
129                 if (emails.size() > 0) {
130                     email = (String JavaDoc)emails.get(0);
131                 }
132                 try{
133                   logsession.log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_INFO_USERAUTHENTICATION,"NULL-Authenticated user");
134                 }catch(RemoteException JavaDoc re){
135                   throw new EJBException JavaDoc(re);
136                 }
137
138                 String JavaDoc altName = (email == null) ? null : ("rfc822Name=" + email);
139
140                 // Use default certificate profile 0
141
UserDataVO ret = new UserDataVO(username, dn, admin.getCaId(), altName, email, UserDataConstants.STATUS_NEW, SecConst.USER_ENDUSER, SecConst.PROFILE_NO_PROFILE, SecConst.PROFILE_NO_PROFILE,
142                                                 null, null, SecConst.TOKEN_SOFT_BROWSERGEN,0,null);
143                 ret.setPassword(password);
144                 debug("<authenticateUser("+username+", hiddenpwd)");
145                 return ret;
146             }
147             try{
148               logsession.log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date JavaDoc(),username, null, LogEntry.EVENT_ERROR_USERAUTHENTICATION,"User does not contain a DN.");
149             }catch(RemoteException JavaDoc re){
150               throw new EJBException JavaDoc(re);
151             }
152
153             throw new AuthLoginException("User " + username + " does not contain a DN.");
154         } catch (AuthLoginException le) {
155             throw le;
156         } catch (Exception JavaDoc e) {
157             throw new EJBException JavaDoc(e.toString());
158         }
159     } //authenticateUser
160

161     /**
162      * Implements IAuthenticationSession::finishUser. Does nothing...
163      *
164      * @param admin administrator performing this task
165      * @param username username to be finished
166      * @param password password for user to be finished
167      */

168     public void finishUser(Admin admin, String JavaDoc username, String JavaDoc password)
169         throws ObjectNotFoundException JavaDoc {
170         debug(">finishUser(" + username + ", hiddenpwd)");
171         debug("<finishUser(" + username + ", hiddenpwd)");
172     } //finishUser
173
}
174
Popular Tags