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 javax.security.auth.login.LoginException;
27:
28: import org.jboss.security.SimpleGroup;
29: import org.jboss.security.SimplePrincipal;
30: import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
31:
32: /**
33: * A simple server login module useful to quick setup of security for testing
34: * purposes. It implements the following simple algorithm:
35: * <ul>
36: * <li> if password is null, authenticate the user and assign an identity of "guest"
37: * and a role of "guest".
38: * <li> else if password is equal to the user name, assign an identity equal to
39: * the username and both "user" and "guest" roles
40: * <li> else authentication fails.
41: * </ul>
42: *
43: * @author <a href="on@ibis.odessa.ua">Oleg Nitz</a>
44: * @author Scott.Stark@jboss.org
45: * @version $Revision: 57203 $
46: */
47: public class SimpleServerLoginModule extends
48: UsernamePasswordLoginModule {
49: private SimplePrincipal user;
50: private boolean guestOnly;
51:
52: protected Principal getIdentity() {
53: Principal principal = user;
54: if (principal == null)
55: principal = super .getIdentity();
56: return principal;
57: }
58:
59: protected boolean validatePassword(String inputPassword,
60: String expectedPassword) {
61: boolean isValid = false;
62: if (inputPassword == null) {
63: guestOnly = true;
64: isValid = true;
65: user = new SimplePrincipal("guest");
66: } else {
67: isValid = inputPassword.equals(expectedPassword);
68: }
69: return isValid;
70: }
71:
72: protected Group[] getRoleSets() throws LoginException {
73: Group[] roleSets = { new SimpleGroup("Roles") };
74: if (guestOnly == false)
75: roleSets[0].addMember(new SimplePrincipal("user"));
76: roleSets[0].addMember(new SimplePrincipal("guest"));
77: return roleSets;
78: }
79:
80: protected String getUsersPassword() throws LoginException {
81: return getUsername();
82: }
83:
84: }
|