001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import java.util.Map;
020import java.util.Objects;
021import java.util.TreeMap;
022
023import org.apache.commons.lang3.ArrayFill;
024
025/**
026 * Contains various authentication data.
027 */
028public class UserAuthenticationData {
029
030    /**
031     * Represents a user authentication item.
032     */
033    public static class Type implements Comparable<Type> {
034
035        /** The type name */
036        private final String type;
037
038        /**
039         * Creates a new Type.
040         *
041         * @param type the type
042         */
043        public Type(final String type) {
044            this.type = type;
045        }
046
047        @Override
048        public int compareTo(final Type o) {
049            return type.compareTo(o.type);
050        }
051
052        @Override
053        public boolean equals(final Object o) {
054            if (this == o) {
055                return true;
056            }
057            if (o == null || getClass() != o.getClass()) {
058                return false;
059            }
060            return Objects.equals(type, ((Type) o).type);
061        }
062
063        /**
064         * @return The hash code.
065         * @since 2.0
066         */
067        @Override
068        public int hashCode() {
069            return type != null ? type.hashCode() : 0;
070        }
071
072        /**
073         * @return The type.
074         * @since 2.0
075         */
076        @Override
077        public String toString() {
078            return type;
079        }
080    }
081
082    /** The user name. */
083    public static final Type USERNAME = new Type("username");
084
085    /** The password. */
086    public static final Type PASSWORD = new Type("password");
087
088    /** The user's domain. */
089    public static final Type DOMAIN = new Type("domain");
090
091    /** The authentication data. */
092    private final Map<Type, char[]> authenticationData = new TreeMap<>();
093
094    /**
095     * Creates a new uninitialized instance.
096     */
097    public UserAuthenticationData() {
098        // do nothing
099    }
100
101    /**
102     * Deletes all data stored within this authenticator.
103     */
104    public void cleanup() {
105        // step 1: nullify character buffers
106        for (final char[] data : authenticationData.values()) {
107            ArrayFill.fill(data, (char) 0);
108        }
109        // step 2: allow data itself to GC
110        authenticationData.clear();
111    }
112
113    /**
114     * Gets a data from the collection.
115     *
116     * @param type The Type to retrieve.
117     * @return a character array containing the data associated with the type.
118     */
119    public char[] getData(final Type type) {
120        return authenticationData.get(type);
121    }
122
123    /**
124     * Sets a data to this collection.
125     *
126     * @param type The Type to add
127     * @param data The data associated with the Type
128     */
129    public void setData(final Type type, final char[] data) {
130        authenticationData.put(type, data);
131    }
132}