KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > jndi > LoginInitialContextFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.jndi;
8
9 import java.util.Hashtable JavaDoc;
10 import java.security.Principal JavaDoc;
11 import javax.naming.AuthenticationException JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.security.auth.login.LoginContext JavaDoc;
15 import javax.security.auth.login.LoginException JavaDoc;
16
17 import org.jnp.interfaces.NamingContextFactory;
18 import org.jboss.security.auth.callback.UsernamePasswordHandler;
19
20 /** A naming provider InitialContextFactory implementation that combines the authentication phase
21  * with the InitialContext creation. During the getInitialContext callback from the JNDI naming
22  * layer a JAAS LoginContext is created using the login configuration name passed in as
23  * the Context.SECURITY_PROTOCOL env property. The CallbackHandler used is a
24  * org.jboss.security.auth.callback.UsernamePasswordHandler that is populated
25  * with the username obtained from the Context.SECURITY_PRINCIPAL env property
26  * and the credentials from the Context.SECURITY_CREDENTIALS env property.
27  *
28  * @see javax.naming.spi.InitialContextFactory
29  *
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 1.4.6.1 $
32  */

33 public class LoginInitialContextFactory extends NamingContextFactory
34 {
35    // InitialContextFactory implementation --------------------------
36

37    /** Create new initial context by invoking the NamingContextFactory version of this
38     * method after performing a JAAS login.
39     *
40     */

41    public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
42       throws NamingException JavaDoc
43    {
44       // Get the login configuration name to use, defaulting to "other"
45
String JavaDoc protocol = "other";
46       Object JavaDoc prop = env.get(Context.SECURITY_PROTOCOL);
47       if( prop != null )
48          protocol = prop.toString();
49
50       // Get the login principal and credentials from the JNDI env
51
Object JavaDoc credentials = env.get(Context.SECURITY_CREDENTIALS);
52       Object JavaDoc principal = env.get(Context.SECURITY_PRINCIPAL);
53       try
54       {
55          // Get the principal username
56
String JavaDoc username;
57          if( principal instanceof Principal JavaDoc )
58          {
59             Principal JavaDoc p = (Principal JavaDoc) principal;
60             username = p.getName();
61          }
62          else
63          {
64             username = principal.toString();
65          }
66          UsernamePasswordHandler handler = new UsernamePasswordHandler(username,
67             credentials);
68          // Do the JAAS login
69
LoginContext JavaDoc lc = new LoginContext JavaDoc(protocol, handler);
70          lc.login();
71       }
72       catch(LoginException JavaDoc e)
73       {
74          AuthenticationException JavaDoc ex = new AuthenticationException JavaDoc("Failed to login using protocol="+protocol);
75          ex.setRootCause(e);
76          throw ex;
77       }
78
79       // Now return the context using the standard jnp naming context factory
80
Context JavaDoc iniCtx = super.getInitialContext(env);
81       return iniCtx;
82    }
83
84 }
85
Popular Tags