KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxremoting > AuthenticatorTest


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.jmxremoting;
18
19 import java.security.Principal JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import javax.security.auth.Subject JavaDoc;
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
30 import javax.security.auth.login.Configuration JavaDoc;
31 import javax.security.auth.login.LoginException JavaDoc;
32 import javax.security.auth.spi.LoginModule JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  *
38  *
39  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 public class AuthenticatorTest extends TestCase {
42     private static final String JavaDoc CONFIG_NAME = "testConfig";
43     private Configuration JavaDoc oldConfiguration;
44     private Configuration JavaDoc loginConfig;
45     private String JavaDoc[] credentials;
46     private Authenticator authenticator;
47
48     public void testAuthenticateWithValidPassword() {
49         try {
50             Subject JavaDoc s = authenticator.authenticate(credentials);
51             Set JavaDoc principals = s.getPrincipals();
52             assertTrue(principals.contains(new MockPrincipal("username")));
53         } catch (SecurityException JavaDoc e) {
54             e.printStackTrace();
55             fail();
56         }
57     }
58
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         try {
62             oldConfiguration = Configuration.getConfiguration();
63         } catch (SecurityException JavaDoc e) {
64             oldConfiguration = null;
65         }
66         loginConfig = new MockConfiguration();
67         Configuration.setConfiguration(loginConfig);
68
69         credentials = new String JavaDoc[]{"username", "password"};
70         authenticator = new Authenticator(CONFIG_NAME, getClass().getClassLoader());
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         Configuration.setConfiguration(oldConfiguration);
75         super.tearDown();
76     }
77
78     private class MockConfiguration extends Configuration JavaDoc {
79         public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(String JavaDoc applicationName) {
80             if (CONFIG_NAME.equals(applicationName) == false) {
81                 fail();
82             }
83             Map JavaDoc map = new HashMap JavaDoc();
84             map.put("username", "password");
85             AppConfigurationEntry JavaDoc entry = new AppConfigurationEntry JavaDoc(MockModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map);
86             return new AppConfigurationEntry JavaDoc[] {entry};
87         }
88
89         public void refresh() {
90         }
91     }
92
93     public static class MockModule implements LoginModule JavaDoc {
94         private Subject JavaDoc subject;
95         private CallbackHandler JavaDoc handler;
96         private Map JavaDoc options;
97         private String JavaDoc username;
98
99         public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options) {
100             this.subject = subject;
101             this.handler = callbackHandler;
102             this.options = options;
103         }
104
105         public boolean login() throws LoginException JavaDoc {
106             NameCallback JavaDoc nameCallback = new NameCallback JavaDoc("name");
107             PasswordCallback JavaDoc passwordCallback = new PasswordCallback JavaDoc("password", false);
108             try {
109                 handler.handle(new Callback JavaDoc[] {nameCallback, passwordCallback});
110                 username = nameCallback.getName();
111                 String JavaDoc password = (String JavaDoc) options.get(username);
112                 if (password == null) {
113                     return false;
114                 }
115                 return password.equals(new String JavaDoc(passwordCallback.getPassword()));
116             } catch (java.io.IOException JavaDoc e) {
117                 return false;
118             } catch (UnsupportedCallbackException JavaDoc e) {
119                 return false;
120             }
121         }
122
123         public boolean commit() throws LoginException JavaDoc {
124             subject.getPrincipals().add(new MockPrincipal(username));
125             return true;
126         }
127
128         public boolean logout() throws LoginException JavaDoc {
129             return false;
130         }
131
132         public boolean abort() throws LoginException JavaDoc {
133             return true;
134         }
135     }
136
137     private static class MockPrincipal implements Principal JavaDoc {
138         private final String JavaDoc name;
139
140         public MockPrincipal(String JavaDoc name) {
141             this.name = name;
142         }
143
144         public String JavaDoc getName() {
145             return name;
146         }
147
148         public boolean equals(Object JavaDoc o) {
149             if (this == o) return true;
150             if (!(o instanceof MockPrincipal)) return false;
151
152             final MockPrincipal mockPrincipal = (MockPrincipal) o;
153
154             if (!name.equals(mockPrincipal.name)) return false;
155
156             return true;
157         }
158
159         public int hashCode() {
160             return name.hashCode();
161         }
162     }
163 }
164
Popular Tags