1 9 package org.jboss.portal.core.security.jaas; 10 11 import java.security.Principal ; 12 import java.security.acl.Group ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import javax.naming.InitialContext ; 18 import javax.naming.NamingException ; 19 import javax.security.auth.Subject ; 20 import javax.security.auth.callback.CallbackHandler ; 21 import javax.security.auth.login.LoginException ; 22 import javax.transaction.TransactionManager ; 23 24 import org.jboss.portal.common.transaction.Transactions; 25 import org.jboss.security.SimpleGroup; 26 import org.jboss.security.auth.spi.UsernamePasswordLoginModule; 27 import org.hibernate.SessionFactory; 28 import org.hibernate.Session; 29 import org.hibernate.HibernateException; 30 import org.hibernate.Query; 31 32 38 public class HibernateLoginModule 39 extends UsernamePasswordLoginModule 40 { 41 42 protected String factoryJNDIName; 43 protected String principalsQuery; 44 protected String rolesQuery; 45 protected String additionalRole; 46 47 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) 48 { 49 super.initialize(subject, callbackHandler, sharedState, options); 50 51 factoryJNDIName = (String )options.get("factoryJNDIName"); 53 principalsQuery = (String )options.get("principalsQuery"); 54 rolesQuery = (String )options.get("rolesQuery"); 55 additionalRole = (String )options.get("additionalRole"); 56 57 log.trace("factoryJNDIName = " + factoryJNDIName); 59 log.trace("principalsQuery = " + principalsQuery); 60 log.trace("rolesQuery = " + rolesQuery); 61 log.trace("additionalRole = " + additionalRole); 62 } 63 64 private SessionFactory factory; 65 66 private Session getSession() throws HibernateException, IllegalStateException , NamingException 67 { 68 if (factory == null) 69 { 70 factory = (SessionFactory)new InitialContext ().lookup(factoryJNDIName); 71 } 72 return factory.getCurrentSession(); 73 } 74 75 protected String getUsersPassword() throws LoginException 76 { 77 try 78 { 79 TransactionManager tm = (TransactionManager )new InitialContext ().lookup("java:/TransactionManager"); 80 return (String )Transactions.required(tm, new Transactions.Runnable() 81 { 82 public Object run() throws Exception 83 { 84 Session session = null; 85 String password = null; 86 try 87 { 88 session = getSession(); 89 Query query = session.createQuery(principalsQuery); 90 query.setString("username", getUsername()); 91 password = (String )query.uniqueResult(); 92 } 93 catch (Exception e) 94 { 95 throw new LoginException (e.toString()); 96 } 97 return password; 98 } 99 }); 100 } 101 catch (Exception e) 102 { 103 Throwable cause = e.getCause(); 104 throw new LoginException (cause.toString()); 105 } 106 } 107 108 protected Group [] getRoleSets() throws LoginException 109 { 110 try 111 { 112 TransactionManager tm = (TransactionManager )new InitialContext ().lookup("java:/TransactionManager"); 113 return (Group [])Transactions.required(tm, new Transactions.Runnable() 114 { 115 public Object run() throws Exception 116 { 117 Group [] roleSets = null; 118 Session session = null; 119 try 120 { 121 session = getSession(); 122 Query query = session.createQuery(rolesQuery); 123 query.setString("username", getUsername()); 124 125 Group rolesGroup = new SimpleGroup("Roles"); 126 rolesGroup.addMember(HibernateLoginModule.this.createIdentity(additionalRole)); 127 HashMap setsMap = new HashMap (); 128 setsMap.put("Roles", rolesGroup); 129 130 for (Iterator iterator = query.iterate(); iterator.hasNext();) 131 { 132 String name = (String )iterator.next(); 133 String groupName = "Roles"; 134 135 Group group = (Group )setsMap.get(groupName); 136 if (group == null) 137 { 138 group = new SimpleGroup(groupName); 139 setsMap.put(groupName, group); 140 } 141 142 try 143 { 144 Principal p = HibernateLoginModule.this.createIdentity(name); 145 group.addMember(p); 146 } 147 catch (Exception e) 148 { 149 log.debug("Failed to create principal " + name, e); 150 } 151 } 152 153 roleSets = new Group [setsMap.size()]; 154 setsMap.values().toArray(roleSets); 155 } 156 catch (Exception e) 157 { 158 throw new LoginException (e.toString()); 159 } 160 return roleSets; 161 } 162 }); 163 } 164 catch (Exception e) 165 { 166 Throwable cause = e.getCause(); 167 throw new LoginException (cause.toString()); 168 } 169 170 } 171 172 } 173 | Popular Tags |