001/*
002 * Copyright 2019-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2019-2022 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2019-2022 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk;
037
038
039
040import java.util.ArrayList;
041import java.util.List;
042
043import com.unboundid.asn1.ASN1OctetString;
044import com.unboundid.util.NotNull;
045import com.unboundid.util.Nullable;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class provides an implementation of the SCRAM-SHA-1 SASL mechanism as
053 * described in <A HREF="http://www.ietf.org/rfc/rfc5802.txt">RFC 5802</A>.
054 */
055@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
056public final class SCRAMSHA1BindRequest
057       extends SCRAMBindRequest
058{
059  /**
060   * The name for the SCRAM-SHA-1 SASL mechanism.
061   */
062  @NotNull public static final String SCRAM_SHA_1_MECHANISM_NAME =
063       "SCRAM-SHA-1";
064
065
066
067  /**
068   * The serial version UID for this serializable class.
069   */
070  private static final long serialVersionUID = -1807244826649889525L;
071
072
073
074  /**
075   * Creates a new SCRAM-SHA-1 bind request with the provided information.
076   *
077   * @param username The username for this bind request.  It must not be {@code
078   *                 null} or empty.
079   * @param password The password for this bind request.  It must not be {@code
080   *                 null} or empty.
081   * @param controls The set of controls to include in the bind request.  It may
082   *                 be {@code null} or empty if no controls are needed.
083   */
084  public SCRAMSHA1BindRequest(@NotNull final String username,
085                              @NotNull final String password,
086                              @Nullable final Control... controls)
087  {
088    super(username, new ASN1OctetString(password), controls);
089  }
090
091
092
093  /**
094   * Creates a new SCRAM-SHA-1 bind request with the provided information.
095   *
096   * @param username The username for this bind request.  It must not be {@code
097   *                 null} or empty.
098   * @param password The password for this bind request.  It must not be {@code
099   *                 null} or empty.
100   * @param controls The set of controls to include in the bind request.  It may
101   *                 be {@code null} or empty if no controls are needed.
102   */
103  public SCRAMSHA1BindRequest(@NotNull final String username,
104                              @NotNull final byte[] password,
105                              @Nullable final Control... controls)
106  {
107    super(username, new ASN1OctetString(password), controls);
108  }
109
110
111
112  /**
113   * {@inheritDoc}
114   */
115  @Override()
116  @NotNull()
117  public String getSASLMechanismName()
118  {
119    return SCRAM_SHA_1_MECHANISM_NAME;
120  }
121
122
123
124  /**
125   * {@inheritDoc}
126   */
127  @Override()
128  @NotNull()
129  protected String getDigestAlgorithmName()
130  {
131    return "SHA-1";
132  }
133
134
135
136  /**
137   * {@inheritDoc}
138   */
139  @Override()
140  @NotNull()
141  protected String getMACAlgorithmName()
142  {
143    return "HmacSHA1";
144  }
145
146
147
148  /**
149   * {@inheritDoc}
150   */
151  @Override()
152  @NotNull()
153  public SCRAMSHA1BindRequest getRebindRequest(@NotNull final String host,
154                                               final int port)
155  {
156    return duplicate();
157  }
158
159
160
161  /**
162   * {@inheritDoc}
163   */
164  @Override()
165  @NotNull()
166  public SCRAMSHA1BindRequest duplicate()
167  {
168    return duplicate(getControls());
169  }
170
171
172
173  /**
174   * {@inheritDoc}
175   */
176  @Override()
177  @NotNull()
178  public SCRAMSHA1BindRequest duplicate(@Nullable final Control[] controls)
179  {
180    return new SCRAMSHA1BindRequest(getUsername(), getPasswordBytes(),
181         controls);
182  }
183
184
185
186  /**
187   * {@inheritDoc}
188   */
189  @Override()
190  public void toString(@NotNull final StringBuilder buffer)
191  {
192    buffer.append("SCRAMSHA1BindRequest(username='");
193    buffer.append(getUsername());
194    buffer.append('\'');
195
196    final Control[] controls = getControls();
197    if (controls.length > 0)
198    {
199      buffer.append(", controls={");
200      for (int i=0; i < controls.length; i++)
201      {
202        if (i > 0)
203        {
204          buffer.append(", ");
205        }
206
207        buffer.append(controls[i]);
208      }
209      buffer.append('}');
210    }
211
212    buffer.append(')');
213  }
214
215
216
217  /**
218   * {@inheritDoc}
219   */
220  @Override()
221  public void toCode(@NotNull final List<String> lineList,
222                     @NotNull final String requestID,
223                     final int indentSpaces, final boolean includeProcessing)
224  {
225    // Create the request variable.
226    final List<ToCodeArgHelper> constructorArgs = new ArrayList<>(4);
227    constructorArgs.add(ToCodeArgHelper.createString(getUsername(),
228         "Username"));
229    constructorArgs.add(ToCodeArgHelper.createString("---redacted-password---",
230         "Password"));
231
232    final Control[] controls = getControls();
233    if (controls.length > 0)
234    {
235      constructorArgs.add(ToCodeArgHelper.createControlArray(controls,
236           "Bind Controls"));
237    }
238
239    ToCodeHelper.generateMethodCall(lineList, indentSpaces,
240         "SCRAMSHA1BindRequest", requestID + "Request",
241         "new SCRAMSHA1BindRequest", constructorArgs);
242
243
244    // Add lines for processing the request and obtaining the result.
245    if (includeProcessing)
246    {
247      // Generate a string with the appropriate indent.
248      final StringBuilder buffer = new StringBuilder();
249      for (int i=0; i < indentSpaces; i++)
250      {
251        buffer.append(' ');
252      }
253      final String indent = buffer.toString();
254
255      lineList.add("");
256      lineList.add(indent + "try");
257      lineList.add(indent + '{');
258      lineList.add(indent + "  BindResult " + requestID +
259           "Result = connection.bind(" + requestID + "Request);");
260      lineList.add(indent + "  // The bind was processed successfully.");
261      lineList.add(indent + '}');
262      lineList.add(indent + "catch (LDAPException e)");
263      lineList.add(indent + '{');
264      lineList.add(indent + "  // The bind failed.  Maybe the following will " +
265           "help explain why.");
266      lineList.add(indent + "  // Note that the connection is now likely in " +
267           "an unauthenticated state.");
268      lineList.add(indent + "  ResultCode resultCode = e.getResultCode();");
269      lineList.add(indent + "  String message = e.getMessage();");
270      lineList.add(indent + "  String matchedDN = e.getMatchedDN();");
271      lineList.add(indent + "  String[] referralURLs = e.getReferralURLs();");
272      lineList.add(indent + "  Control[] responseControls = " +
273           "e.getResponseControls();");
274      lineList.add(indent + '}');
275    }
276  }
277}