001/*
002 * Copyright 2017-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2017-2019 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.listener;
022
023
024
025import java.util.Arrays;
026import java.util.List;
027
028import com.unboundid.ldap.sdk.LDAPException;
029import com.unboundid.ldap.sdk.Modification;
030import com.unboundid.ldap.sdk.ReadOnlyEntry;
031import com.unboundid.util.ThreadSafety;
032import com.unboundid.util.ThreadSafetyLevel;
033
034
035
036/**
037 * This class provides an implementation of an in-memory directory server
038 * password encoder that leaves the password in the clear.  This doesn't provide
039 * any more protection than leaving passwords unencoded, but it does make it
040 * possible to store these passwords with a prefix, and to use an optional
041 * output format (e.g., to format the clear-text value in base64 or
042 * hexadecimal).
043 */
044@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
045public final class ClearInMemoryPasswordEncoder
046       extends InMemoryPasswordEncoder
047{
048  /**
049   * Creates a new instance of this in-memory directory server password encoder
050   * with the provided information.
051   *
052   * @param  prefix           The string that will appear at the beginning of
053   *                          encoded passwords.  It must not be {@code null} or
054   *                          empty.
055   * @param  outputFormatter  The output formatter that will be used to format
056   *                          the encoded representation of clear-text
057   *                          passwords.  It may be {@code null} if no
058   *                          special formatting should be applied to the raw
059   *                          bytes.
060   */
061  public ClearInMemoryPasswordEncoder(final String prefix,
062              final PasswordEncoderOutputFormatter outputFormatter)
063  {
064    super(prefix, outputFormatter);
065  }
066
067
068
069  /**
070   * {@inheritDoc}
071   */
072  @Override()
073  protected byte[] encodePassword(final byte[] clearPassword,
074                                  final ReadOnlyEntry userEntry,
075                                  final List<Modification> modifications)
076            throws LDAPException
077  {
078    return clearPassword;
079  }
080
081
082
083  /**
084   * {@inheritDoc}
085   */
086  @Override()
087  protected void ensurePreEncodedPasswordAppearsValid(
088                      final byte[] unPrefixedUnFormattedEncodedPasswordBytes,
089                      final ReadOnlyEntry userEntry,
090                      final List<Modification> modifications)
091            throws LDAPException
092  {
093    // No validation is required.
094  }
095
096
097
098  /**
099   * {@inheritDoc}
100   */
101  @Override()
102  protected boolean passwordMatches(final byte[] clearPasswordBytes,
103                         final byte[] unPrefixedUnFormattedEncodedPasswordBytes,
104                         final ReadOnlyEntry userEntry)
105            throws LDAPException
106  {
107    return Arrays.equals(clearPasswordBytes,
108         unPrefixedUnFormattedEncodedPasswordBytes);
109  }
110
111
112
113  /**
114   * {@inheritDoc}
115   */
116  @Override()
117  protected byte[] extractClearPassword(
118                 final byte[] unPrefixedUnFormattedEncodedPasswordBytes,
119                 final ReadOnlyEntry userEntry)
120            throws LDAPException
121  {
122    return unPrefixedUnFormattedEncodedPasswordBytes;
123  }
124
125
126
127  /**
128   * {@inheritDoc}
129   */
130  @Override()
131  public void toString(final StringBuilder buffer)
132  {
133    buffer.append("ClearInMemoryPasswordEncoder(prefix='");
134    buffer.append(getPrefix());
135    buffer.append("', outputFormatter=");
136
137    final PasswordEncoderOutputFormatter outputFormatter =
138         getOutputFormatter();
139    if (outputFormatter == null)
140    {
141      buffer.append("null");
142    }
143    else
144    {
145      outputFormatter.toString(buffer);
146    }
147
148    buffer.append(')');
149  }
150}