View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.ksb.service.BasicAuthenticationConnectionCredentials;
28  import org.kuali.rice.ksb.service.BasicAuthenticationCredentials;
29  import org.kuali.rice.ksb.service.BasicAuthenticationService;
30  
31  /**
32   * Implements the BasicAuthenticationService
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   * @see org.kuali.rice.ksb.service.BasicAuthenticationService
36   */
37  
38  public class BasicAuthenticationServiceImpl implements BasicAuthenticationService {
39  
40      private Map<QName, List<BasicAuthenticationCredentials>> serviceCredentialsMap;
41      private Map<QName, BasicAuthenticationConnectionCredentials> connectionCredentialsMap;
42  
43      /**
44       * Constructs BasicAuthenticationServiceImpl with a serviceCredentialsMap and a connectionCredentialsMap
45       */
46      public BasicAuthenticationServiceImpl() {
47          this.serviceCredentialsMap = Collections.synchronizedMap(
48                  new HashMap<QName, List<BasicAuthenticationCredentials>>());
49          this.connectionCredentialsMap = Collections.synchronizedMap(
50                  new HashMap<QName, BasicAuthenticationConnectionCredentials>());
51      }
52  
53      public boolean checkServiceAuthentication(String serviceNameSpaceURI, QName serviceName, String username,
54              String password) {
55          List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
56          if (credentialsList != null) {
57              synchronized (credentialsList) {
58                  for (BasicAuthenticationCredentials credentials : credentialsList) {
59                      if (StringUtils.equals(username, credentials.getUsername()) && StringUtils.equals(
60                              serviceNameSpaceURI, credentials.getServiceNameSpaceURI())) {
61                          return StringUtils.equals(password, credentials.getPassword());
62                      }
63                  }
64              }
65          }
66          return false;
67      }
68  
69      public String getPasswordForService(String serviceNameSpaceURI, QName serviceName, String username) {
70          List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
71          if (credentialsList != null) {
72              synchronized (credentialsList) {
73                  for (BasicAuthenticationCredentials credentials : credentialsList) {
74                      if (StringUtils.equals(username, credentials.getUsername()) && StringUtils.equals(
75                              serviceNameSpaceURI, credentials.getServiceNameSpaceURI())) {
76                          return credentials.getPassword();
77                      }
78                  }
79              }
80          }
81          return null;
82      }
83  
84      public BasicAuthenticationConnectionCredentials getConnectionCredentials(String serviceNameSpaceURI,
85              String serviceName) {
86          return connectionCredentialsMap.get(new QName(serviceNameSpaceURI, serviceName));
87      }
88  
89      public void registerServiceCredentials(BasicAuthenticationCredentials credentials) {
90          synchronized (serviceCredentialsMap) {
91              QName serviceName = new QName(credentials.getServiceNameSpaceURI(), credentials.getLocalServiceName());
92              List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
93              if (credentialsList == null) {
94                  credentialsList = Collections.synchronizedList(new ArrayList<BasicAuthenticationCredentials>());
95                  serviceCredentialsMap.put(serviceName, credentialsList);
96              }
97              credentialsList.add(credentials);
98          }
99      }
100 
101     public void registerConnectionCredentials(BasicAuthenticationConnectionCredentials credentials) {
102         synchronized (connectionCredentialsMap) {
103             connectionCredentialsMap.put(new QName(credentials.getServiceNameSpaceURI(),
104                     credentials.getLocalServiceName()), credentials);
105         }
106     }
107 
108 }