KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > security > ConfiguredIdentityLoginModule


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.security;
23
24
25 import java.security.Principal JavaDoc;
26 import java.security.acl.Group JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.resource.spi.security.PasswordCredential JavaDoc;
30 import javax.security.auth.Subject JavaDoc;
31 import javax.security.auth.callback.CallbackHandler JavaDoc;
32 import javax.security.auth.login.LoginException JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.security.SimplePrincipal;
36
37 /**
38  * A simple login module that simply associates the principal specified
39  * in the module options with any subject authenticated against the module.
40  * The type of Principal class used is
41  * <code>org.jboss.security.SimplePrincipal.</code>
42  * <p>
43  * If no principal option is specified a principal with the name of 'guest'
44  * is used.
45  *
46  * @see org.jboss.security.SimpleGroup
47  * @see org.jboss.security.SimplePrincipal
48  *
49  * @author Scott.Stark@jboss.org
50  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
51  * @version $Revision: 37459 $
52  */

53 public class ConfiguredIdentityLoginModule extends AbstractPasswordCredentialLoginModule
54 {
55    private String JavaDoc principalName;
56    private String JavaDoc userName;
57    private String JavaDoc password;
58
59    private static final Logger log = Logger.getLogger(ConfiguredIdentityLoginModule.class);
60
61
62    public ConfiguredIdentityLoginModule()
63    {
64    }
65
66    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc handler, Map JavaDoc sharedState, Map JavaDoc options)
67    {
68       super.initialize(subject, handler, sharedState, options);
69       principalName = (String JavaDoc) options.get("principal");
70       if( principalName == null )
71       {
72          throw new IllegalArgumentException JavaDoc("Must supply a principal name!");
73       }
74       userName = (String JavaDoc) options.get("userName");
75       if( userName == null )
76       {
77          throw new IllegalArgumentException JavaDoc("Must supply a user name!");
78       }
79       password = (String JavaDoc) options.get("password");
80       if( password == null )
81       {
82          log.warn("Creating LoginModule with no configured password!");
83          password = "";
84       }
85       log.trace("got principal: " + principalName + ", username: " + userName + ", password: " + password);
86
87    }
88
89    public boolean login() throws LoginException JavaDoc
90    {
91       log.trace("login called");
92       if( super.login() == true )
93          return true;
94
95       Principal JavaDoc principal = new SimplePrincipal(principalName);
96       SubjectActions.addPrincipals(subject, principal);
97       // Put the principal name into the sharedState map
98
sharedState.put("javax.security.auth.login.name", principalName);
99       PasswordCredential JavaDoc cred = new PasswordCredential JavaDoc(userName, password.toCharArray());
100       cred.setManagedConnectionFactory(getMcf());
101       SubjectActions.addCredentials(subject, cred);
102       super.loginOk = true;
103       return true;
104    }
105
106    protected Principal JavaDoc getIdentity()
107    {
108       log.trace("getIdentity called");
109       Principal JavaDoc principal = new SimplePrincipal(principalName);
110       return principal;
111    }
112
113    /** This method simply returns an empty array of Groups which means that
114    no role based permissions are assigned.
115    */

116    protected Group JavaDoc[] getRoleSets() throws LoginException JavaDoc
117    {
118       log.trace("getRoleSets called");
119       return new Group JavaDoc[] {};
120    }
121    
122 }
123
Popular Tags