1 7 package org.jboss.security.auth.spi; 8 9 10 import java.security.Principal ; 11 import java.security.acl.Group ; 12 import java.util.Enumeration ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.lang.reflect.Constructor ; 17 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.spi.LoginModule ; 22 23 import org.jboss.logging.Logger; 24 import org.jboss.security.NestableGroup; 25 import org.jboss.security.SimpleGroup; 26 import org.jboss.security.SimplePrincipal; 27 28 57 public abstract class AbstractServerLoginModule implements LoginModule 58 { 59 protected Subject subject; 60 protected CallbackHandler callbackHandler; 61 protected Map sharedState; 62 protected Map options; 63 protected Logger log; 64 65 protected boolean useFirstPass; 66 69 protected boolean loginOk; 70 71 protected String principalClassName; 72 73 protected Principal unauthenticatedIdentity; 74 75 97 public void initialize(Subject subject, CallbackHandler callbackHandler, 98 Map sharedState, Map options) 99 { 100 this.subject = subject; 101 this.callbackHandler = callbackHandler; 102 this.sharedState = sharedState; 103 this.options = options; 104 log = Logger.getLogger(getClass()); 105 if( log.isTraceEnabled() ) 106 log.trace("initialize, instance=@"+System.identityHashCode(this)); 107 111 String passwordStacking = (String ) options.get("password-stacking"); 112 if( passwordStacking != null && passwordStacking.equalsIgnoreCase("useFirstPass") ) 113 useFirstPass = true; 114 115 principalClassName = (String ) options.get("principalClass"); 117 118 String name = (String ) options.get("unauthenticatedIdentity"); 120 if( name != null ) 121 { 122 try 123 { 124 unauthenticatedIdentity = createIdentity(name); 125 log.trace("Saw unauthenticatedIdentity="+name); 126 } 127 catch(Exception e) 128 { 129 log.warn("Failed to create custom unauthenticatedIdentity", e); 130 } 131 } 132 } 133 134 143 public boolean login() throws LoginException 144 { 145 log.trace("login"); 146 loginOk = false; 147 if( useFirstPass == true ) 149 { 150 try 151 { 152 Object identity = sharedState.get("javax.security.auth.login.name"); 153 Object credential = sharedState.get("javax.security.auth.login.password"); 154 if( identity != null && credential != null ) 155 { 156 loginOk = true; 157 return true; 158 } 159 } 161 catch(Exception e) 162 { log.error("login failed", e); 164 } 165 } 166 return false; 167 } 168 169 179 public boolean commit() throws LoginException 180 { 181 log.trace("commit, loginOk="+loginOk); 182 if( loginOk == false ) 183 return false; 184 185 Set principals = subject.getPrincipals(); 186 Principal identity = getIdentity(); 187 principals.add(identity); 188 Group [] roleSets = getRoleSets(); 189 for(int g = 0; g < roleSets.length; g ++) 190 { 191 Group group = roleSets[g]; 192 String name = group.getName(); 193 Group subjectGroup = createGroup(name, principals); 194 if( subjectGroup instanceof NestableGroup ) 195 { 196 199 SimpleGroup tmp = new SimpleGroup("Roles"); 200 subjectGroup.addMember(tmp); 201 subjectGroup = tmp; 202 } 203 Enumeration members = group.members(); 205 while( members.hasMoreElements() ) 206 { 207 Principal role = (Principal ) members.nextElement(); 208 subjectGroup.addMember(role); 209 } 210 } 211 return true; 212 } 213 214 217 public boolean abort() throws LoginException 218 { 219 log.trace("abort"); 220 return true; 221 } 222 223 226 public boolean logout() throws LoginException 227 { 228 log.trace("logout"); 229 Principal identity = getIdentity(); 231 Set principals = subject.getPrincipals(); 232 principals.remove(identity); 233 return true; 235 } 236 238 240 243 abstract protected Principal getIdentity(); 244 251 abstract protected Group [] getRoleSets() throws LoginException ; 252 253 protected boolean getUseFirstPass() 254 { 255 return useFirstPass; 256 } 257 protected Principal getUnauthenticatedIdentity() 258 { 259 return unauthenticatedIdentity; 260 } 261 262 266 protected Group createGroup(String name, Set principals) 267 { 268 Group roles = null; 269 Iterator iter = principals.iterator(); 270 while( iter.hasNext() ) 271 { 272 Object next = iter.next(); 273 if( (next instanceof Group ) == false ) 274 continue; 275 Group grp = (Group ) next; 276 if( grp.getName().equals(name) ) 277 { 278 roles = grp; 279 break; 280 } 281 } 282 if( roles == null ) 284 { 285 roles = new SimpleGroup(name); 286 principals.add(roles); 287 } 288 return roles; 289 } 290 291 300 protected Principal createIdentity(String username) 301 throws Exception 302 { 303 Principal p = null; 304 if( principalClassName == null ) 305 { 306 p = new SimplePrincipal(username); 307 } 308 else 309 { 310 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 311 Class clazz = loader.loadClass(principalClassName); 312 Class [] ctorSig = {String .class}; 313 Constructor ctor = clazz.getConstructor(ctorSig); 314 Object [] ctorArgs = {username}; 315 p = (Principal ) ctor.newInstance(ctorArgs); 316 } 317 return p; 318 } 319 } 320 | Popular Tags |