KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > TestLoginModule


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.test;
8
9 import java.util.Map JavaDoc;
10 import javax.security.auth.Subject JavaDoc;
11 import javax.security.auth.callback.CallbackHandler JavaDoc;
12 import javax.security.auth.login.LoginException JavaDoc;
13 import javax.security.auth.spi.LoginModule JavaDoc;
14 import org.jboss.security.SimplePrincipal;
15
16 public class TestLoginModule implements LoginModule JavaDoc
17 {
18    Subject JavaDoc subject;
19    String JavaDoc principal;
20    String JavaDoc name;
21    boolean succeed;
22    boolean throwEx;
23
24    public TestLoginModule()
25    {
26    }
27
28    public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc handler, Map JavaDoc sharedState, Map JavaDoc options)
29    {
30       this.subject = subject;
31       principal = (String JavaDoc) options.get("principal");
32       if( principal == null )
33           principal = "guest";
34       name = (String JavaDoc) options.get("name");
35       String JavaDoc opt = (String JavaDoc) options.get("succeed");
36       succeed = Boolean.valueOf(opt).booleanValue();
37       opt = (String JavaDoc) options.get("throwEx");
38       throwEx = Boolean.valueOf(opt).booleanValue();
39       System.out.println("initialize, name="+name);
40       opt = (String JavaDoc) options.get("initEx");
41       if( Boolean.valueOf(opt) == Boolean.TRUE )
42          throw new IllegalArgumentException JavaDoc("Failed during init, name="+name);
43    }
44
45    public boolean login() throws LoginException JavaDoc
46    {
47       System.out.println("login, name="+name+", succeed="+succeed);
48       if( throwEx )
49          throw new LoginException JavaDoc("Failed during login, name="+name);
50       return succeed;
51    }
52
53    public boolean commit() throws LoginException JavaDoc
54    {
55       System.out.println("commit, name="+name);
56       subject.getPrincipals().add(new SimplePrincipal(principal));
57       subject.getPublicCredentials().add("A public credential");
58       subject.getPrivateCredentials().add("A private credential");
59       return true;
60    }
61
62    public boolean abort() throws LoginException JavaDoc
63    {
64       System.out.println("abort, name="+name);
65       return true;
66    }
67
68    public boolean logout() throws LoginException JavaDoc
69    {
70       System.out.println("logout, name="+name);
71       subject.getPrincipals().remove(new SimplePrincipal(principal));
72       return succeed;
73    }
74
75 }
76
Popular Tags