Home » geronimo-2.2-source-release » org.apache.geronimo.jmxremoting » [javadoc | source]

    1   /**
    2    *
    3    * Copyright 2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  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;
   20   import java.util.HashMap;
   21   import java.util.Map;
   22   import java.util.Set;
   23   import javax.security.auth.Subject;
   24   import javax.security.auth.callback.Callback;
   25   import javax.security.auth.callback.CallbackHandler;
   26   import javax.security.auth.callback.NameCallback;
   27   import javax.security.auth.callback.PasswordCallback;
   28   import javax.security.auth.callback.UnsupportedCallbackException;
   29   import javax.security.auth.login.AppConfigurationEntry;
   30   import javax.security.auth.login.Configuration;
   31   import javax.security.auth.login.LoginException;
   32   import javax.security.auth.spi.LoginModule;
   33   
   34   import junit.framework.TestCase;
   35   
   36   /**
   37    * 
   38    * 
   39    * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
   40    */
   41   public class AuthenticatorTest extends TestCase {
   42       private static final String CONFIG_NAME = "testConfig";
   43       private Configuration oldConfiguration;
   44       private Configuration loginConfig;
   45       private String[] credentials;
   46       private Authenticator authenticator;
   47   
   48       public void testAuthenticateWithValidPassword() {
   49           try {
   50               Subject s = authenticator.authenticate(credentials);
   51               Set principals = s.getPrincipals();
   52               assertTrue(principals.contains(new MockPrincipal("username")));
   53           } catch (SecurityException e) {
   54               e.printStackTrace();
   55               fail();
   56           }
   57       }
   58   
   59       protected void setUp() throws Exception {
   60           super.setUp();
   61           try {
   62               oldConfiguration = Configuration.getConfiguration();
   63           } catch (SecurityException e) {
   64               oldConfiguration = null;
   65           }
   66           loginConfig = new MockConfiguration();
   67           Configuration.setConfiguration(loginConfig);
   68   
   69           credentials = new String[]{"username", "password"};
   70           authenticator = new Authenticator(CONFIG_NAME, getClass().getClassLoader());
   71       }
   72   
   73       protected void tearDown() throws Exception {
   74           Configuration.setConfiguration(oldConfiguration);
   75           super.tearDown();
   76       }
   77   
   78       private class MockConfiguration extends Configuration {
   79           public AppConfigurationEntry[] getAppConfigurationEntry(String applicationName) {
   80               if (CONFIG_NAME.equals(applicationName) == false) {
   81                   fail();
   82               }
   83               Map map = new HashMap();
   84               map.put("username", "password");
   85               AppConfigurationEntry entry = new AppConfigurationEntry(MockModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map);
   86               return new AppConfigurationEntry[] {entry};
   87           }
   88   
   89           public void refresh() {
   90           }
   91       }
   92   
   93       public static class MockModule implements LoginModule {
   94           private Subject subject;
   95           private CallbackHandler handler;
   96           private Map options;
   97           private String username;
   98   
   99           public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
  100               this.subject = subject;
  101               this.handler = callbackHandler;
  102               this.options = options;
  103           }
  104   
  105           public boolean login() throws LoginException {
  106               NameCallback nameCallback = new NameCallback("name");
  107               PasswordCallback passwordCallback = new PasswordCallback("password", false);
  108               try {
  109                   handler.handle(new Callback[] {nameCallback, passwordCallback});
  110                   username = nameCallback.getName();
  111                   String password = (String) options.get(username);
  112                   if (password == null) {
  113                       return false;
  114                   }
  115                   return password.equals(new String(passwordCallback.getPassword()));
  116               } catch (java.io.IOException e) {
  117                   return false;
  118               } catch (UnsupportedCallbackException e) {
  119                   return false;
  120               }
  121           }
  122   
  123           public boolean commit() throws LoginException {
  124               subject.getPrincipals().add(new MockPrincipal(username));
  125               return true;
  126           }
  127   
  128           public boolean logout() throws LoginException {
  129               return false;
  130           }
  131   
  132           public boolean abort() throws LoginException {
  133               return true;
  134           }
  135       }
  136   
  137       private static class MockPrincipal implements Principal {
  138           private final String name;
  139   
  140           public MockPrincipal(String name) {
  141               this.name = name;
  142           }
  143   
  144           public String getName() {
  145               return name;
  146           }
  147   
  148           public boolean equals(Object 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   }

Home » geronimo-2.2-source-release » org.apache.geronimo.jmxremoting » [javadoc | source]