1 28 package net.sf.jguard.ext.authentication.loginmodules; 29 30 import java.sql.Connection ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 import javax.security.auth.Subject ; 40 import javax.security.auth.callback.CallbackHandler ; 41 import javax.security.auth.login.FailedLoginException ; 42 import javax.security.auth.login.LoginException ; 43 import javax.security.auth.spi.LoginModule ; 44 45 import net.sf.jguard.ext.authentication.manager.AuthenticationManagerFactory; 46 import net.sf.jguard.ext.authentication.manager.JdbcAuthenticationManager; 47 import net.sf.jguard.ext.database.ConnectionFactory; 48 49 54 public class JdbcLoginModule extends UserLoginModule implements LoginModule { 55 56 static public final String CRED_VALUE = "cred_value"; 57 static public final String CRED_NAME = "cred_name"; 58 static public final String NAME = "name"; 59 60 private static final String ID = "id"; 61 private static final Logger logger = Logger.getLogger(JdbcLoginModule.class.getName()); 62 63 private int userId = -1; 64 65 private ConnectionFactory connectionFactory; 66 67 private Properties props; 68 69 78 public void initialize(Subject subj, CallbackHandler cbkHandler, Map sState, Map opts) { 79 super.initialize(subj, cbkHandler, sState, opts); 80 81 JdbcAuthenticationManager authManager = (JdbcAuthenticationManager) AuthenticationManagerFactory 82 .getAuthenticationManager(); 83 props = authManager.getProperties(); 84 connectionFactory = authManager.getConnectionFactory(); 85 86 } 87 88 93 public boolean login() throws LoginException { 94 super.login(); 95 userId = getUserID(login); 96 if (!skipPasswordCheck) { 97 boolean passwordValidated = false; 98 try{ 99 passwordValidated = validatePassword(new String (password)); 100 }catch(LoginException e){ 101 loginOK = false; 102 throw e; 103 } 104 logger.finest("password validation =" + passwordValidated + " for userId=" + userId); 105 } 106 return true; 107 108 } 109 110 private boolean validatePassword(String cryptedPassword) throws LoginException { 111 boolean authenticated; 112 Connection conn2 = null; 113 PreparedStatement pst2 = null; 114 ResultSet rs2 = null; 115 try { 116 conn2 = connectionFactory.getConnection(); 117 pst2 = conn2.prepareStatement((String ) props.get("USER_PASSWORD_EXIST")); 118 pst2.setInt(1, userId); 119 pst2.setString(2, new String (cryptedPassword)); 120 rs2 = pst2.executeQuery(); 121 authenticated = rs2.next(); 122 if (authenticated == false) { 123 if (logger.isLoggable(Level.FINEST)) { 124 logger.finest(" password does not match "); 125 } 126 throw new FailedLoginException ("login.password.does.not.match"); 127 } 128 } catch (SQLException e2) { 129 if (logger.isLoggable(Level.SEVERE)) { 130 logger.severe("login() - connection in " + this.getClass().getName() + " failed " + e2.getMessage()); 131 logger.severe("USER_PASSWORD_EXIST=" + props.get("USER_PASSWORD_EXIST")); 132 } 133 throw new LoginException (e2.getMessage()); 134 135 } finally { 136 try { 137 rs2.close(); 138 pst2.close(); 139 conn2.close(); 140 } catch (SQLException e2) { 141 if (logger.isLoggable(Level.SEVERE)) { 142 logger.severe("login() - connection in MySQLLoginModule failed " + e2.getMessage()); 143 } 144 } 145 } 146 return authenticated; 147 } 148 149 private int getUserID(String login) throws LoginException { 150 boolean authenticated; 151 ResultSet rs = null; 153 PreparedStatement pst = null; 154 Connection conn = null; 155 int userId = -1; 156 try { 157 conn = connectionFactory.getConnection(); 158 pst = conn.prepareStatement((String ) props.get("USER_LOGIN_EXIST")); 159 pst.setString(1, login); 160 rs = pst.executeQuery(); 161 authenticated = rs.next(); 162 163 if (authenticated == false) { 164 if (logger.isLoggable(Level.FINEST)) { 165 logger.finest(" user " + login + " does not exists "); 166 } 167 throw new FailedLoginException ("login.user.does.not.exist"); 168 } 169 170 userId = rs.getInt(JdbcLoginModule.ID); 171 172 } catch (SQLException e1) { 173 if (logger.isLoggable(Level.SEVERE)) { 174 logger.severe("login() - connection " + e1.getMessage()); 175 } 176 throw new LoginException (e1.getMessage()); 177 } finally { 178 try { 179 rs.close(); 180 pst.close(); 181 conn.close(); 182 } catch (SQLException e2) { 183 if (logger.isLoggable(Level.SEVERE)) { 184 logger.severe("login() - connection failed " + e2.getMessage()); 185 } 186 } 187 } 188 189 return userId; 190 } 191 192 197 public boolean commit() throws LoginException { 198 if(!loginOK){ 199 return false; 200 } 201 try { 202 Subject subj = JdbcAuthenticationManager.getUser(userId); 203 subject.getPrincipals().addAll(subj.getPrincipals()); 205 subject.getPublicCredentials().addAll(subj.getPublicCredentials()); 206 subject.getPrivateCredentials().addAll(subj.getPrivateCredentials()); 207 if (logger.isLoggable(Level.FINEST)) { 208 logger.finest(" user authenticated subject=" + subject); 209 } 210 } catch (SQLException e) { 211 logger.log(Level.SEVERE, "commit()", e); 212 throw new LoginException (e.getMessage()); 213 } 214 215 return true; 216 } 217 218 } 219 | Popular Tags |