Home » openejb-3.1.2-src » org.apache » openejb » core » security » jaas » [javadoc | source]

    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.openejb.core.security.jaas;
   18   
   19   import static org.apache.openejb.util.IOUtils.readProperties;
   20   import org.apache.openejb.util.ConfUtils;
   21   import org.apache.openejb.util.LogCategory;
   22   import org.apache.openejb.util.Logger;
   23   
   24   import javax.security.auth.Subject;
   25   import javax.security.auth.callback.Callback;
   26   import javax.security.auth.callback.CallbackHandler;
   27   import javax.security.auth.callback.NameCallback;
   28   import javax.security.auth.callback.PasswordCallback;
   29   import javax.security.auth.callback.UnsupportedCallbackException;
   30   import javax.security.auth.login.FailedLoginException;
   31   import javax.security.auth.login.LoginException;
   32   import javax.security.auth.spi.LoginModule;
   33   import java.io.IOException;
   34   import java.net.URL;
   35   import java.util.Enumeration;
   36   import java.util.Map;
   37   import java.util.Properties;
   38   import java.util.Set;
   39   import java.util.LinkedHashSet;
   40   
   41   /**
   42    * @version $Rev: 768623 $ $Date: 2009-04-25 16:05:24 -0700 (Sat, 25 Apr 2009) $
   43    */
   44   public class PropertiesLoginModule implements LoginModule {
   45   
   46       private final String USER_FILE = "UsersFile";
   47       private final String GROUP_FILE = "GroupsFile";
   48   
   49       private static Logger log = Logger.getInstance(LogCategory.OPENEJB_SECURITY, "org.apache.openejb.util.resources");
   50   
   51       private Subject subject;
   52       private CallbackHandler callbackHandler;
   53   
   54       private boolean debug;
   55       private Properties users = new Properties();
   56       private Properties groups = new Properties();
   57       private String user;
   58       private Set principals = new LinkedHashSet();
   59   
   60       private URL usersUrl;
   61       private URL groupsUrl;
   62   
   63       public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
   64           this.subject = subject;
   65           this.callbackHandler = callbackHandler;
   66   
   67           debug = log.isDebugEnabled() || "true".equalsIgnoreCase((String) options.get("Debug"));
   68           String usersFile = (String) options.get(USER_FILE) + "";
   69           String groupsFile = (String) options.get(GROUP_FILE) + "";
   70   
   71           usersUrl = ConfUtils.getConfResource(usersFile);
   72           groupsUrl = ConfUtils.getConfResource(groupsFile);
   73   
   74           if (debug){
   75               log.debug("Users file: " + usersUrl.toExternalForm());
   76               log.debug("Groups file: " + groupsUrl.toExternalForm());
   77           }
   78       }
   79   
   80       public boolean login() throws LoginException {
   81           try {
   82               users = readProperties(usersUrl);
   83           } catch (IOException ioe) {
   84               throw new LoginException("Unable to load user properties file " + usersUrl.getFile());
   85           }
   86   
   87           try {
   88               groups = readProperties(groupsUrl);
   89           } catch (IOException ioe) {
   90               throw new LoginException("Unable to load group properties file " + groupsUrl.getFile());
   91           }
   92   
   93           Callback[] callbacks = new Callback[2];
   94   
   95           callbacks[0] = new NameCallback("Username: ");
   96           callbacks[1] = new PasswordCallback("Password: ", false);
   97           try {
   98               callbackHandler.handle(callbacks);
   99           } catch (IOException ioe) {
  100               throw new LoginException(ioe.getMessage());
  101           } catch (UnsupportedCallbackException uce) {
  102               throw new LoginException(uce.getMessage() + " not available to obtain information from user");
  103           }
  104   
  105           user = ((NameCallback) callbacks[0]).getName();
  106           char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
  107           if (tmpPassword == null) tmpPassword = new char[0];
  108   
  109           String password = users.getProperty(user);
  110   
  111           if (password == null) throw new FailedLoginException("User does not exist");
  112           if (!password.equals(new String(tmpPassword))) throw new FailedLoginException("Password does not match");
  113   
  114           users.clear();
  115   
  116           if (debug) {
  117               log.debug("Logged in as '" + user+"'");
  118           }
  119           return true;
  120       }
  121   
  122       public boolean commit() throws LoginException {
  123           principals.add(new UserPrincipal(user));
  124   
  125           for (Enumeration enumeration = groups.keys(); enumeration.hasMoreElements();) {
  126               String name = (String) enumeration.nextElement();
  127               String[] userList = ((String) groups.getProperty(name) + "").split(",");
  128               for (int i = 0; i < userList.length; i++) {
  129                   if (user.equals(userList[i])) {
  130                       principals.add(new GroupPrincipal(name));
  131                       break;
  132                   }
  133               }
  134           }
  135   
  136           subject.getPrincipals().addAll(principals);
  137   
  138           clear();
  139   
  140           if (debug) {
  141               log.debug("commit");
  142           }
  143           return true;
  144       }
  145   
  146       public boolean abort() throws LoginException {
  147           clear();
  148   
  149           if (debug) {
  150               log.debug("abort");
  151           }
  152           return true;
  153       }
  154   
  155       public boolean logout() throws LoginException {
  156           subject.getPrincipals().removeAll(principals);
  157           principals.clear();
  158   
  159           if (debug) {
  160               log.debug("logout");
  161           }
  162           return true;
  163       }
  164   
  165       private void clear() {
  166           groups.clear();
  167           user = null;
  168       }
  169   
  170   }

Home » openejb-3.1.2-src » org.apache » openejb » core » security » jaas » [javadoc | source]