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.Arrays;
020import java.util.Map;
021import java.util.Objects;
022import java.util.TreeMap;
023
024/**
025 * Contains various authentication data.
026 */
027public class UserAuthenticationData {
028
029    /**
030     * Represents a user authentication item.
031     */
032    public static class Type implements Comparable<Type> {
033
034        /** The type name */
035        private final String type;
036
037        /**
038         * Creates a new Type.
039         *
040         * @param type the type
041         */
042        public Type(final String type) {
043            this.type = type;
044        }
045
046        @Override
047        public int compareTo(final Type o) {
048            return type.compareTo(o.type);
049        }
050
051        @Override
052        public boolean equals(final Object o) {
053            if (this == o) {
054                return true;
055            }
056            if (o == null || getClass() != o.getClass()) {
057                return false;
058            }
059            return Objects.equals(type, ((Type) o).type);
060        }
061
062        /**
063         * @return The hash code.
064         * @since 2.0
065         */
066        @Override
067        public int hashCode() {
068            return type != null ? type.hashCode() : 0;
069        }
070
071        /**
072         * @return The type.
073         * @since 2.0
074         */
075        @Override
076        public String toString() {
077            return type;
078        }
079    }
080
081    /** The user name. */
082    public static final Type USERNAME = new Type("username");
083
084    /** The password. */
085    public static final Type PASSWORD = new Type("password");
086
087    /** The user's domain. */
088    public static final Type DOMAIN = new Type("domain");
089
090    /** The authentication data. */
091    private final Map<Type, char[]> authenticationData = new TreeMap<>();
092
093    /**
094     * Creates a new uninitialized instance.
095     */
096    public UserAuthenticationData() {
097        // do nothing
098    }
099
100    /**
101     * Deletes all data stored within this authenticator.
102     */
103    public void cleanup() {
104        // step 1: nullify character buffers
105        for (final char[] data : authenticationData.values()) {
106            if (data == null) {
107                continue;
108            }
109
110            Arrays.fill(data, (char) 0);
111        }
112        // step 2: allow data itself to gc
113        authenticationData.clear();
114    }
115
116    /**
117     * Gets a data from the collection.
118     *
119     * @param type The Type to retrieve.
120     * @return a character array containing the data associated with the type.
121     */
122    public char[] getData(final Type type) {
123        return authenticationData.get(type);
124    }
125
126    /**
127     * Sets a data to this collection.
128     *
129     * @param type The Type to add
130     * @param data The data associated with the Type
131     */
132    public void setData(final Type type, final char[] data) {
133        authenticationData.put(type, data);
134    }
135}