001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.service.impl;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.xml.namespace.QName;
025
026import org.apache.commons.lang.StringUtils;
027import org.kuali.rice.ksb.service.BasicAuthenticationConnectionCredentials;
028import org.kuali.rice.ksb.service.BasicAuthenticationCredentials;
029import org.kuali.rice.ksb.service.BasicAuthenticationService;
030
031/**
032 * Implements the BasicAuthenticationService
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 * @see org.kuali.rice.ksb.service.BasicAuthenticationService
036 */
037
038public class BasicAuthenticationServiceImpl implements BasicAuthenticationService {
039
040    private Map<QName, List<BasicAuthenticationCredentials>> serviceCredentialsMap;
041    private Map<QName, BasicAuthenticationConnectionCredentials> connectionCredentialsMap;
042
043    /**
044     * Constructs BasicAuthenticationServiceImpl with a serviceCredentialsMap and a connectionCredentialsMap
045     */
046    public BasicAuthenticationServiceImpl() {
047        this.serviceCredentialsMap = Collections.synchronizedMap(
048                new HashMap<QName, List<BasicAuthenticationCredentials>>());
049        this.connectionCredentialsMap = Collections.synchronizedMap(
050                new HashMap<QName, BasicAuthenticationConnectionCredentials>());
051    }
052
053    public boolean checkServiceAuthentication(String serviceNameSpaceURI, QName serviceName, String username,
054            String password) {
055        List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
056        if (credentialsList != null) {
057            synchronized (credentialsList) {
058                for (BasicAuthenticationCredentials credentials : credentialsList) {
059                    if (StringUtils.equals(username, credentials.getUsername()) && StringUtils.equals(
060                            serviceNameSpaceURI, credentials.getServiceNameSpaceURI())) {
061                        return StringUtils.equals(password, credentials.getPassword());
062                    }
063                }
064            }
065        }
066        return false;
067    }
068
069    public String getPasswordForService(String serviceNameSpaceURI, QName serviceName, String username) {
070        List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
071        if (credentialsList != null) {
072            synchronized (credentialsList) {
073                for (BasicAuthenticationCredentials credentials : credentialsList) {
074                    if (StringUtils.equals(username, credentials.getUsername()) && StringUtils.equals(
075                            serviceNameSpaceURI, credentials.getServiceNameSpaceURI())) {
076                        return credentials.getPassword();
077                    }
078                }
079            }
080        }
081        return null;
082    }
083
084    public BasicAuthenticationConnectionCredentials getConnectionCredentials(String serviceNameSpaceURI,
085            String serviceName) {
086        return connectionCredentialsMap.get(new QName(serviceNameSpaceURI, serviceName));
087    }
088
089    public void registerServiceCredentials(BasicAuthenticationCredentials credentials) {
090        synchronized (serviceCredentialsMap) {
091            QName serviceName = new QName(credentials.getServiceNameSpaceURI(), credentials.getLocalServiceName());
092            List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
093            if (credentialsList == null) {
094                credentialsList = Collections.synchronizedList(new ArrayList<BasicAuthenticationCredentials>());
095                serviceCredentialsMap.put(serviceName, credentialsList);
096            }
097            credentialsList.add(credentials);
098        }
099    }
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}