01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.security.auth.spi;
23:
24: import java.security.Principal;
25: import java.security.acl.Group;
26: import java.util.Map;
27: import java.util.StringTokenizer;
28: import javax.security.auth.Subject;
29: import javax.security.auth.callback.CallbackHandler;
30: import javax.security.auth.login.LoginException;
31:
32: import org.jboss.security.SimpleGroup;
33: import org.jboss.security.SimplePrincipal;
34: import org.jboss.security.auth.spi.AbstractServerLoginModule;
35:
36: /**
37: * A simple login module that simply associates the principal specified
38: * in the module options with any subject authenticated against the module.
39: * The type of Principal class used is
40: * <code>org.jboss.security.SimplePrincipal.</code>
41: * <p>
42: * If no principal option is specified a principal with the name of 'guest'
43: * is used.
44: *
45: * @see org.jboss.security.SimpleGroup
46: * @see org.jboss.security.SimplePrincipal
47: *
48: * @author Scott.Stark@jboss.org
49: * @version $Revision: 57203 $
50: */
51: public class IdentityLoginModule extends AbstractServerLoginModule {
52: private String principalName;
53: private String roleNames;
54:
55: public IdentityLoginModule() {
56: }
57:
58: public void initialize(Subject subject, CallbackHandler handler,
59: Map sharedState, Map options) {
60: super .initialize(subject, handler, sharedState, options);
61: principalName = (String) options.get("principal");
62: if (principalName == null)
63: principalName = "guest";
64: roleNames = (String) options.get("roles");
65: }
66:
67: public boolean login() throws LoginException {
68: if (super .login() == true)
69: return true;
70:
71: Principal principal = new SimplePrincipal(principalName);
72: subject.getPrincipals().add(principal);
73: // Put the principal name into the sharedState map
74: sharedState
75: .put("javax.security.auth.login.name", principalName);
76: super .loginOk = true;
77: return true;
78: }
79:
80: protected Principal getIdentity() {
81: Principal principal = new SimplePrincipal(principalName);
82: return principal;
83: }
84:
85: protected Group[] getRoleSets() throws LoginException {
86: SimpleGroup roles = new SimpleGroup("Roles");
87: Group[] roleSets = { roles };
88: if (roleNames != null) {
89: StringTokenizer tokenizer = new StringTokenizer(roleNames,
90: ",");
91: while (tokenizer.hasMoreTokens()) {
92: String roleName = tokenizer.nextToken();
93: roles.addMember(new SimplePrincipal(roleName));
94: }
95: }
96: return roleSets;
97: }
98: }
|