001/**
002 * Copyright 2005-2016 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 BasicAuthenticationConnectionCredentials getConnectionCredentials(String serviceNameSpaceURI,
070            String serviceName) {
071        return connectionCredentialsMap.get(new QName(serviceNameSpaceURI, serviceName));
072    }
073
074    public void registerServiceCredentials(BasicAuthenticationCredentials credentials) {
075        synchronized (serviceCredentialsMap) {
076            QName serviceName = new QName(credentials.getServiceNameSpaceURI(), credentials.getLocalServiceName());
077            List<BasicAuthenticationCredentials> credentialsList = serviceCredentialsMap.get(serviceName);
078            if (credentialsList == null) {
079                credentialsList = Collections.synchronizedList(new ArrayList<BasicAuthenticationCredentials>());
080                serviceCredentialsMap.put(serviceName, credentialsList);
081            }
082            credentialsList.add(credentials);
083        }
084    }
085
086    public void registerConnectionCredentials(BasicAuthenticationConnectionCredentials credentials) {
087        synchronized (connectionCredentialsMap) {
088            connectionCredentialsMap.put(new QName(credentials.getServiceNameSpaceURI(),
089                    credentials.getLocalServiceName()), credentials);
090        }
091    }
092
093}