001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.server.config.beans;
021
022
023import java.util.Arrays;
024import java.util.HashSet;
025import java.util.Set;
026
027
028import org.apache.directory.server.config.ConfigurationElement;
029
030
031/**
032 * A bean used to store the hash interceptor configuration
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class HashInterceptorBean extends InterceptorBean
037{
038    /** The hash algorithm */
039    @ConfigurationElement(attributeType = "ads-hashAlgorithm", isOptional = true, defaultValue = "SSHA-256" )
040    private String hashAlgorithm;
041
042    /** The reference to the Password Policy component */
043    @ConfigurationElement(attributeType = "ads-hashAttribute", isOptional = true, defaultValues = {"2.5.4.35"} )
044    private Set<String> hashAttributes = new HashSet<>();
045
046
047    /**
048     * Creates a new AuthenticationInterceptorBean instance
049     */
050    public HashInterceptorBean()
051    {
052        super();
053    }
054
055
056    /**
057     * @param hashAttributes The attributes that need to be hashed
058     */
059    public void addHashAttributes( String[] hashAttributes )
060    {
061        if ( hashAttributes != null && hashAttributes.length > 0 ) 
062        {
063            if ( this.hashAttributes == null ) 
064            {
065                this.hashAttributes = new HashSet<>();
066            }
067            this.hashAttributes.addAll( Arrays.asList( hashAttributes ) );
068        }
069    }
070
071
072    /**
073     * @return the hash algorithm
074     */
075    public String getHashAlgorithm()
076    {
077        return hashAlgorithm;
078    }
079
080
081    /**
082     * @return the attributes to hash
083     */
084    public Set<String> getHashAttributes()
085    {
086        return hashAttributes;
087    }
088
089
090    /**
091     * @param hashAlgorithm The hash algorithm to use
092     */
093    public void setHashAlgorithm( String hashAlgorithm )
094    {
095        this.hashAlgorithm = hashAlgorithm;
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public String toString( String tabs )
104    {
105        StringBuilder sb = new StringBuilder();
106
107        sb.append( tabs ).append( "HashInterceptor :\n" );
108        sb.append( super.toString( tabs + "  " ) );
109
110        if ( hashAlgorithm != null )
111        {
112            sb.append( tabs ).append( "  hashAlgorithm : " )
113                    .append( hashAlgorithm ).append( "\n" );
114        }
115        if ( ( hashAttributes != null ) && !hashAttributes.isEmpty() )
116        {
117            sb.append( tabs ).append( "  hashAttributes :\n" );
118
119            for ( String hashAttribute : hashAttributes )
120            {
121                sb.append( tabs ).append( "    " ).append( hashAttribute );
122            }
123        }
124
125        return sb.toString();
126    }
127}