1 13 14 package org.ejbca.samples; 15 16 import java.io.BufferedReader ; 17 import java.io.IOException ; 18 import java.io.InputStreamReader ; 19 import java.io.PrintWriter ; 20 import java.net.HttpURLConnection ; 21 import java.net.URL ; 22 import java.net.URLEncoder ; 23 import java.rmi.RemoteException ; 24 25 import javax.ejb.CreateException ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.ObjectNotFoundException ; 28 import javax.naming.NamingException ; 29 30 import org.ejbca.core.ejb.BaseSessionBean; 31 import org.ejbca.core.ejb.log.ILogSessionHome; 32 import org.ejbca.core.ejb.log.ILogSessionRemote; 33 import org.ejbca.core.model.SecConst; 34 import org.ejbca.core.model.ca.AuthLoginException; 35 import org.ejbca.core.model.ca.AuthStatusException; 36 import org.ejbca.core.model.log.Admin; 37 import org.ejbca.core.model.log.LogEntry; 38 import org.ejbca.core.model.ra.UserDataVO; 39 40 41 42 43 69 public class RemoteAuthenticationSessionBean extends BaseSessionBean { 70 private static String REMOTE_PROTOCOL_VER = "1.0"; 71 72 73 String remoteurl = null; 74 75 76 private ILogSessionRemote logsession; 77 78 79 80 85 public void ejbCreate() throws CreateException { 86 debug(">ejbCreate()"); 87 88 remoteurl = getLocator().getString("java:comp/env/AuthURL"); 90 try { 91 ILogSessionHome logsessionhome = (ILogSessionHome) getLocator().getLocalHome(ILogSessionHome.COMP_NAME); 92 logsession = logsessionhome.create(); 93 } catch (Exception e) { 94 throw new EJBException (e); 95 } 96 97 debug("<ejbCreate()"); 98 } 99 100 110 public UserDataVO authenticateUser(Admin admin, String username, String password) 111 throws ObjectNotFoundException , AuthStatusException, AuthLoginException { 112 debug(">authenticateUser(" + username + ", hiddenpwd)"); 113 114 UserDataVO ret; 115 116 try { 117 ret = getDNfromRemote(REMOTE_PROTOCOL_VER, username, password); 118 } catch (Exception e) { 119 error("Authentication failed.", e); 120 throw new EJBException (e); 121 } 122 123 ret.setType(SecConst.USER_ENDUSER); 125 try{ 126 logsession.log(admin, ret.getCAId(), LogEntry.MODULE_CA, new java.util.Date (),username, null, LogEntry.EVENT_INFO_USERAUTHENTICATION,"Autenticated user"); 127 }catch(RemoteException re){ 128 throw new EJBException (re); 129 } 130 debug(">authenticateUser("+username+", hiddenpwd)"); 131 return ret; 132 } 134 141 public void finishUser(Admin admin, String username, String password) 142 throws ObjectNotFoundException { 143 } 144 145 158 private UserDataVO getDNfromRemote(String version, String user, String password) 159 throws NamingException , IOException { 160 debug(">getDNfromRemote(" + version + ", " + user + ", " + password + ")"); 161 162 URL url = new URL (remoteurl); 164 HttpURLConnection connection = (HttpURLConnection ) url.openConnection(); 165 connection.setDoOutput(true); 166 connection.setRequestMethod("POST"); 167 168 { 169 PrintWriter out = new PrintWriter (connection.getOutputStream()); 170 out.print("version=" + URLEncoder.encode(version,"UTF-8") + '&'); 171 out.print("username=" + URLEncoder.encode(user,"UTF-8") + '&'); 172 out.print("password=" + URLEncoder.encode(password,"UTF-8")); 173 out.close(); 174 } 175 176 BufferedReader in = new BufferedReader (new InputStreamReader (connection.getInputStream())); 177 178 if ((in.readLine().indexOf("status=200 OK") >= 0) && 179 (in.readLine().indexOf("result=grant") >= 0)) { 180 String dname = ""; 181 String email = null; 182 final String preFix = "dn-"; 183 184 while (true) { 185 final String line = in.readLine(); 186 187 if (line == null) { 188 break; 189 } 190 191 line.trim(); 192 193 if (line.indexOf('=') > 0) { 194 if (line.indexOf(preFix) == 0) { 195 if (line.substring(preFix.length()).indexOf("email") == 0) { 196 email = line.substring(preFix.length() + 6); 197 } else { 198 if (dname.length() > 0) { 199 dname += ", "; 200 } 201 202 dname += line.substring(preFix.length()); 203 } 204 } else { 205 dname += line; 206 } 207 } 208 } 209 210 UserDataVO ret = new UserDataVO(); 211 ret.setDN(dname); 212 ret.setEmail(email); 213 debug("<getDNfromRemote"); 214 215 return ret; 216 } 217 218 debug("<getDNfromRemote"); 219 220 return null; 221 } } | Popular Tags |