001/*
002 * Copyright 2014-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2014-2024 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) 2014-2024 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.io.Serializable;
041import java.util.ArrayList;
042import java.util.Collections;
043import java.util.List;
044
045import com.unboundid.asn1.ASN1OctetString;
046import com.unboundid.util.Mutable;
047import com.unboundid.util.NotNull;
048import com.unboundid.util.Nullable;
049import com.unboundid.util.StaticUtils;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052import com.unboundid.util.Validator;
053
054
055
056/**
057 * This class provides a data structure that may be used to hold a number of
058 * properties that may be used during processing for a SASL DIGEST-MD5 bind
059 * operation.
060 */
061@Mutable()
062@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
063public final class DIGESTMD5BindRequestProperties
064       implements Serializable
065{
066  /**
067   * The serial version UID for this serializable class.
068   */
069  private static final long serialVersionUID = -2000440962628192477L;
070
071
072
073  // The password for the DIGEST-MD5 bind request.
074  @NotNull private ASN1OctetString password;
075
076  // The SASL quality of protection value(s) allowed for the DIGEST-MD5 bind
077  // request.
078  @NotNull private List<SASLQualityOfProtection> allowedQoP;
079
080  // The authentication ID string for the DIGEST-MD5 bind request.
081  @NotNull private String authenticationID;
082
083  // The authorization ID string for the DIGEST-MD5 bind request, if available.
084  @Nullable private String authorizationID;
085
086  // The realm for the DIGEST-MD5 bind request, if available.
087  @Nullable private String realm;
088
089
090
091  /**
092   * Creates a new set of DIGEST-MD5 bind request properties with the provided
093   * information.
094   *
095   * @param  authenticationID  The authentication ID for the DIGEST-MD5 bind
096   *                           request.  It must not be {@code null}.
097   * @param  password          The password for the DIGEST-MD5 bind request.  It
098   *                           may be {@code null} if anonymous authentication
099   *                           is to be performed.
100   */
101  public DIGESTMD5BindRequestProperties(@NotNull final String authenticationID,
102                                        @Nullable final String password)
103  {
104    this(authenticationID, new ASN1OctetString(password));
105  }
106
107
108
109  /**
110   * Creates a new set of DIGEST-MD5 bind request properties with the provided
111   * information.
112   *
113   * @param  authenticationID  The authentication ID for the DIGEST-MD5 bind
114   *                           request.  It must not be {@code null}.
115   * @param  password          The password for the DIGEST-MD5 bind request.  It
116   *                           may be {@code null} if anonymous authentication
117   *                           is to be performed.
118   */
119  public DIGESTMD5BindRequestProperties(@NotNull final String authenticationID,
120                                        @Nullable final byte[] password)
121  {
122    this(authenticationID, new ASN1OctetString(password));
123  }
124
125
126
127  /**
128   * Creates a new set of DIGEST-MD5 bind request properties with the provided
129   * information.
130   *
131   * @param  authenticationID  The authentication ID for the DIGEST-MD5 bind
132   *                           request.  It must not be {@code null}.
133   * @param  password          The password for the DIGEST-MD5 bind request.  It
134   *                           may be {@code null} if anonymous authentication
135   *                           is to be performed.
136   */
137  public DIGESTMD5BindRequestProperties(@NotNull final String authenticationID,
138              @Nullable final ASN1OctetString password)
139  {
140    Validator.ensureNotNull(authenticationID);
141
142    this.authenticationID = authenticationID;
143
144    if (password == null)
145    {
146      this.password = new ASN1OctetString();
147    }
148    else
149    {
150      this.password = password;
151    }
152
153    authorizationID = null;
154    realm           = null;
155    allowedQoP      = Collections.singletonList(SASLQualityOfProtection.AUTH);
156  }
157
158
159
160  /**
161   * Retrieves the authentication ID for the DIGEST-MD5 bind request.
162   *
163   * @return  The authentication ID for the DIGEST-MD5 bind request.
164   */
165  @NotNull()
166  public String getAuthenticationID()
167  {
168    return authenticationID;
169  }
170
171
172
173  /**
174   * Specifies the authentication ID for the DIGEST-MD5 bind request.  It must
175   * not be {@code null}, and should generally start with "dn:" followed by the
176   * full DN for the target user (or just "dn:" for anonymous), or "u:" followed
177   * by the username for the target user.
178   *
179   * @param  authenticationID  The authentication ID for the DIGEST-MD5 bind
180   *                           request.  It must not be {@code null}.
181   */
182  public void setAuthenticationID(@NotNull final String authenticationID)
183  {
184    Validator.ensureNotNull(authenticationID);
185    this.authenticationID = authenticationID;
186  }
187
188
189
190  /**
191   * Retrieves the authorization ID for the DIGEST-MD5 bind request.
192   *
193   * @return  The authorization ID for the DIGEST-MD5 bind request, or
194   *          {@code null} if no authorization ID should be included in the
195   *          bind request.
196   */
197  @Nullable()
198  public String getAuthorizationID()
199  {
200    return authorizationID;
201  }
202
203
204
205  /**
206   * Specifies the authorization ID for the DIGEST-MD5 bind request.  It may be
207   * {@code null} if not alternate authorization identity is needed.  If
208   * provided, the authorization ID should generally start with "dn:" followed
209   * by the full DN for the target user (or just "dn:" for anonymous), or "u:"
210   * followed by the username for the target user.
211   *
212   * @param  authorizationID  The authorization ID for the DIGEST-MD5 bind
213   *                          request.
214   */
215  public void setAuthorizationID(@Nullable final String authorizationID)
216  {
217    this.authorizationID = authorizationID;
218  }
219
220
221
222  /**
223   * Retrieves the password for the DIGEST-MD5 bind request.
224   *
225   * @return  The password for the DIGEST-MD5 bind request.
226   */
227  @NotNull()
228  public ASN1OctetString getPassword()
229  {
230    return password;
231  }
232
233
234
235  /**
236   * Specifies the password for the DIGEST-MD5 bind request.  It may be
237   * {@code null} or empty when authenticating as the anonymous user.
238   *
239   * @param  password  The password for the DIGEST-MD5 bind request.  It may be
240   *                   {@code null} or empty when authenticating as the
241   *                   anonymous user.
242   */
243  public void setPassword(@NotNull final String password)
244  {
245    setPassword(new ASN1OctetString(password));
246  }
247
248
249
250  /**
251   * Specifies the password for the DIGEST-MD5 bind request.  It may be
252   * {@code null} or empty when authenticating as the anonymous user.
253   *
254   * @param  password  The password for the DIGEST-MD5 bind request.  It may be
255   *                   {@code null} or empty when authenticating as the
256   *                   anonymous user.
257   */
258  public void setPassword(@NotNull final byte[] password)
259  {
260    setPassword(new ASN1OctetString(password));
261  }
262
263
264
265  /**
266   * Specifies the password for the DIGEST-MD5 bind request.  It may be
267   * {@code null} or empty when authenticating as the anonymous user.
268   *
269   * @param  password  The password for the DIGEST-MD5 bind request.  It may be
270   *                   {@code null} or empty when authenticating as the
271   *                   anonymous user.
272   */
273  public void setPassword(@Nullable final ASN1OctetString password)
274  {
275    if (password == null)
276    {
277      this.password = new ASN1OctetString();
278    }
279    else
280    {
281      this.password = password;
282    }
283  }
284
285
286
287  /**
288   * Retrieves the realm for the DIGEST-MD5 bind request.
289   *
290   * @return  The realm for the DIGEST-MD5 bind request, or {@code null} if no
291   *          realm should be included in the bind request.
292   */
293  @Nullable()
294  public String getRealm()
295  {
296    return realm;
297  }
298
299
300
301  /**
302   * Specifies the realm for the DIGEST-MD5 bind request.  It may be
303   * {@code null} if no realm should be included in the bind request.
304   *
305   * @param  realm  The realm for the DIGEST-MD5 bind request.  It may be
306   *                {@code null} if no realm should be included in the bind
307   *                request.
308   */
309  public void setRealm(@Nullable final String realm)
310  {
311    this.realm = realm;
312  }
313
314
315
316  /**
317   * Retrieves the list of allowed qualities of protection that may be used for
318   * communication that occurs on the connection after the authentication has
319   * completed, in order from most preferred to least preferred.
320   *
321   * @return  The list of allowed qualities of protection that may be used for
322   *          communication that occurs on the connection after the
323   *          authentication has completed, in order from most preferred to
324   *          least preferred.
325   */
326  @NotNull()
327  public List<SASLQualityOfProtection> getAllowedQoP()
328  {
329    return allowedQoP;
330  }
331
332
333
334  /**
335   * Specifies the list of allowed qualities of protection that may be used for
336   * communication that occurs on the connection after the authentication has
337   * completed, in order from most preferred to least preferred.
338   *
339   * @param  allowedQoP  The list of allowed qualities of protection that may be
340   *                     used for communication that occurs on the connection
341   *                     after the authentication has completed, in order from
342   *                     most preferred to least preferred.  If this is
343   *                     {@code null} or empty, then a list containing only the
344   *                     {@link SASLQualityOfProtection#AUTH} quality of
345   *                     protection value will be used.
346   */
347  public void setAllowedQoP(
348                   @Nullable final List<SASLQualityOfProtection> allowedQoP)
349  {
350    if ((allowedQoP == null) || allowedQoP.isEmpty())
351    {
352      this.allowedQoP = Collections.singletonList(SASLQualityOfProtection.AUTH);
353    }
354    else
355    {
356      this.allowedQoP =
357           Collections.unmodifiableList(new ArrayList<>(allowedQoP));
358    }
359  }
360
361
362
363  /**
364   * Specifies the list of allowed qualities of protection that may be used for
365   * communication that occurs on the connection after the authentication has
366   * completed, in order from most preferred to least preferred.
367   *
368   * @param  allowedQoP  The list of allowed qualities of protection that may be
369   *                     used for communication that occurs on the connection
370   *                     after the authentication has completed, in order from
371   *                     most preferred to least preferred.  If this is
372   *                     {@code null} or empty, then a list containing only the
373   *                     {@link SASLQualityOfProtection#AUTH} quality of
374   *                     protection value will be used.
375   */
376  public void setAllowedQoP(
377                   @Nullable final SASLQualityOfProtection... allowedQoP)
378  {
379    setAllowedQoP(StaticUtils.toList(allowedQoP));
380  }
381
382
383
384  /**
385   * Retrieves a string representation of the DIGEST-MD5 bind request
386   * properties.
387   *
388   * @return  A string representation of the DIGEST-MD5 bind request properties.
389   */
390  @Override()
391  @NotNull()
392  public String toString()
393  {
394    final StringBuilder buffer = new StringBuilder();
395    toString(buffer);
396    return buffer.toString();
397  }
398
399
400
401  /**
402   * Appends a string representation of the DIGEST-MD5 bind request properties
403   * to the provided buffer.
404   *
405   * @param  buffer  The buffer to which the information should be appended.
406   */
407  public void toString(@NotNull final StringBuilder buffer)
408  {
409    buffer.append("DIGESTMD5BindRequestProperties(authenticationID='");
410    buffer.append(authenticationID);
411    buffer.append('\'');
412
413    if (authorizationID != null)
414    {
415      buffer.append(", authorizationID='");
416      buffer.append(authorizationID);
417      buffer.append('\'');
418    }
419
420    if (realm != null)
421    {
422      buffer.append(", realm='");
423      buffer.append(realm);
424      buffer.append('\'');
425    }
426
427    buffer.append(", qop='");
428    buffer.append(SASLQualityOfProtection.toString(allowedQoP));
429    buffer.append("')");
430  }
431}