KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security.jndi;
8
9 import org.jboss.security.SimplePrincipal;
10 import org.jnp.interfaces.NamingContextFactory;
11
12 import javax.naming.Context JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import java.security.Principal JavaDoc;
15 import java.util.Hashtable JavaDoc;
16
17 /** A naming provider InitialContextFactory implementation that combines the
18  * authentication phase with the InitialContext creation. During the
19  * getInitialContext callback from the JNDI naming, layer security context
20  * identity is populated with the username obtained from the
21  * Context.SECURITY_PRINCIPAL env property and the credentials from the
22  * Context.SECURITY_CREDENTIALS env property. There is no actual authentication
23  * of this information. It is merely made available to the jboss transport
24  * layer for incorporation into subsequent invocations. Authentication and
25  * authorization will occur on the server.
26  *
27  * @see javax.naming.spi.InitialContextFactory
28  *
29  * @author Scott.Stark@jboss.org
30  * @version $Revision: 1.1 $
31  */

32 public class JndiLoginInitialContextFactory extends NamingContextFactory
33 {
34    // InitialContextFactory implementation --------------------------
35

36    /** Take the env Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS
37     * and propagate these to the SecurityAssociation principal and credential.
38     * If Context.SECURITY_PRINCIPAL is a java.security.Principal then it is
39     * used as is, otherwise its treated as a name using toString and a
40     * SimplePrincipal is created. The Context.SECURITY_CREDENTIALS is passed
41     * as is.
42     * @param env
43     * @throws NamingException
44     */

45    public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
46       throws NamingException JavaDoc
47    {
48       // Get the login principal and credentials from the JNDI env
49
Object JavaDoc credentials = env.get(Context.SECURITY_CREDENTIALS);
50       Object JavaDoc principal = env.get(Context.SECURITY_PRINCIPAL);
51       Principal securityPrincipal = null;
52       // See if the principal is a Principal or String
53
if( principal instanceof Principal )
54       {
55          securityPrincipal = (Principal) principal;
56       }
57       else
58       {
59          // Simply convert this to a name using toString
60
String JavaDoc username = principal.toString();
61          securityPrincipal = new SimplePrincipal(username);
62       }
63       // Associate this security context
64
SecurityAssociationActions.setPrincipalInfo(securityPrincipal, credentials);
65       // Now return the context using the standard jnp naming context factory
66
Context JavaDoc iniCtx = super.getInitialContext(env);
67       return iniCtx;
68    }
69
70 }
71
Popular Tags