View Javadoc
1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ws.security;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.NodeList;
27  
28  
29  /**
30   * Since we don't have an authentication service, this is a stupid class that  
31   * verifies passwords and returns person ids based on a ks.users.xml file loaded from the classpath.
32   *
33   */
34  public class AuthenticationService {
35  
36      private static Map<String, String> passwords;
37      private static Map<String, String> personids;
38      
39      //Load personids and passwords from ks.users.xml file into a hash
40      static {
41          passwords = new HashMap<String, String>();
42          personids = new HashMap<String, String>();
43          
44          try {
45              DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
46              DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
47              Document doc = docBuilder.parse(AuthenticationService.class.getClassLoader().getResourceAsStream("ks.users.xml"));        
48              NodeList users = doc.getElementsByTagName("user");
49              for(int s=0; s < users.getLength() ; s++){
50                  Element user = (Element)users.item(s);
51                  passwords.put(user.getAttribute("username"), user.getAttribute("password"));
52                  personids.put(user.getAttribute("username"), user.getAttribute("personid"));
53              }
54          } catch (Exception e){
55          }
56      }
57      
58      public static boolean validateUsernamePassword(String username, String password){
59          return password.equals(passwords.get(username));
60      }
61      
62      public static String getPasswordForUsername(String username){
63          return passwords.get(username);
64      }
65      
66      public static String getPersonIdForUsername(String username){
67          return personids.get(username);
68      }
69  }