1 7 package org.jboss.security.auth.spi; 8 9 import java.security.acl.Group ; 10 import java.util.Map ; 11 import java.sql.Connection ; 12 import java.sql.PreparedStatement ; 13 import java.sql.ResultSet ; 14 import java.sql.SQLException ; 15 import javax.naming.InitialContext ; 16 import javax.naming.NamingException ; 17 import javax.sql.DataSource ; 18 import javax.security.auth.Subject ; 19 import javax.security.auth.callback.CallbackHandler ; 20 import javax.security.auth.login.LoginException ; 21 import javax.security.auth.login.FailedLoginException ; 22 23 49 public class DatabaseServerLoginModule extends UsernamePasswordLoginModule 50 { 51 52 protected String dsJndiName; 53 54 protected String principalsQuery = "select Password from Principals where PrincipalID=?"; 55 56 protected String rolesQuery = "select Role, RoleGroup from Roles where PrincipalID=?"; 57 58 69 public void initialize(Subject subject, CallbackHandler callbackHandler, 70 Map sharedState, Map options) 71 { 72 super.initialize(subject, callbackHandler, sharedState, options); 73 dsJndiName = (String ) options.get("dsJndiName"); 74 if( dsJndiName == null ) 75 dsJndiName = "java:/DefaultDS"; 76 Object tmp = options.get("principalsQuery"); 77 if( tmp != null ) 78 principalsQuery = tmp.toString(); 79 tmp = options.get("rolesQuery"); 80 if( tmp != null ) 81 rolesQuery = tmp.toString(); 82 log.trace("DatabaseServerLoginModule, dsJndiName="+dsJndiName); 83 log.trace("principalsQuery="+principalsQuery); 84 log.trace("rolesQuery="+rolesQuery); 85 } 86 87 93 protected String getUsersPassword() throws LoginException 94 { 95 String username = getUsername(); 96 String password = null; 97 Connection conn = null; 98 PreparedStatement ps = null; 99 ResultSet rs = null; 100 101 try 102 { 103 InitialContext ctx = new InitialContext (); 104 DataSource ds = (DataSource ) ctx.lookup(dsJndiName); 105 conn = ds.getConnection(); 106 ps = conn.prepareStatement(principalsQuery); 108 ps.setString(1, username); 109 rs = ps.executeQuery(); 110 if( rs.next() == false ) 111 throw new FailedLoginException ("No matching username found in Principals"); 112 113 password = rs.getString(1); 114 password = convertRawPassword(password); 115 } 116 catch(NamingException ex) 117 { 118 throw new LoginException (ex.toString(true)); 119 } 120 catch(SQLException ex) 121 { 122 log.error("Query failed", ex); 123 throw new LoginException (ex.toString()); 124 } 125 finally 126 { 127 if (rs != null) 128 { 129 try 130 { 131 rs.close(); 132 } 133 catch(SQLException e) 134 {} 135 } 136 if( ps != null ) 137 { 138 try 139 { 140 ps.close(); 141 } 142 catch(SQLException e) 143 {} 144 } 145 if( conn != null ) 146 { 147 try 148 { 149 conn.close(); 150 } 151 catch (SQLException ex) 152 {} 153 } 154 } 155 return password; 156 } 157 158 163 protected Group [] getRoleSets() throws LoginException 164 { 165 String username = getUsername(); 166 Group [] roleSets = Util.getRoleSets(username, dsJndiName, rolesQuery, this); 167 return roleSets; 168 } 169 170 176 protected String convertRawPassword(String rawPassword) 177 { 178 return rawPassword; 179 } 180 } 181 | Popular Tags |