KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > NoLoginModuleReuseTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.security.jaas;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import javax.security.auth.Subject JavaDoc;
22 import javax.security.auth.callback.CallbackHandler JavaDoc;
23 import javax.security.auth.login.LoginException JavaDoc;
24 import javax.security.auth.spi.LoginModule JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal;
29 import org.apache.geronimo.security.jaas.server.JaasSecuritySession;
30 import org.apache.geronimo.security.jaas.server.JaasLoginModuleConfiguration;
31
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class NoLoginModuleReuseTest extends TestCase {
37
38     public void testNoLoginModuleReuse() throws Exception JavaDoc {
39         JaasLoginModuleConfiguration m1 = new JaasLoginModuleConfiguration(MockLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, new HashMap JavaDoc(), true, "D1", true, MockLoginModule.class.getClassLoader());
40         doSecurityContextLogin(m1);
41         doSecurityContextLogin(m1);
42     }
43
44     private void doSecurityContextLogin(JaasLoginModuleConfiguration m1) throws LoginException JavaDoc {
45         JaasSecuritySession c = new JaasSecuritySession("realm", new JaasLoginModuleConfiguration[] {m1}, new HashMap JavaDoc(), this.getClass().getClassLoader());
46         Subject JavaDoc s = c.getSubject();
47         c.getLoginModule(0).initialize(s, null, null, null);
48         c.getLoginModule(0).login();
49         c.getLoginModule(0).commit();
50     }
51
52     public static class MockLoginModule implements LoginModule JavaDoc {
53
54         private Subject JavaDoc subject;
55         private boolean used = false;
56
57         public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc map, Map JavaDoc map1) {
58             this.subject = subject;
59         }
60
61         public boolean login() throws LoginException JavaDoc {
62             if (used) {
63                 throw new LoginException JavaDoc("already used");
64             }
65             used = true;
66             return true;
67         }
68
69         public boolean commit() throws LoginException JavaDoc {
70             subject.getPrincipals().add(new GeronimoGroupPrincipal("Foo"));
71             return true;
72         }
73
74         public boolean abort() throws LoginException JavaDoc {
75             return false;
76         }
77
78         public boolean logout() throws LoginException JavaDoc {
79             return false;
80         }
81     }
82
83 }
84
Popular Tags