001    /*
002     * Copyright 2009 Red Hat, Inc.
003     * Red Hat licenses this file to you under the Apache License, version
004     * 2.0 (the "License"); you may not use this file except in compliance
005     * with the License.  You may obtain a copy of the License at
006     *    http://www.apache.org/licenses/LICENSE-2.0
007     * Unless required by applicable law or agreed to in writing, software
008     * distributed under the License is distributed on an "AS IS" BASIS,
009     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
010     * implied.  See the License for the specific language governing
011     * permissions and limitations under the License.
012     */
013    
014    package org.hornetq.spi.core.security;
015    
016    import java.util.ArrayList;
017    import java.util.HashMap;
018    import java.util.List;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import org.hornetq.core.logging.Logger;
023    import org.hornetq.core.security.CheckType;
024    import org.hornetq.core.security.Role;
025    
026    /**
027     * A basic implementation of the HornetQSecurityManager. This can be used within an appserver and be deployed by
028     * BasicUserCredentialsDeployer or used standalone or embedded.
029     *
030     * @author <a href="ataylor@redhat.com">Andy Taylor</a>
031     */
032    public class HornetQSecurityManagerImpl implements HornetQSecurityManager
033    {
034       private static final Logger log = Logger.getLogger(HornetQSecurityManagerImpl.class);
035    
036       // Static --------------------------------------------------------
037    
038       // Attributes ----------------------------------------------------
039    
040       /**
041        * the current valid users
042        */
043       private final Map<String, User> users = new HashMap<String, User>();
044    
045       private String defaultUser = null;
046    
047       /**
048        * the roles for the users
049        */
050       private final Map<String, List<String>> roles = new HashMap<String, List<String>>();
051    
052       // HornetQComponent implementation ------------------------------------------
053    
054       public void start()
055       {
056       }
057    
058       public void stop()
059       {
060          users.clear();
061    
062          roles.clear();
063    
064          defaultUser = null;
065       }
066    
067       public boolean isStarted()
068       {
069          return true;
070       }
071    
072       // Public ---------------------------------------------------------------------
073    
074       public boolean validateUser(final String user, final String password)
075       {
076          if (user == null && defaultUser == null)
077          {
078             return false;
079          }
080    
081          User theUser = users.get(user == null ? defaultUser : user);
082    
083          boolean ok = theUser != null && theUser.isValid(user == null ? defaultUser : user, password == null ? defaultUser
084                                                                                                             : password);
085          return ok;
086       }
087    
088       public boolean validateUserAndRole(final String user,
089                                          final String password,
090                                          final Set<Role> roles,
091                                          final CheckType checkType)
092       {
093          if (validateUser(user, password))
094          {
095             List<String> availableRoles = this.roles.get(user == null ? defaultUser : user);
096    
097             if (availableRoles == null)
098             {
099                return false;
100             }
101    
102             for (String availableRole : availableRoles)
103             {
104                if (roles != null)
105                {
106                   for (Role role : roles)
107                   {
108                      if (role.getName().equals(availableRole) && checkType.hasRole(role))
109                      {
110                         return true;
111                      }
112                   }
113                }
114             }
115          }
116    
117          return false;
118       }
119    
120       public void addUser(final String user, final String password)
121       {
122          if (user == null)
123          {
124             throw new IllegalArgumentException("User cannot be null");
125          }
126          if (password == null)
127          {
128             throw new IllegalArgumentException("password cannot be null");
129          }
130          users.put(user, new User(user, password));
131       }
132    
133       public void removeUser(final String user)
134       {
135          users.remove(user);
136          roles.remove(user);
137       }
138    
139       public void addRole(final String user, final String role)
140       {
141          if (roles.get(user) == null)
142          {
143             roles.put(user, new ArrayList<String>());
144          }
145          roles.get(user).add(role);
146       }
147    
148       public void removeRole(final String user, final String role)
149       {
150          if (roles.get(user) == null)
151          {
152             return;
153          }
154          roles.get(user).remove(role);
155       }
156    
157       /*
158       * set the default user for null users
159       */
160       public void setDefaultUser(final String username)
161       {
162          defaultUser = username;
163       }
164    
165       static class User
166       {
167          final String user;
168    
169          final String password;
170    
171          User(final String user, final String password)
172          {
173             this.user = user;
174             this.password = password;
175          }
176    
177          @Override
178          public boolean equals(final Object o)
179          {
180             if (this == o)
181             {
182                return true;
183             }
184             if (o == null || getClass() != o.getClass())
185             {
186                return false;
187             }
188    
189             User user1 = (User)o;
190    
191             if (!user.equals(user1.user))
192             {
193                return false;
194             }
195    
196             return true;
197          }
198    
199          @Override
200          public int hashCode()
201          {
202             return user.hashCode();
203          }
204    
205          public boolean isValid(final String user, final String password)
206          {
207             if (user == null)
208             {
209                return false;
210             }
211             return this.user.equals(user) && this.password.equals(password);
212          }
213       }
214    }