1 7 package org.jboss.security.jndi; 8 9 import java.util.Hashtable ; 10 import java.security.Principal ; 11 import javax.naming.AuthenticationException ; 12 import javax.naming.Context ; 13 import javax.naming.NamingException ; 14 import javax.security.auth.login.LoginContext ; 15 import javax.security.auth.login.LoginException ; 16 17 import org.jnp.interfaces.NamingContextFactory; 18 import org.jboss.security.auth.callback.UsernamePasswordHandler; 19 20 33 public class LoginInitialContextFactory extends NamingContextFactory 34 { 35 37 41 public Context getInitialContext(Hashtable env) 42 throws NamingException 43 { 44 String protocol = "other"; 46 Object prop = env.get(Context.SECURITY_PROTOCOL); 47 if( prop != null ) 48 protocol = prop.toString(); 49 50 Object credentials = env.get(Context.SECURITY_CREDENTIALS); 52 Object principal = env.get(Context.SECURITY_PRINCIPAL); 53 try 54 { 55 String username; 57 if( principal instanceof Principal ) 58 { 59 Principal p = (Principal ) principal; 60 username = p.getName(); 61 } 62 else 63 { 64 username = principal.toString(); 65 } 66 UsernamePasswordHandler handler = new UsernamePasswordHandler(username, 67 credentials); 68 LoginContext lc = new LoginContext (protocol, handler); 70 lc.login(); 71 } 72 catch(LoginException e) 73 { 74 AuthenticationException ex = new AuthenticationException ("Failed to login using protocol="+protocol); 75 ex.setRootCause(e); 76 throw ex; 77 } 78 79 Context iniCtx = super.getInitialContext(env); 81 return iniCtx; 82 } 83 84 } 85 | Popular Tags |