001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.ametys.core.authentication;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.component.Component;
021
022import org.ametys.runtime.plugin.component.AbstractLogEnabled;
023
024/**
025 * Abstract implementation of {@link CredentialProvider}, which is configurable.
026 * Extends this class for implementing a CredentialProvider (and implement {@link NonBlockingCredentialProvider},
027 * {@link BlockingCredentialProvider} or both)
028 */
029public abstract class AbstractCredentialProvider extends AbstractLogEnabled implements CredentialProvider, Component
030{
031    private String _cpModelId;
032    private Map<String, Object> _paramValues;
033    private String _label;
034    private String _id;
035    
036    public String getId()
037    {
038        return _id;
039    }
040    
041    @Override
042    public String getLabel()
043    {
044        return _label;
045    }
046    
047    @Override
048    public String getCredentialProviderModelId()
049    {
050        return _cpModelId;
051    }
052
053    @Override
054    public Map<String, Object> getParameterValues()
055    {
056        return _paramValues;
057    }
058
059    @Override
060    public void init(String id, String cpModelId, Map<String, Object> paramValues, String label)
061    {
062        _id = id;
063        _cpModelId = cpModelId;
064        _paramValues = paramValues;
065        _label = label;
066    }
067    
068    @Override
069    public boolean equals(Object obj)
070    {
071        if (this == obj)
072        {
073            return true;
074        }
075        if (obj == null)
076        {
077            return false;
078        }
079        if (!(obj instanceof CredentialProvider))
080        {
081            return false;
082        }
083        
084        CredentialProvider secondCp = (CredentialProvider) obj;
085        // Two credential providers are said equals if and only if their model id is equal and their parameter values are equals
086        return _cpModelId.equals(secondCp.getCredentialProviderModelId()) && _paramValues.equals(secondCp.getParameterValues());
087    }
088    
089    @Override
090    public int hashCode()
091    {
092        final int prime = 31;
093        int result = 1;
094        result = prime * result + ((_cpModelId == null) ? 0 : _cpModelId.hashCode());
095        result = prime * result + ((_paramValues == null) ? 0 : _paramValues.hashCode());
096        return result;
097    }
098    
099}