KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > auth > spi > SimpleServerLoginModule


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

7
8 package org.jboss.security.auth.spi;
9
10 import java.security.Principal JavaDoc;
11 import java.security.acl.Group JavaDoc;
12 import javax.security.auth.login.LoginException JavaDoc;
13
14 import org.jboss.security.SimpleGroup;
15 import org.jboss.security.SimplePrincipal;
16 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
17
18 /**
19  * A simple server login module useful to quick setup of security for testing
20  * purposes. It implements the following simple algorithm:
21  * <ul>
22  * <li> if password is null, authenticate the user and assign an identity of "guest"
23  * and a role of "guest".
24  * <li> else if password is equal to the user name, assign an identity equal to
25  * the username and both "user" and "guest" roles
26  * <li> else authentication fails.
27  * </ul>
28  *
29  * @author <a HREF="on@ibis.odessa.ua">Oleg Nitz</a>
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 1.4 $
32  */

33 public class SimpleServerLoginModule extends UsernamePasswordLoginModule
34 {
35    private SimplePrincipal user;
36    private boolean guestOnly;
37
38    protected Principal JavaDoc getIdentity()
39    {
40       Principal JavaDoc principal = user;
41       if( principal == null )
42          principal = super.getIdentity();
43       return principal;
44    }
45
46    protected boolean validatePassword(String JavaDoc inputPassword, String JavaDoc expectedPassword)
47    {
48       boolean isValid = false;
49       if( inputPassword == null )
50       {
51          guestOnly = true;
52          isValid = true;
53          user = new SimplePrincipal("guest");
54       }
55       else
56       {
57          isValid = inputPassword.equals(expectedPassword);
58       }
59       return isValid;
60    }
61
62    protected Group JavaDoc[] getRoleSets() throws LoginException JavaDoc
63    {
64       Group JavaDoc[] roleSets = {new SimpleGroup("Roles")};
65       if( guestOnly == false )
66          roleSets[0].addMember(new SimplePrincipal("user"));
67       roleSets[0].addMember(new SimplePrincipal("guest"));
68       return roleSets;
69    }
70
71    protected String JavaDoc getUsersPassword() throws LoginException JavaDoc
72    {
73       return getUsername();
74    }
75
76 }
77
Popular Tags